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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 | 103 |
104 if (module->import_table.size() > 0) { | 104 if (module->import_table.size() > 0) { |
105 thrower.Error("Not supported: module has imports."); | 105 thrower.Error("Not supported: module has imports."); |
106 } | 106 } |
107 if (module->export_table.size() == 0) { | 107 if (module->export_table.size() == 0) { |
108 thrower.Error("Not supported: module has no exports."); | 108 thrower.Error("Not supported: module has no exports."); |
109 } | 109 } |
110 | 110 |
111 if (thrower.error()) return -1; | 111 if (thrower.error()) return -1; |
112 | 112 |
| 113 ModuleEnv module_env; |
| 114 module_env.module = module; |
| 115 module_env.origin = module->origin; |
| 116 |
| 117 for (size_t i = 0; i < module->functions.size(); i++) { |
| 118 FunctionBody body = { |
| 119 &module_env, module->functions[i].sig, module->module_start, |
| 120 module->module_start + module->functions[i].code_start_offset, |
| 121 module->module_start + module->functions[i].code_end_offset}; |
| 122 DecodeResult result = VerifyWasmCode(isolate->allocator(), body); |
| 123 if (result.failed()) { |
| 124 thrower.Error("Function did not verify"); |
| 125 return -1; |
| 126 } |
| 127 } |
| 128 |
| 129 // The code verifies, we create an instance to run it in the interpreter. |
113 WasmModuleInstance instance(module); | 130 WasmModuleInstance instance(module); |
114 instance.context = isolate->native_context(); | 131 instance.context = isolate->native_context(); |
115 instance.mem_size = GetMinModuleMemSize(module); | 132 instance.mem_size = GetMinModuleMemSize(module); |
116 instance.mem_start = nullptr; | 133 // TODO(ahaas): Move memory allocation to wasm-module.cc for better |
| 134 // encapsulation. |
| 135 instance.mem_start = |
| 136 static_cast<byte*>(calloc(GetMinModuleMemSize(module), 1)); |
117 instance.globals_start = nullptr; | 137 instance.globals_start = nullptr; |
118 | |
119 ModuleEnv module_env; | |
120 module_env.module = module; | |
121 module_env.instance = &instance; | 138 module_env.instance = &instance; |
122 module_env.origin = module->origin; | |
123 | |
124 const WasmFunction* function = &(module->functions[function_index]); | |
125 | |
126 FunctionBody body = {&module_env, function->sig, module->module_start, | |
127 module->module_start + function->code_start_offset, | |
128 module->module_start + function->code_end_offset}; | |
129 DecodeResult result = VerifyWasmCode(isolate->allocator(), body); | |
130 if (result.failed()) { | |
131 thrower.Error("Function did not verify"); | |
132 return -1; | |
133 } | |
134 | 139 |
135 WasmInterpreter interpreter(&instance, isolate->allocator()); | 140 WasmInterpreter interpreter(&instance, isolate->allocator()); |
136 | 141 |
137 WasmInterpreter::Thread* thread = interpreter.GetThread(0); | 142 WasmInterpreter::Thread* thread = interpreter.GetThread(0); |
138 thread->Reset(); | 143 thread->Reset(); |
139 thread->PushFrame(function, args); | 144 thread->PushFrame(&(module->functions[function_index]), args); |
140 if (thread->Run() == WasmInterpreter::FINISHED) { | 145 WasmInterpreter::State interpreter_result = thread->Run(); |
| 146 if (instance.mem_start) { |
| 147 free(instance.mem_start); |
| 148 } |
| 149 if (interpreter_result == WasmInterpreter::FINISHED) { |
141 WasmVal val = thread->GetReturnValue(); | 150 WasmVal val = thread->GetReturnValue(); |
142 return val.to<int32_t>(); | 151 return val.to<int32_t>(); |
143 } else if (thread->state() == WasmInterpreter::TRAPPED) { | 152 } else if (thread->state() == WasmInterpreter::TRAPPED) { |
144 return 0xdeadbeef; | 153 return 0xdeadbeef; |
145 } else { | 154 } else { |
146 thrower.Error("Interpreter did not finish execution within its step bound"); | 155 thrower.Error("Interpreter did not finish execution within its step bound"); |
147 return -1; | 156 return -1; |
148 } | 157 } |
149 } | 158 } |
150 | 159 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 return static_cast<int32_t>(HeapNumber::cast(*result)->value()); | 195 return static_cast<int32_t>(HeapNumber::cast(*result)->value()); |
187 } | 196 } |
188 thrower.Error("WASM.compileRun() failed: Return value should be number"); | 197 thrower.Error("WASM.compileRun() failed: Return value should be number"); |
189 return -1; | 198 return -1; |
190 } | 199 } |
191 | 200 |
192 } // namespace testing | 201 } // namespace testing |
193 } // namespace wasm | 202 } // namespace wasm |
194 } // namespace internal | 203 } // namespace internal |
195 } // namespace v8 | 204 } // namespace v8 |
OLD | NEW |