OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "test/cctest/wasm/wasm-module-runner.h" |
| 6 |
| 7 #include "src/handles.h" |
| 8 #include "src/isolate.h" |
| 9 #include "src/objects.h" |
| 10 #include "src/property-descriptor.h" |
| 11 #include "src/wasm/module-decoder.h" |
| 12 #include "src/wasm/wasm-interpreter.h" |
| 13 #include "src/wasm/wasm-module.h" |
| 14 #include "src/wasm/wasm-result.h" |
| 15 #include "src/zone.h" |
| 16 |
| 17 namespace v8 { |
| 18 namespace internal { |
| 19 namespace wasm { |
| 20 namespace testing { |
| 21 |
| 22 uint32_t GetMinModuleMemSize(const WasmModule* module) { |
| 23 return WasmModule::kPageSize * module->min_mem_pages; |
| 24 } |
| 25 |
| 26 const WasmModule* DecodeWasmModuleForTesting(Isolate* isolate, Zone* zone, |
| 27 ErrorThrower& thrower, |
| 28 const byte* module_start, |
| 29 const byte* module_end, |
| 30 ModuleOrigin origin) { |
| 31 // Decode the module, but don't verify function bodies, since we'll |
| 32 // be compiling them anyway. |
| 33 ModuleResult decoding_result = |
| 34 DecodeWasmModule(isolate, zone, module_start, module_end, false, origin); |
| 35 |
| 36 std::unique_ptr<const WasmModule> module(decoding_result.val); |
| 37 if (decoding_result.failed()) { |
| 38 // Module verification failed. throw. |
| 39 thrower.Error("WASM.compileRun() failed: %s", |
| 40 decoding_result.error_msg.get()); |
| 41 return nullptr; |
| 42 } |
| 43 |
| 44 if (thrower.error()) return nullptr; |
| 45 return module.release(); |
| 46 } |
| 47 |
| 48 const Handle<JSObject> InstantiateModuleForTesting(Isolate* isolate, |
| 49 ErrorThrower& thrower, |
| 50 const WasmModule* module) { |
| 51 CHECK(module != nullptr); |
| 52 |
| 53 if (module->import_table.size() > 0) { |
| 54 thrower.Error("Not supported: module has imports."); |
| 55 } |
| 56 if (module->export_table.size() == 0) { |
| 57 thrower.Error("Not supported: module has no exports."); |
| 58 } |
| 59 |
| 60 if (thrower.error()) return Handle<JSObject>::null(); |
| 61 |
| 62 MaybeHandle<FixedArray> compiled_module = |
| 63 module->CompileFunctions(isolate, &thrower); |
| 64 |
| 65 if (compiled_module.is_null()) return Handle<JSObject>::null(); |
| 66 return WasmModule::Instantiate(isolate, compiled_module.ToHandleChecked(), |
| 67 Handle<JSReceiver>::null(), |
| 68 Handle<JSArrayBuffer>::null()) |
| 69 .ToHandleChecked(); |
| 70 } |
| 71 |
| 72 int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start, |
| 73 const byte* module_end, bool asm_js) { |
| 74 HandleScope scope(isolate); |
| 75 Zone zone(isolate->allocator()); |
| 76 |
| 77 ErrorThrower thrower(isolate, "CompileAndRunWasmModule"); |
| 78 std::unique_ptr<const WasmModule> module(DecodeWasmModuleForTesting( |
| 79 isolate, &zone, thrower, module_start, module_end, |
| 80 asm_js ? kAsmJsOrigin : kWasmOrigin)); |
| 81 |
| 82 if (module == nullptr) { |
| 83 return -1; |
| 84 } |
| 85 Handle<JSObject> instance = |
| 86 InstantiateModuleForTesting(isolate, thrower, module.get()); |
| 87 if (instance.is_null()) { |
| 88 return -1; |
| 89 } |
| 90 return CallWasmFunctionForTesting(isolate, instance, thrower, |
| 91 asm_js ? "caller" : "main", 0, nullptr, |
| 92 asm_js); |
| 93 } |
| 94 |
| 95 int32_t InterpretWasmModule(Isolate* isolate, ErrorThrower& thrower, |
| 96 const WasmModule* module, int function_index, |
| 97 WasmVal* args) { |
| 98 CHECK(module != nullptr); |
| 99 |
| 100 Zone zone(isolate->allocator()); |
| 101 v8::internal::HandleScope scope(isolate); |
| 102 |
| 103 if (module->import_table.size() > 0) { |
| 104 thrower.Error("Not supported: module has imports."); |
| 105 } |
| 106 if (module->export_table.size() == 0) { |
| 107 thrower.Error("Not supported: module has no exports."); |
| 108 } |
| 109 |
| 110 if (thrower.error()) return -1; |
| 111 |
| 112 WasmModuleInstance instance(module); |
| 113 instance.context = isolate->native_context(); |
| 114 instance.mem_size = GetMinModuleMemSize(module); |
| 115 instance.mem_start = nullptr; |
| 116 instance.globals_start = nullptr; |
| 117 |
| 118 ModuleEnv module_env; |
| 119 module_env.module = module; |
| 120 module_env.instance = &instance; |
| 121 module_env.origin = module->origin; |
| 122 |
| 123 const WasmFunction* function = &(module->functions[function_index]); |
| 124 |
| 125 FunctionBody body = {&module_env, function->sig, module->module_start, |
| 126 module->module_start + function->code_start_offset, |
| 127 module->module_start + function->code_end_offset}; |
| 128 DecodeResult result = VerifyWasmCode(isolate->allocator(), body); |
| 129 if (result.failed()) { |
| 130 thrower.Error("Function did not verify"); |
| 131 return -1; |
| 132 } |
| 133 |
| 134 WasmInterpreter interpreter(&instance, isolate->allocator()); |
| 135 |
| 136 WasmInterpreter::Thread* thread = interpreter.GetThread(0); |
| 137 thread->Reset(); |
| 138 thread->PushFrame(function, args); |
| 139 if (thread->Run() == WasmInterpreter::FINISHED) { |
| 140 WasmVal val = thread->GetReturnValue(); |
| 141 return val.to<int32_t>(); |
| 142 } else if (thread->state() == WasmInterpreter::TRAPPED) { |
| 143 return 0xdeadbeef; |
| 144 } else { |
| 145 thrower.Error("Interpreter did not finish execution within its step bound"); |
| 146 return -1; |
| 147 } |
| 148 } |
| 149 |
| 150 int32_t CallWasmFunctionForTesting(Isolate* isolate, Handle<JSObject> instance, |
| 151 ErrorThrower& thrower, const char* name, |
| 152 int argc, Handle<Object> argv[], |
| 153 bool asm_js) { |
| 154 Handle<JSObject> exports_object; |
| 155 if (asm_js) { |
| 156 exports_object = instance; |
| 157 } else { |
| 158 Handle<Name> exports = isolate->factory()->InternalizeUtf8String("exports"); |
| 159 exports_object = Handle<JSObject>::cast( |
| 160 JSObject::GetProperty(instance, exports).ToHandleChecked()); |
| 161 } |
| 162 Handle<Name> main_name = isolate->factory()->NewStringFromAsciiChecked(name); |
| 163 PropertyDescriptor desc; |
| 164 Maybe<bool> property_found = JSReceiver::GetOwnPropertyDescriptor( |
| 165 isolate, exports_object, main_name, &desc); |
| 166 if (!property_found.FromMaybe(false)) return -1; |
| 167 |
| 168 Handle<JSFunction> main_export = Handle<JSFunction>::cast(desc.value()); |
| 169 |
| 170 // Call the JS function. |
| 171 Handle<Object> undefined = isolate->factory()->undefined_value(); |
| 172 MaybeHandle<Object> retval = |
| 173 Execution::Call(isolate, main_export, undefined, argc, argv); |
| 174 |
| 175 // The result should be a number. |
| 176 if (retval.is_null()) { |
| 177 thrower.Error("WASM.compileRun() failed: Invocation was null"); |
| 178 return -1; |
| 179 } |
| 180 Handle<Object> result = retval.ToHandleChecked(); |
| 181 if (result->IsSmi()) { |
| 182 return Smi::cast(*result)->value(); |
| 183 } |
| 184 if (result->IsHeapNumber()) { |
| 185 return static_cast<int32_t>(HeapNumber::cast(*result)->value()); |
| 186 } |
| 187 thrower.Error("WASM.compileRun() failed: Return value should be number"); |
| 188 return -1; |
| 189 } |
| 190 |
| 191 } // namespace testing |
| 192 } // namespace wasm |
| 193 } // namespace internal |
| 194 } // namespace v8 |
OLD | NEW |