| OLD | NEW |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 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 "test/common/wasm/wasm-module-runner.h" | 5 #include "test/common/wasm/wasm-module-runner.h" |
| 6 | 6 |
| 7 #include "src/handles.h" | 7 #include "src/handles.h" |
| 8 #include "src/isolate.h" | 8 #include "src/isolate.h" |
| 9 #include "src/objects.h" | 9 #include "src/objects.h" |
| 10 #include "src/property-descriptor.h" | 10 #include "src/property-descriptor.h" |
| 11 #include "src/wasm/module-decoder.h" | 11 #include "src/wasm/module-decoder.h" |
| 12 #include "src/wasm/wasm-interpreter.h" | 12 #include "src/wasm/wasm-interpreter.h" |
| 13 #include "src/wasm/wasm-js.h" | 13 #include "src/wasm/wasm-js.h" |
| 14 #include "src/wasm/wasm-module.h" | 14 #include "src/wasm/wasm-module.h" |
| 15 #include "src/wasm/wasm-result.h" | 15 #include "src/wasm/wasm-result.h" |
| 16 #include "src/zone/zone.h" | |
| 17 | 16 |
| 18 namespace v8 { | 17 namespace v8 { |
| 19 namespace internal { | 18 namespace internal { |
| 20 namespace wasm { | 19 namespace wasm { |
| 21 namespace testing { | 20 namespace testing { |
| 22 | 21 |
| 23 uint32_t GetMinModuleMemSize(const WasmModule* module) { | 22 uint32_t GetMinModuleMemSize(const WasmModule* module) { |
| 24 return WasmModule::kPageSize * module->min_mem_pages; | 23 return WasmModule::kPageSize * module->min_mem_pages; |
| 25 } | 24 } |
| 26 | 25 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 50 ErrorThrower* thrower, | 49 ErrorThrower* thrower, |
| 51 const WasmModule* module) { | 50 const WasmModule* module) { |
| 52 CHECK(module != nullptr); | 51 CHECK(module != nullptr); |
| 53 | 52 |
| 54 if (module->import_table.size() > 0) { | 53 if (module->import_table.size() > 0) { |
| 55 thrower->Error("Not supported: module has imports."); | 54 thrower->Error("Not supported: module has imports."); |
| 56 } | 55 } |
| 57 if (module->export_table.size() == 0) { | 56 if (module->export_table.size() == 0) { |
| 58 thrower->Error("Not supported: module has no exports."); | 57 thrower->Error("Not supported: module has no exports."); |
| 59 } | 58 } |
| 60 | |
| 61 if (thrower->error()) return Handle<JSObject>::null(); | 59 if (thrower->error()) return Handle<JSObject>::null(); |
| 62 | 60 |
| 63 // Although we decoded the module for some pre-validation, run the bytes | 61 // Although we decoded the module for some pre-validation, run the bytes |
| 64 // again through the normal pipeline. | 62 // again through the normal pipeline. |
| 65 MaybeHandle<JSObject> module_object = CreateModuleObjectFromBytes( | 63 MaybeHandle<JSObject> module_object = CreateModuleObjectFromBytes( |
| 66 isolate, module->module_start, module->module_end, thrower, | 64 isolate, module->module_start, module->module_end, thrower, |
| 67 ModuleOrigin::kWasmOrigin); | 65 ModuleOrigin::kWasmOrigin); |
| 68 if (module_object.is_null()) return Handle<JSObject>::null(); | 66 if (module_object.is_null()) { |
| 67 thrower->Error("Module pre-validation failed."); |
| 68 return Handle<JSObject>::null(); |
| 69 } |
| 69 MaybeHandle<JSObject> maybe_instance = WasmModule::Instantiate( | 70 MaybeHandle<JSObject> maybe_instance = WasmModule::Instantiate( |
| 70 isolate, thrower, module_object.ToHandleChecked(), | 71 isolate, thrower, module_object.ToHandleChecked(), |
| 71 Handle<JSReceiver>::null(), Handle<JSArrayBuffer>::null()); | 72 Handle<JSReceiver>::null(), Handle<JSArrayBuffer>::null()); |
| 72 Handle<JSObject> instance; | 73 Handle<JSObject> instance; |
| 73 if (!maybe_instance.ToHandle(&instance)) { | 74 if (!maybe_instance.ToHandle(&instance)) { |
| 74 return Handle<JSObject>::null(); | 75 return Handle<JSObject>::null(); |
| 75 } | 76 } |
| 76 return instance; | 77 return instance; |
| 77 } | 78 } |
| 78 | 79 |
| 80 const Handle<JSObject> CompileInstantiateWasmModuleForTesting( |
| 81 Isolate* isolate, Zone* zone, const byte* module_start, |
| 82 const byte* module_end, ModuleOrigin origin) { |
| 83 ErrorThrower thrower(isolate, "CompileInstantiateWasmModule"); |
| 84 std::unique_ptr<const WasmModule> module(DecodeWasmModuleForTesting( |
| 85 isolate, zone, &thrower, module_start, module_end, origin)); |
| 86 |
| 87 if (module == nullptr) { |
| 88 thrower.Error("Wasm module decode failed"); |
| 89 return Handle<JSObject>::null(); |
| 90 } |
| 91 return InstantiateModuleForTesting(isolate, &thrower, module.get()); |
| 92 } |
| 93 |
| 94 int32_t RunWasmModuleForTesting(Isolate* isolate, Handle<JSObject> instance, |
| 95 int argc, Handle<Object> argv[], |
| 96 ModuleOrigin origin) { |
| 97 ErrorThrower thrower(isolate, "RunWasmModule"); |
| 98 const char* f_name = origin == ModuleOrigin::kAsmJsOrigin ? "caller" : "main"; |
| 99 return CallWasmFunctionForTesting(isolate, instance, &thrower, f_name, argc, |
| 100 argv, origin); |
| 101 } |
| 102 |
| 79 int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start, | 103 int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start, |
| 80 const byte* module_end, ModuleOrigin origin) { | 104 const byte* module_end, ModuleOrigin origin) { |
| 81 HandleScope scope(isolate); | 105 HandleScope scope(isolate); |
| 82 Zone zone(isolate->allocator()); | 106 Zone zone(isolate->allocator()); |
| 83 | 107 |
| 84 ErrorThrower thrower(isolate, "CompileAndRunWasmModule"); | 108 Handle<JSObject> instance = CompileInstantiateWasmModuleForTesting( |
| 85 std::unique_ptr<const WasmModule> module(DecodeWasmModuleForTesting( | 109 isolate, &zone, module_start, module_end, origin); |
| 86 isolate, &zone, &thrower, module_start, module_end, origin)); | |
| 87 | |
| 88 if (module == nullptr) { | |
| 89 return -1; | |
| 90 } | |
| 91 Handle<JSObject> instance = | |
| 92 InstantiateModuleForTesting(isolate, &thrower, module.get()); | |
| 93 if (instance.is_null()) { | 110 if (instance.is_null()) { |
| 94 return -1; | 111 return -1; |
| 95 } | 112 } |
| 96 const char* f_name = origin == ModuleOrigin::kAsmJsOrigin ? "caller" : "main"; | 113 return RunWasmModuleForTesting(isolate, instance, 0, nullptr, origin); |
| 97 return CallWasmFunctionForTesting(isolate, instance, &thrower, f_name, 0, | |
| 98 nullptr, origin); | |
| 99 } | 114 } |
| 100 | 115 |
| 101 int32_t InterpretWasmModule(Isolate* isolate, ErrorThrower* thrower, | 116 int32_t InterpretWasmModule(Isolate* isolate, ErrorThrower* thrower, |
| 102 const WasmModule* module, int function_index, | 117 const WasmModule* module, int function_index, |
| 103 WasmVal* args) { | 118 WasmVal* args) { |
| 104 CHECK(module != nullptr); | 119 CHECK(module != nullptr); |
| 105 | 120 |
| 106 Zone zone(isolate->allocator()); | 121 Zone zone(isolate->allocator()); |
| 107 v8::internal::HandleScope scope(isolate); | 122 v8::internal::HandleScope scope(isolate); |
| 108 | 123 |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 void SetupIsolateForWasmModule(Isolate* isolate) { | 222 void SetupIsolateForWasmModule(Isolate* isolate) { |
| 208 WasmJs::InstallWasmMapsIfNeeded(isolate, isolate->native_context()); | 223 WasmJs::InstallWasmMapsIfNeeded(isolate, isolate->native_context()); |
| 209 WasmJs::InstallWasmModuleSymbolIfNeeded(isolate, isolate->global_object(), | 224 WasmJs::InstallWasmModuleSymbolIfNeeded(isolate, isolate->global_object(), |
| 210 isolate->native_context()); | 225 isolate->native_context()); |
| 211 } | 226 } |
| 212 | 227 |
| 213 } // namespace testing | 228 } // namespace testing |
| 214 } // namespace wasm | 229 } // namespace wasm |
| 215 } // namespace internal | 230 } // namespace internal |
| 216 } // namespace v8 | 231 } // namespace v8 |
| OLD | NEW |