OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdlib.h> | 5 #include <stdlib.h> |
6 #include <string.h> | 6 #include <string.h> |
7 | 7 |
8 #include "src/snapshot/code-serializer.h" | 8 #include "src/snapshot/code-serializer.h" |
9 #include "src/version.h" | 9 #include "src/version.h" |
10 #include "src/wasm/module-decoder.h" | 10 #include "src/wasm/module-decoder.h" |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 } | 211 } |
212 | 212 |
213 // Approximate gtest TEST_F style, in case we adopt gtest. | 213 // Approximate gtest TEST_F style, in case we adopt gtest. |
214 class WasmSerializationTest { | 214 class WasmSerializationTest { |
215 public: | 215 public: |
216 WasmSerializationTest() : zone_(&allocator_, ZONE_NAME) { | 216 WasmSerializationTest() : zone_(&allocator_, ZONE_NAME) { |
217 // Don't call here if we move to gtest. | 217 // Don't call here if we move to gtest. |
218 SetUp(); | 218 SetUp(); |
219 } | 219 } |
220 | 220 |
| 221 static void BuildWireBytes(Zone* zone, ZoneBuffer* buffer) { |
| 222 WasmModuleBuilder* builder = new (zone) WasmModuleBuilder(zone); |
| 223 TestSignatures sigs; |
| 224 |
| 225 WasmFunctionBuilder* f = builder->AddFunction(sigs.i_i()); |
| 226 byte code[] = {WASM_GET_LOCAL(0), kExprI32Const, 1, kExprI32Add}; |
| 227 EMIT_CODE_WITH_END(f, code); |
| 228 f->ExportAs(CStrVector(kFunctionName)); |
| 229 |
| 230 builder->WriteTo(*buffer); |
| 231 } |
| 232 |
221 void ClearSerializedData() { | 233 void ClearSerializedData() { |
222 serialized_bytes_.first = nullptr; | 234 serialized_bytes_.first = nullptr; |
223 serialized_bytes_.second = 0; | 235 serialized_bytes_.second = 0; |
224 } | 236 } |
225 | 237 |
226 void InvalidateVersion() { | 238 void InvalidateVersion() { |
227 uint32_t* buffer = reinterpret_cast<uint32_t*>( | 239 uint32_t* buffer = reinterpret_cast<uint32_t*>( |
228 const_cast<uint8_t*>(serialized_bytes_.first)); | 240 const_cast<uint8_t*>(serialized_bytes_.first)); |
229 buffer[SerializedCodeData::kVersionHashOffset] = Version::Hash() + 1; | 241 buffer[SerializedCodeData::kVersionHashOffset] = Version::Hash() + 1; |
230 } | 242 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 | 284 |
273 Isolate* current_isolate() { | 285 Isolate* current_isolate() { |
274 return reinterpret_cast<Isolate*>(current_isolate_v8_); | 286 return reinterpret_cast<Isolate*>(current_isolate_v8_); |
275 } | 287 } |
276 | 288 |
277 ~WasmSerializationTest() { | 289 ~WasmSerializationTest() { |
278 // Don't call from here if we move to gtest | 290 // Don't call from here if we move to gtest |
279 TearDown(); | 291 TearDown(); |
280 } | 292 } |
281 | 293 |
| 294 v8::Isolate* current_isolate_v8() { return current_isolate_v8_; } |
| 295 |
282 private: | 296 private: |
283 static const char* kFunctionName; | 297 static const char* kFunctionName; |
284 | 298 |
285 Zone* zone() { return &zone_; } | 299 Zone* zone() { return &zone_; } |
286 const v8::WasmCompiledModule::CallerOwnedBuffer& wire_bytes() const { | 300 const v8::WasmCompiledModule::CallerOwnedBuffer& wire_bytes() const { |
287 return wire_bytes_; | 301 return wire_bytes_; |
288 } | 302 } |
289 | 303 |
290 const v8::WasmCompiledModule::CallerOwnedBuffer& serialized_bytes() const { | 304 const v8::WasmCompiledModule::CallerOwnedBuffer& serialized_bytes() const { |
291 return serialized_bytes_; | 305 return serialized_bytes_; |
292 } | 306 } |
293 | 307 |
294 v8::Isolate* current_isolate_v8() { return current_isolate_v8_; } | |
295 | |
296 void SetUp() { | 308 void SetUp() { |
297 WasmModuleBuilder* builder = new (zone()) WasmModuleBuilder(zone()); | |
298 TestSignatures sigs; | |
299 | |
300 WasmFunctionBuilder* f = builder->AddFunction(sigs.i_i()); | |
301 byte code[] = {WASM_GET_LOCAL(0), kExprI32Const, 1, kExprI32Add}; | |
302 EMIT_CODE_WITH_END(f, code); | |
303 f->ExportAs(CStrVector(kFunctionName)); | |
304 | |
305 ZoneBuffer buffer(&zone_); | 309 ZoneBuffer buffer(&zone_); |
306 builder->WriteTo(buffer); | 310 WasmSerializationTest::BuildWireBytes(zone(), &buffer); |
307 | 311 |
308 Isolate* serialization_isolate = CcTest::InitIsolateOnce(); | 312 Isolate* serialization_isolate = CcTest::InitIsolateOnce(); |
309 ErrorThrower thrower(serialization_isolate, ""); | 313 ErrorThrower thrower(serialization_isolate, ""); |
310 uint8_t* bytes = nullptr; | 314 uint8_t* bytes = nullptr; |
311 size_t bytes_size = 0; | 315 size_t bytes_size = 0; |
312 { | 316 { |
313 HandleScope scope(serialization_isolate); | 317 HandleScope scope(serialization_isolate); |
314 testing::SetupIsolateForWasmModule(serialization_isolate); | 318 testing::SetupIsolateForWasmModule(serialization_isolate); |
315 | 319 |
316 ModuleResult decoding_result = | 320 ModuleResult decoding_result = |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
412 { | 416 { |
413 HandleScope scope(test.current_isolate()); | 417 HandleScope scope(test.current_isolate()); |
414 test.InvalidateVersion(); | 418 test.InvalidateVersion(); |
415 test.InvalidateWireBytes(); | 419 test.InvalidateWireBytes(); |
416 test.Deserialize(); | 420 test.Deserialize(); |
417 } | 421 } |
418 Cleanup(test.current_isolate()); | 422 Cleanup(test.current_isolate()); |
419 Cleanup(); | 423 Cleanup(); |
420 } | 424 } |
421 | 425 |
| 426 bool False(v8::Local<v8::Context> context) { return false; } |
| 427 |
| 428 TEST(BlockWasmCodeGen) { |
| 429 v8::internal::AccountingAllocator allocator; |
| 430 Zone zone(&allocator, ZONE_NAME); |
| 431 ZoneBuffer buffer(&zone); |
| 432 WasmSerializationTest::BuildWireBytes(&zone, &buffer); |
| 433 Isolate* isolate = CcTest::InitIsolateOnce(); |
| 434 HandleScope scope(isolate); |
| 435 testing::SetupIsolateForWasmModule(isolate); |
| 436 CcTest::isolate()->SetAllowCodeGenerationFromStringsCallback(False); |
| 437 |
| 438 ErrorThrower thrower(isolate, "block codegen"); |
| 439 MaybeHandle<WasmModuleObject> ret = wasm::CreateModuleObjectFromBytes( |
| 440 isolate, buffer.begin(), buffer.end(), &thrower, |
| 441 wasm::ModuleOrigin::kWasmOrigin, Handle<v8::internal::Script>::null(), |
| 442 Vector<const byte>::empty()); |
| 443 CcTest::isolate()->SetAllowCodeGenerationFromStringsCallback(nullptr); |
| 444 CHECK(ret.is_null()); |
| 445 CHECK(thrower.error()); |
| 446 } |
| 447 |
| 448 TEST(BlockWasmCodeGenAtDeserialization) { |
| 449 WasmSerializationTest test; |
| 450 { |
| 451 HandleScope scope(test.current_isolate()); |
| 452 test.current_isolate_v8()->SetAllowCodeGenerationFromStringsCallback(False); |
| 453 v8::MaybeLocal<v8::WasmCompiledModule> nothing = test.Deserialize(); |
| 454 CHECK(nothing.IsEmpty()); |
| 455 } |
| 456 Cleanup(test.current_isolate()); |
| 457 Cleanup(); |
| 458 } |
| 459 |
422 TEST(MemorySize) { | 460 TEST(MemorySize) { |
423 { | 461 { |
424 // Initial memory size is 16, see wasm-module-builder.cc | 462 // Initial memory size is 16, see wasm-module-builder.cc |
425 static const int kExpectedValue = 16; | 463 static const int kExpectedValue = 16; |
426 TestSignatures sigs; | 464 TestSignatures sigs; |
427 v8::internal::AccountingAllocator allocator; | 465 v8::internal::AccountingAllocator allocator; |
428 Zone zone(&allocator, ZONE_NAME); | 466 Zone zone(&allocator, ZONE_NAME); |
429 | 467 |
430 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 468 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
431 WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v()); | 469 WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v()); |
(...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
917 }; | 955 }; |
918 | 956 |
919 testing::CompileInstantiateWasmModuleForTesting(isolate, &thrower, data, | 957 testing::CompileInstantiateWasmModuleForTesting(isolate, &thrower, data, |
920 data + arraysize(data), | 958 data + arraysize(data), |
921 ModuleOrigin::kWasmOrigin); | 959 ModuleOrigin::kWasmOrigin); |
922 // It should be possible to instantiate this module. | 960 // It should be possible to instantiate this module. |
923 CHECK(!thrower.error()); | 961 CHECK(!thrower.error()); |
924 } | 962 } |
925 Cleanup(); | 963 Cleanup(); |
926 } | 964 } |
OLD | NEW |