Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: test/common/wasm/wasm-module-runner.cc

Issue 2496203002: [wasm] Reimplement function verification in the module decoder. (Closed)
Patch Set: Create a dummy ModuleEnv Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/common/wasm/wasm-module-runner.h ('k') | test/fuzzer/wasm-call.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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-objects.h" 15 #include "src/wasm/wasm-objects.h"
16 #include "src/wasm/wasm-result.h" 16 #include "src/wasm/wasm-result.h"
17 17
18 namespace v8 { 18 namespace v8 {
19 namespace internal { 19 namespace internal {
20 namespace wasm { 20 namespace wasm {
21 namespace testing { 21 namespace testing {
22 22
23 uint32_t GetMinModuleMemSize(const WasmModule* module) { 23 uint32_t GetMinModuleMemSize(const WasmModule* module) {
24 return WasmModule::kPageSize * module->min_mem_pages; 24 return WasmModule::kPageSize * module->min_mem_pages;
25 } 25 }
26 26
27 const WasmModule* DecodeWasmModuleForTesting(Isolate* isolate, 27 const WasmModule* DecodeWasmModuleForTesting(
28 ErrorThrower* thrower, 28 Isolate* isolate, ErrorThrower* thrower, const byte* module_start,
29 const byte* module_start, 29 const byte* module_end, ModuleOrigin origin, bool verify_functions) {
30 const byte* module_end,
31 ModuleOrigin origin) {
32 // Decode the module, but don't verify function bodies, since we'll 30 // Decode the module, but don't verify function bodies, since we'll
33 // be compiling them anyway. 31 // be compiling them anyway.
34 ModuleResult decoding_result = 32 ModuleResult decoding_result = DecodeWasmModule(
35 DecodeWasmModule(isolate, module_start, module_end, false, origin); 33 isolate, module_start, module_end, verify_functions, origin);
36 34
37 if (decoding_result.failed()) { 35 if (decoding_result.failed()) {
38 // Module verification failed. throw. 36 // Module verification failed. throw.
39 thrower->CompileError("WASM.compileRun() failed: %s", 37 thrower->CompileError("WASM.compileRun() failed: %s",
40 decoding_result.error_msg.get()); 38 decoding_result.error_msg.get());
41 } 39 }
42 40
43 if (thrower->error()) { 41 if (thrower->error()) {
44 if (decoding_result.val) delete decoding_result.val; 42 if (decoding_result.val) delete decoding_result.val;
45 return nullptr; 43 return nullptr;
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 120
123 if (module->import_table.size() > 0) { 121 if (module->import_table.size() > 0) {
124 thrower->CompileError("Not supported: module has imports."); 122 thrower->CompileError("Not supported: module has imports.");
125 } 123 }
126 if (module->export_table.size() == 0) { 124 if (module->export_table.size() == 0) {
127 thrower->CompileError("Not supported: module has no exports."); 125 thrower->CompileError("Not supported: module has no exports.");
128 } 126 }
129 127
130 if (thrower->error()) return -1; 128 if (thrower->error()) return -1;
131 129
132 ModuleEnv module_env;
133 module_env.module = module;
134 module_env.origin = module->origin;
135
136 for (size_t i = 0; i < module->functions.size(); i++) {
137 FunctionBody body = {
138 &module_env, module->functions[i].sig, module->module_start,
139 module->module_start + module->functions[i].code_start_offset,
140 module->module_start + module->functions[i].code_end_offset};
141 DecodeResult result = VerifyWasmCode(isolate->allocator(), body);
142 if (result.failed()) {
143 thrower->CompileError("Function did not verify");
144 return -1;
145 }
146 }
147
148 // The code verifies, we create an instance to run it in the interpreter. 130 // The code verifies, we create an instance to run it in the interpreter.
149 WasmInstance instance(module); 131 WasmInstance instance(module);
150 instance.context = isolate->native_context(); 132 instance.context = isolate->native_context();
151 instance.mem_size = GetMinModuleMemSize(module); 133 instance.mem_size = GetMinModuleMemSize(module);
152 // TODO(ahaas): Move memory allocation to wasm-module.cc for better 134 // TODO(ahaas): Move memory allocation to wasm-module.cc for better
153 // encapsulation. 135 // encapsulation.
154 instance.mem_start = 136 instance.mem_start =
155 static_cast<byte*>(calloc(GetMinModuleMemSize(module), 1)); 137 static_cast<byte*>(calloc(GetMinModuleMemSize(module), 1));
156 instance.globals_start = nullptr; 138 instance.globals_start = nullptr;
157 module_env.instance = &instance;
158 139
159 WasmInterpreter interpreter(&instance, isolate->allocator()); 140 WasmInterpreter interpreter(&instance, isolate->allocator());
160 141
161 WasmInterpreter::Thread* thread = interpreter.GetThread(0); 142 WasmInterpreter::Thread* thread = interpreter.GetThread(0);
162 thread->Reset(); 143 thread->Reset();
163 thread->PushFrame(&(module->functions[function_index]), args); 144 thread->PushFrame(&(module->functions[function_index]), args);
164 WasmInterpreter::State interpreter_result = thread->Run(); 145 WasmInterpreter::State interpreter_result = thread->Run();
165 if (instance.mem_start) { 146 if (instance.mem_start) {
166 free(instance.mem_start); 147 free(instance.mem_start);
167 } 148 }
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 203
223 void SetupIsolateForWasmModule(Isolate* isolate) { 204 void SetupIsolateForWasmModule(Isolate* isolate) {
224 WasmJs::InstallWasmMapsIfNeeded(isolate, isolate->native_context()); 205 WasmJs::InstallWasmMapsIfNeeded(isolate, isolate->native_context());
225 WasmJs::InstallWasmModuleSymbolIfNeeded(isolate, isolate->global_object(), 206 WasmJs::InstallWasmModuleSymbolIfNeeded(isolate, isolate->global_object(),
226 isolate->native_context()); 207 isolate->native_context());
227 } 208 }
228 } // namespace testing 209 } // namespace testing
229 } // namespace wasm 210 } // namespace wasm
230 } // namespace internal 211 } // namespace internal
231 } // namespace v8 212 } // namespace v8
OLDNEW
« no previous file with comments | « test/common/wasm/wasm-module-runner.h ('k') | test/fuzzer/wasm-call.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698