| 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" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 } | 39 } |
| 40 | 40 |
| 41 if (thrower->error()) { | 41 if (thrower->error()) { |
| 42 if (decoding_result.val) delete decoding_result.val; | 42 if (decoding_result.val) delete decoding_result.val; |
| 43 return nullptr; | 43 return nullptr; |
| 44 } | 44 } |
| 45 return decoding_result.val; | 45 return decoding_result.val; |
| 46 } | 46 } |
| 47 | 47 |
| 48 const Handle<WasmInstanceObject> InstantiateModuleForTesting( | 48 const Handle<WasmInstanceObject> InstantiateModuleForTesting( |
| 49 Isolate* isolate, ErrorThrower* thrower, const WasmModule* module) { | 49 Isolate* isolate, ErrorThrower* thrower, const WasmModule* module, |
| 50 CHECK(module != nullptr); | 50 const ModuleWireBytes& wire_bytes) { |
| 51 | 51 DCHECK_NOT_NULL(module); |
| 52 if (module->import_table.size() > 0) { | 52 if (module->import_table.size() > 0) { |
| 53 thrower->CompileError("Not supported: module has imports."); | 53 thrower->CompileError("Not supported: module has imports."); |
| 54 } | 54 } |
| 55 | 55 |
| 56 if (thrower->error()) return Handle<WasmInstanceObject>::null(); | 56 if (thrower->error()) return Handle<WasmInstanceObject>::null(); |
| 57 | 57 |
| 58 // Although we decoded the module for some pre-validation, run the bytes | 58 // Although we decoded the module for some pre-validation, run the bytes |
| 59 // again through the normal pipeline. | 59 // again through the normal pipeline. |
| 60 // TODO(wasm): Use {module} instead of decoding the module bytes again. | 60 // TODO(wasm): Use {module} instead of decoding the module bytes again. |
| 61 MaybeHandle<WasmModuleObject> module_object = CreateModuleObjectFromBytes( | 61 MaybeHandle<WasmModuleObject> module_object = CreateModuleObjectFromBytes( |
| 62 isolate, module->module_start, module->module_end, thrower, | 62 isolate, wire_bytes.module_bytes.start(), wire_bytes.module_bytes.end(), |
| 63 ModuleOrigin::kWasmOrigin, Handle<Script>::null(), nullptr, nullptr); | 63 thrower, ModuleOrigin::kWasmOrigin, Handle<Script>::null(), nullptr, |
| 64 nullptr); |
| 64 if (module_object.is_null()) { | 65 if (module_object.is_null()) { |
| 65 thrower->CompileError("Module pre-validation failed."); | 66 thrower->CompileError("Module pre-validation failed."); |
| 66 return Handle<WasmInstanceObject>::null(); | 67 return Handle<WasmInstanceObject>::null(); |
| 67 } | 68 } |
| 68 MaybeHandle<WasmInstanceObject> maybe_instance = WasmModule::Instantiate( | 69 MaybeHandle<WasmInstanceObject> maybe_instance = WasmModule::Instantiate( |
| 69 isolate, thrower, module_object.ToHandleChecked(), | 70 isolate, thrower, module_object.ToHandleChecked(), |
| 70 Handle<JSReceiver>::null(), Handle<JSArrayBuffer>::null()); | 71 Handle<JSReceiver>::null(), Handle<JSArrayBuffer>::null()); |
| 71 Handle<WasmInstanceObject> instance; | 72 Handle<WasmInstanceObject> instance; |
| 72 if (!maybe_instance.ToHandle(&instance)) { | 73 if (!maybe_instance.ToHandle(&instance)) { |
| 73 return Handle<WasmInstanceObject>::null(); | 74 return Handle<WasmInstanceObject>::null(); |
| 74 } | 75 } |
| 75 return instance; | 76 return instance; |
| 76 } | 77 } |
| 77 | 78 |
| 78 const Handle<WasmInstanceObject> CompileInstantiateWasmModuleForTesting( | 79 const Handle<WasmInstanceObject> CompileInstantiateWasmModuleForTesting( |
| 79 Isolate* isolate, ErrorThrower* thrower, const byte* module_start, | 80 Isolate* isolate, ErrorThrower* thrower, const byte* module_start, |
| 80 const byte* module_end, ModuleOrigin origin) { | 81 const byte* module_end, ModuleOrigin origin) { |
| 81 std::unique_ptr<const WasmModule> module(DecodeWasmModuleForTesting( | 82 std::unique_ptr<const WasmModule> module(DecodeWasmModuleForTesting( |
| 82 isolate, thrower, module_start, module_end, origin)); | 83 isolate, thrower, module_start, module_end, origin)); |
| 83 | 84 |
| 84 if (module == nullptr) { | 85 if (module == nullptr) { |
| 85 thrower->CompileError("Wasm module decoding failed"); | 86 thrower->CompileError("Wasm module decoding failed"); |
| 86 return Handle<WasmInstanceObject>::null(); | 87 return Handle<WasmInstanceObject>::null(); |
| 87 } | 88 } |
| 88 return InstantiateModuleForTesting(isolate, thrower, module.get()); | 89 return InstantiateModuleForTesting(isolate, thrower, module.get(), |
| 90 ModuleWireBytes(module_start, module_end)); |
| 89 } | 91 } |
| 90 | 92 |
| 91 int32_t RunWasmModuleForTesting(Isolate* isolate, Handle<JSObject> instance, | 93 int32_t RunWasmModuleForTesting(Isolate* isolate, Handle<JSObject> instance, |
| 92 int argc, Handle<Object> argv[], | 94 int argc, Handle<Object> argv[], |
| 93 ModuleOrigin origin) { | 95 ModuleOrigin origin) { |
| 94 ErrorThrower thrower(isolate, "RunWasmModule"); | 96 ErrorThrower thrower(isolate, "RunWasmModule"); |
| 95 const char* f_name = origin == ModuleOrigin::kAsmJsOrigin ? "caller" : "main"; | 97 const char* f_name = origin == ModuleOrigin::kAsmJsOrigin ? "caller" : "main"; |
| 96 return CallWasmFunctionForTesting(isolate, instance, &thrower, f_name, argc, | 98 return CallWasmFunctionForTesting(isolate, instance, &thrower, f_name, argc, |
| 97 argv, origin); | 99 argv, origin); |
| 98 } | 100 } |
| 99 | 101 |
| 100 int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start, | 102 int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start, |
| 101 const byte* module_end, ModuleOrigin origin) { | 103 const byte* module_end, ModuleOrigin origin) { |
| 102 HandleScope scope(isolate); | 104 HandleScope scope(isolate); |
| 103 ErrorThrower thrower(isolate, "CompileAndRunWasmModule"); | 105 ErrorThrower thrower(isolate, "CompileAndRunWasmModule"); |
| 104 Handle<JSObject> instance = CompileInstantiateWasmModuleForTesting( | 106 Handle<JSObject> instance = CompileInstantiateWasmModuleForTesting( |
| 105 isolate, &thrower, module_start, module_end, origin); | 107 isolate, &thrower, module_start, module_end, origin); |
| 106 if (instance.is_null()) { | 108 if (instance.is_null()) { |
| 107 return -1; | 109 return -1; |
| 108 } | 110 } |
| 109 return RunWasmModuleForTesting(isolate, instance, 0, nullptr, origin); | 111 return RunWasmModuleForTesting(isolate, instance, 0, nullptr, origin); |
| 110 } | 112 } |
| 111 | 113 |
| 112 int32_t InterpretWasmModule(Isolate* isolate, ErrorThrower* thrower, | 114 int32_t InterpretWasmModule(Isolate* isolate, ErrorThrower* thrower, |
| 113 const WasmModule* module, int function_index, | 115 const WasmModule* module, |
| 114 WasmVal* args, bool* possible_nondeterminism) { | 116 const ModuleWireBytes& wire_bytes, |
| 115 CHECK(module != nullptr); | 117 int function_index, WasmVal* args, |
| 116 | 118 bool* possible_nondeterminism) { |
| 119 DCHECK_NOT_NULL(module); |
| 117 Zone zone(isolate->allocator(), ZONE_NAME); | 120 Zone zone(isolate->allocator(), ZONE_NAME); |
| 118 v8::internal::HandleScope scope(isolate); | 121 v8::internal::HandleScope scope(isolate); |
| 119 | 122 |
| 120 if (module->import_table.size() > 0) { | 123 if (module->import_table.size() > 0) { |
| 121 thrower->CompileError("Not supported: module has imports."); | 124 thrower->CompileError("Not supported: module has imports."); |
| 122 } | 125 } |
| 123 if (module->export_table.size() == 0) { | 126 if (module->export_table.size() == 0) { |
| 124 thrower->CompileError("Not supported: module has no exports."); | 127 thrower->CompileError("Not supported: module has no exports."); |
| 125 } | 128 } |
| 126 | 129 |
| 127 if (thrower->error()) return -1; | 130 if (thrower->error()) return -1; |
| 128 | 131 |
| 129 // The code verifies, we create an instance to run it in the interpreter. | 132 // The code verifies, we create an instance to run it in the interpreter. |
| 130 WasmInstance instance(module); | 133 WasmInstance instance(module); |
| 131 instance.context = isolate->native_context(); | 134 instance.context = isolate->native_context(); |
| 132 instance.mem_size = GetMinModuleMemSize(module); | 135 instance.mem_size = GetMinModuleMemSize(module); |
| 133 // TODO(ahaas): Move memory allocation to wasm-module.cc for better | 136 // TODO(ahaas): Move memory allocation to wasm-module.cc for better |
| 134 // encapsulation. | 137 // encapsulation. |
| 135 instance.mem_start = | 138 instance.mem_start = |
| 136 static_cast<byte*>(calloc(GetMinModuleMemSize(module), 1)); | 139 static_cast<byte*>(calloc(GetMinModuleMemSize(module), 1)); |
| 137 instance.globals_start = nullptr; | 140 instance.globals_start = nullptr; |
| 138 | 141 |
| 139 WasmInterpreter interpreter(&instance, isolate->allocator()); | 142 ModuleBytesEnv env(module, &instance, wire_bytes); |
| 143 WasmInterpreter interpreter(env, isolate->allocator()); |
| 140 | 144 |
| 141 WasmInterpreter::Thread* thread = interpreter.GetThread(0); | 145 WasmInterpreter::Thread* thread = interpreter.GetThread(0); |
| 142 thread->Reset(); | 146 thread->Reset(); |
| 143 thread->PushFrame(&(module->functions[function_index]), args); | 147 thread->PushFrame(&(module->functions[function_index]), args); |
| 144 WasmInterpreter::State interpreter_result = thread->Run(); | 148 WasmInterpreter::State interpreter_result = thread->Run(); |
| 145 if (instance.mem_start) { | 149 if (instance.mem_start) { |
| 146 free(instance.mem_start); | 150 free(instance.mem_start); |
| 147 } | 151 } |
| 148 *possible_nondeterminism = thread->PossibleNondeterminism(); | 152 *possible_nondeterminism = thread->PossibleNondeterminism(); |
| 149 if (interpreter_result == WasmInterpreter::FINISHED) { | 153 if (interpreter_result == WasmInterpreter::FINISHED) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 | 206 |
| 203 void SetupIsolateForWasmModule(Isolate* isolate) { | 207 void SetupIsolateForWasmModule(Isolate* isolate) { |
| 204 WasmJs::InstallWasmMapsIfNeeded(isolate, isolate->native_context()); | 208 WasmJs::InstallWasmMapsIfNeeded(isolate, isolate->native_context()); |
| 205 WasmJs::InstallWasmModuleSymbolIfNeeded(isolate, isolate->global_object(), | 209 WasmJs::InstallWasmModuleSymbolIfNeeded(isolate, isolate->global_object(), |
| 206 isolate->native_context()); | 210 isolate->native_context()); |
| 207 } | 211 } |
| 208 } // namespace testing | 212 } // namespace testing |
| 209 } // namespace wasm | 213 } // namespace wasm |
| 210 } // namespace internal | 214 } // namespace internal |
| 211 } // namespace v8 | 215 } // namespace v8 |
| OLD | NEW |