| 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 18 matching lines...) Expand all Loading... |
| 29 const byte* module_end, | 29 const byte* module_end, |
| 30 ModuleOrigin origin) { | 30 ModuleOrigin origin) { |
| 31 // Decode the module, but don't verify function bodies, since we'll | 31 // Decode the module, but don't verify function bodies, since we'll |
| 32 // be compiling them anyway. | 32 // be compiling them anyway. |
| 33 ModuleResult decoding_result = | 33 ModuleResult decoding_result = |
| 34 DecodeWasmModule(isolate, zone, module_start, module_end, false, origin); | 34 DecodeWasmModule(isolate, zone, module_start, module_end, false, origin); |
| 35 | 35 |
| 36 std::unique_ptr<const WasmModule> module(decoding_result.val); | 36 std::unique_ptr<const WasmModule> module(decoding_result.val); |
| 37 if (decoding_result.failed()) { | 37 if (decoding_result.failed()) { |
| 38 // Module verification failed. throw. | 38 // Module verification failed. throw. |
| 39 thrower->Error("WASM.compileRun() failed: %s", | 39 thrower->CompileError("WASM.compileRun() failed: %s", |
| 40 decoding_result.error_msg.get()); | 40 decoding_result.error_msg.get()); |
| 41 return nullptr; | 41 return nullptr; |
| 42 } | 42 } |
| 43 | 43 |
| 44 if (thrower->error()) return nullptr; | 44 if (thrower->error()) return nullptr; |
| 45 return module.release(); | 45 return module.release(); |
| 46 } | 46 } |
| 47 | 47 |
| 48 const Handle<JSObject> InstantiateModuleForTesting(Isolate* isolate, | 48 const Handle<JSObject> InstantiateModuleForTesting(Isolate* isolate, |
| 49 ErrorThrower* thrower, | 49 ErrorThrower* thrower, |
| 50 const WasmModule* module) { | 50 const WasmModule* module) { |
| 51 CHECK(module != nullptr); | 51 CHECK(module != nullptr); |
| 52 | 52 |
| 53 if (module->import_table.size() > 0) { | 53 if (module->import_table.size() > 0) { |
| 54 thrower->Error("Not supported: module has imports."); | 54 thrower->CompileError("Not supported: module has imports."); |
| 55 } | 55 } |
| 56 if (module->export_table.size() == 0) { | 56 if (module->export_table.size() == 0) { |
| 57 thrower->Error("Not supported: module has no exports."); | 57 thrower->CompileError("Not supported: module has no exports."); |
| 58 } | 58 } |
| 59 if (thrower->error()) return Handle<JSObject>::null(); | 59 if (thrower->error()) return Handle<JSObject>::null(); |
| 60 | 60 |
| 61 // 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 |
| 62 // again through the normal pipeline. | 62 // again through the normal pipeline. |
| 63 MaybeHandle<JSObject> module_object = CreateModuleObjectFromBytes( | 63 MaybeHandle<JSObject> module_object = CreateModuleObjectFromBytes( |
| 64 isolate, module->module_start, module->module_end, thrower, | 64 isolate, module->module_start, module->module_end, thrower, |
| 65 ModuleOrigin::kWasmOrigin, Handle<Script>::null(), nullptr, nullptr); | 65 ModuleOrigin::kWasmOrigin, Handle<Script>::null(), nullptr, nullptr); |
| 66 if (module_object.is_null()) { | 66 if (module_object.is_null()) { |
| 67 thrower->Error("Module pre-validation failed."); | 67 thrower->CompileError("Module pre-validation failed."); |
| 68 return Handle<JSObject>::null(); | 68 return Handle<JSObject>::null(); |
| 69 } | 69 } |
| 70 MaybeHandle<JSObject> maybe_instance = WasmModule::Instantiate( | 70 MaybeHandle<JSObject> maybe_instance = WasmModule::Instantiate( |
| 71 isolate, thrower, module_object.ToHandleChecked(), | 71 isolate, thrower, module_object.ToHandleChecked(), |
| 72 Handle<JSReceiver>::null(), Handle<JSArrayBuffer>::null()); | 72 Handle<JSReceiver>::null(), Handle<JSArrayBuffer>::null()); |
| 73 Handle<JSObject> instance; | 73 Handle<JSObject> instance; |
| 74 if (!maybe_instance.ToHandle(&instance)) { | 74 if (!maybe_instance.ToHandle(&instance)) { |
| 75 return Handle<JSObject>::null(); | 75 return Handle<JSObject>::null(); |
| 76 } | 76 } |
| 77 return instance; | 77 return instance; |
| 78 } | 78 } |
| 79 | 79 |
| 80 const Handle<JSObject> CompileInstantiateWasmModuleForTesting( | 80 const Handle<JSObject> CompileInstantiateWasmModuleForTesting( |
| 81 Isolate* isolate, ErrorThrower* thrower, Zone* zone, | 81 Isolate* isolate, ErrorThrower* thrower, Zone* zone, |
| 82 const byte* module_start, const byte* module_end, ModuleOrigin origin) { | 82 const byte* module_start, const byte* module_end, ModuleOrigin origin) { |
| 83 std::unique_ptr<const WasmModule> module(DecodeWasmModuleForTesting( | 83 std::unique_ptr<const WasmModule> module(DecodeWasmModuleForTesting( |
| 84 isolate, zone, thrower, module_start, module_end, origin)); | 84 isolate, zone, thrower, module_start, module_end, origin)); |
| 85 | 85 |
| 86 if (module == nullptr) { | 86 if (module == nullptr) { |
| 87 thrower->Error("Wasm module decode failed"); | 87 thrower->CompileError("Wasm module decoding failed"); |
| 88 return Handle<JSObject>::null(); | 88 return Handle<JSObject>::null(); |
| 89 } | 89 } |
| 90 return InstantiateModuleForTesting(isolate, thrower, module.get()); | 90 return InstantiateModuleForTesting(isolate, thrower, module.get()); |
| 91 } | 91 } |
| 92 | 92 |
| 93 int32_t RunWasmModuleForTesting(Isolate* isolate, Handle<JSObject> instance, | 93 int32_t RunWasmModuleForTesting(Isolate* isolate, Handle<JSObject> instance, |
| 94 int argc, Handle<Object> argv[], | 94 int argc, Handle<Object> argv[], |
| 95 ModuleOrigin origin) { | 95 ModuleOrigin origin) { |
| 96 ErrorThrower thrower(isolate, "RunWasmModule"); | 96 ErrorThrower thrower(isolate, "RunWasmModule"); |
| 97 const char* f_name = origin == ModuleOrigin::kAsmJsOrigin ? "caller" : "main"; | 97 const char* f_name = origin == ModuleOrigin::kAsmJsOrigin ? "caller" : "main"; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 114 | 114 |
| 115 int32_t InterpretWasmModule(Isolate* isolate, ErrorThrower* thrower, | 115 int32_t InterpretWasmModule(Isolate* isolate, ErrorThrower* thrower, |
| 116 const WasmModule* module, int function_index, | 116 const WasmModule* module, int function_index, |
| 117 WasmVal* args) { | 117 WasmVal* args) { |
| 118 CHECK(module != nullptr); | 118 CHECK(module != nullptr); |
| 119 | 119 |
| 120 Zone zone(isolate->allocator()); | 120 Zone zone(isolate->allocator()); |
| 121 v8::internal::HandleScope scope(isolate); | 121 v8::internal::HandleScope scope(isolate); |
| 122 | 122 |
| 123 if (module->import_table.size() > 0) { | 123 if (module->import_table.size() > 0) { |
| 124 thrower->Error("Not supported: module has imports."); | 124 thrower->CompileError("Not supported: module has imports."); |
| 125 } | 125 } |
| 126 if (module->export_table.size() == 0) { | 126 if (module->export_table.size() == 0) { |
| 127 thrower->Error("Not supported: module has no exports."); | 127 thrower->CompileError("Not supported: module has no exports."); |
| 128 } | 128 } |
| 129 | 129 |
| 130 if (thrower->error()) return -1; | 130 if (thrower->error()) return -1; |
| 131 | 131 |
| 132 ModuleEnv module_env; | 132 ModuleEnv module_env; |
| 133 module_env.module = module; | 133 module_env.module = module; |
| 134 module_env.origin = module->origin; | 134 module_env.origin = module->origin; |
| 135 | 135 |
| 136 for (size_t i = 0; i < module->functions.size(); i++) { | 136 for (size_t i = 0; i < module->functions.size(); i++) { |
| 137 FunctionBody body = { | 137 FunctionBody body = { |
| 138 &module_env, module->functions[i].sig, module->module_start, | 138 &module_env, module->functions[i].sig, module->module_start, |
| 139 module->module_start + module->functions[i].code_start_offset, | 139 module->module_start + module->functions[i].code_start_offset, |
| 140 module->module_start + module->functions[i].code_end_offset}; | 140 module->module_start + module->functions[i].code_end_offset}; |
| 141 DecodeResult result = VerifyWasmCode(isolate->allocator(), body); | 141 DecodeResult result = VerifyWasmCode(isolate->allocator(), body); |
| 142 if (result.failed()) { | 142 if (result.failed()) { |
| 143 thrower->Error("Function did not verify"); | 143 thrower->CompileError("Function did not verify"); |
| 144 return -1; | 144 return -1; |
| 145 } | 145 } |
| 146 } | 146 } |
| 147 | 147 |
| 148 // The code verifies, we create an instance to run it in the interpreter. | 148 // The code verifies, we create an instance to run it in the interpreter. |
| 149 WasmInstance instance(module); | 149 WasmInstance instance(module); |
| 150 instance.context = isolate->native_context(); | 150 instance.context = isolate->native_context(); |
| 151 instance.mem_size = GetMinModuleMemSize(module); | 151 instance.mem_size = GetMinModuleMemSize(module); |
| 152 // TODO(ahaas): Move memory allocation to wasm-module.cc for better | 152 // TODO(ahaas): Move memory allocation to wasm-module.cc for better |
| 153 // encapsulation. | 153 // encapsulation. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 164 WasmInterpreter::State interpreter_result = thread->Run(); | 164 WasmInterpreter::State interpreter_result = thread->Run(); |
| 165 if (instance.mem_start) { | 165 if (instance.mem_start) { |
| 166 free(instance.mem_start); | 166 free(instance.mem_start); |
| 167 } | 167 } |
| 168 if (interpreter_result == WasmInterpreter::FINISHED) { | 168 if (interpreter_result == WasmInterpreter::FINISHED) { |
| 169 WasmVal val = thread->GetReturnValue(); | 169 WasmVal val = thread->GetReturnValue(); |
| 170 return val.to<int32_t>(); | 170 return val.to<int32_t>(); |
| 171 } else if (thread->state() == WasmInterpreter::TRAPPED) { | 171 } else if (thread->state() == WasmInterpreter::TRAPPED) { |
| 172 return 0xdeadbeef; | 172 return 0xdeadbeef; |
| 173 } else { | 173 } else { |
| 174 thrower->Error( | 174 thrower->RangeError( |
| 175 "Interpreter did not finish execution within its step bound"); | 175 "Interpreter did not finish execution within its step bound"); |
| 176 return -1; | 176 return -1; |
| 177 } | 177 } |
| 178 } | 178 } |
| 179 | 179 |
| 180 int32_t CallWasmFunctionForTesting(Isolate* isolate, Handle<JSObject> instance, | 180 int32_t CallWasmFunctionForTesting(Isolate* isolate, Handle<JSObject> instance, |
| 181 ErrorThrower* thrower, const char* name, | 181 ErrorThrower* thrower, const char* name, |
| 182 int argc, Handle<Object> argv[], | 182 int argc, Handle<Object> argv[], |
| 183 ModuleOrigin origin) { | 183 ModuleOrigin origin) { |
| 184 Handle<JSObject> exports_object; | 184 Handle<JSObject> exports_object; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 197 | 197 |
| 198 Handle<JSFunction> main_export = Handle<JSFunction>::cast(desc.value()); | 198 Handle<JSFunction> main_export = Handle<JSFunction>::cast(desc.value()); |
| 199 | 199 |
| 200 // Call the JS function. | 200 // Call the JS function. |
| 201 Handle<Object> undefined = isolate->factory()->undefined_value(); | 201 Handle<Object> undefined = isolate->factory()->undefined_value(); |
| 202 MaybeHandle<Object> retval = | 202 MaybeHandle<Object> retval = |
| 203 Execution::Call(isolate, main_export, undefined, argc, argv); | 203 Execution::Call(isolate, main_export, undefined, argc, argv); |
| 204 | 204 |
| 205 // The result should be a number. | 205 // The result should be a number. |
| 206 if (retval.is_null()) { | 206 if (retval.is_null()) { |
| 207 thrower->Error("WASM.compileRun() failed: Invocation was null"); | 207 thrower->RuntimeError("WASM.compileRun() failed: Invocation was null"); |
| 208 return -1; | 208 return -1; |
| 209 } | 209 } |
| 210 Handle<Object> result = retval.ToHandleChecked(); | 210 Handle<Object> result = retval.ToHandleChecked(); |
| 211 if (result->IsSmi()) { | 211 if (result->IsSmi()) { |
| 212 return Smi::cast(*result)->value(); | 212 return Smi::cast(*result)->value(); |
| 213 } | 213 } |
| 214 if (result->IsHeapNumber()) { | 214 if (result->IsHeapNumber()) { |
| 215 return static_cast<int32_t>(HeapNumber::cast(*result)->value()); | 215 return static_cast<int32_t>(HeapNumber::cast(*result)->value()); |
| 216 } | 216 } |
| 217 thrower->Error("WASM.compileRun() failed: Return value should be number"); | 217 thrower->RuntimeError( |
| 218 "WASM.compileRun() failed: Return value should be number"); |
| 218 return -1; | 219 return -1; |
| 219 } | 220 } |
| 220 | 221 |
| 221 void SetupIsolateForWasmModule(Isolate* isolate) { | 222 void SetupIsolateForWasmModule(Isolate* isolate) { |
| 222 WasmJs::InstallWasmMapsIfNeeded(isolate, isolate->native_context()); | 223 WasmJs::InstallWasmMapsIfNeeded(isolate, isolate->native_context()); |
| 223 WasmJs::InstallWasmModuleSymbolIfNeeded(isolate, isolate->global_object(), | 224 WasmJs::InstallWasmModuleSymbolIfNeeded(isolate, isolate->global_object(), |
| 224 isolate->native_context()); | 225 isolate->native_context()); |
| 225 } | 226 } |
| 226 } // namespace testing | 227 } // namespace testing |
| 227 } // namespace wasm | 228 } // namespace wasm |
| 228 } // namespace internal | 229 } // namespace internal |
| 229 } // namespace v8 | 230 } // namespace v8 |
| OLD | NEW |