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

Side by Side Diff: src/wasm/wasm-module.cc

Issue 1504713014: Initial import of v8-native WASM. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years 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 | « src/wasm/wasm-module.h ('k') | src/wasm/wasm-opcodes.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 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 "src/macro-assembler.h"
6 #include "src/objects.h"
7 #include "src/v8.h"
8
9 #include "src/simulator.h"
10
11 #include "src/wasm/ast-decoder.h"
12 #include "src/wasm/module-decoder.h"
13 #include "src/wasm/wasm-module.h"
14 #include "src/wasm/wasm-result.h"
15
16 #include "src/compiler/wasm-compiler.h"
17
18 namespace v8 {
19 namespace internal {
20 namespace wasm {
21
22 std::ostream& operator<<(std::ostream& os, const WasmModule& module) {
23 os << "WASM module with ";
24 os << (1 << module.min_mem_size_log2) << " min mem";
25 os << (1 << module.max_mem_size_log2) << " max mem";
26 if (module.functions) os << module.functions->size() << " functions";
27 if (module.globals) os << module.functions->size() << " globals";
28 if (module.data_segments) os << module.functions->size() << " data segments";
29 return os;
30 }
31
32
33 std::ostream& operator<<(std::ostream& os, const WasmFunction& function) {
34 os << "WASM function with signature ";
35
36 // TODO(titzer): factor out rendering of signatures.
37 if (function.sig->return_count() == 0) os << "v";
38 for (size_t i = 0; i < function.sig->return_count(); i++) {
39 os << WasmOpcodes::ShortNameOf(function.sig->GetReturn(i));
40 }
41 os << "_";
42 if (function.sig->parameter_count() == 0) os << "v";
43 for (size_t i = 0; i < function.sig->parameter_count(); i++) {
44 os << WasmOpcodes::ShortNameOf(function.sig->GetParam(i));
45 }
46 os << " locals: ";
47 if (function.local_int32_count)
48 os << function.local_int32_count << " int32s ";
49 if (function.local_int64_count)
50 os << function.local_int64_count << " int64s ";
51 if (function.local_float32_count)
52 os << function.local_float32_count << " float32s ";
53 if (function.local_float64_count)
54 os << function.local_float64_count << " float64s ";
55
56 os << " code bytes: "
57 << (function.code_end_offset - function.code_start_offset);
58 return os;
59 }
60
61
62 // A helper class for compiling multiple wasm functions that offers
63 // placeholder code objects for calling functions that are not yet compiled.
64 class WasmLinker {
65 public:
66 WasmLinker(Isolate* isolate, size_t size)
67 : isolate_(isolate), placeholder_code_(size), function_code_(size) {}
68
69 // Get the code object for a function, allocating a placeholder if it has
70 // not yet been compiled.
71 Handle<Code> GetFunctionCode(uint32_t index) {
72 DCHECK(index < function_code_.size());
73 if (function_code_[index].is_null()) {
74 // Create a placeholder code object and encode the corresponding index in
75 // the {constant_pool_offset} field of the code object.
76 // TODO(titzer): placeholder code objects are somewhat dangerous.
77 Handle<Code> self(nullptr, isolate_);
78 byte buffer[] = {0, 0, 0, 0, 0, 0, 0, 0}; // fake instructions.
79 CodeDesc desc = {buffer, 8, 8, 0, 0, nullptr};
80 Handle<Code> code = isolate_->factory()->NewCode(
81 desc, Code::KindField::encode(Code::WASM_FUNCTION), self);
82 code->set_constant_pool_offset(index + kPlaceholderMarker);
83 placeholder_code_[index] = code;
84 function_code_[index] = code;
85 }
86 return function_code_[index];
87 }
88
89 void Finish(uint32_t index, Handle<Code> code) {
90 DCHECK(index < function_code_.size());
91 function_code_[index] = code;
92 }
93
94 void Link(Handle<FixedArray> function_table,
95 std::vector<uint16_t>* functions) {
96 for (size_t i = 0; i < function_code_.size(); i++) {
97 LinkFunction(function_code_[i]);
98 }
99 if (functions && !function_table.is_null()) {
100 int table_size = static_cast<int>(functions->size());
101 DCHECK_EQ(function_table->length(), table_size * 2);
102 for (int i = 0; i < table_size; i++) {
103 function_table->set(i + table_size, *function_code_[functions->at(i)]);
104 }
105 }
106 }
107
108 private:
109 static const int kPlaceholderMarker = 1000000000;
110
111 Isolate* isolate_;
112 std::vector<Handle<Code>> placeholder_code_;
113 std::vector<Handle<Code>> function_code_;
114
115 void LinkFunction(Handle<Code> code) {
116 bool modified = false;
117 int mode_mask = RelocInfo::kCodeTargetMask;
118 AllowDeferredHandleDereference embedding_raw_address;
119 for (RelocIterator it(*code, mode_mask); !it.done(); it.next()) {
120 RelocInfo::Mode mode = it.rinfo()->rmode();
121 if (RelocInfo::IsCodeTarget(mode)) {
122 Code* target =
123 Code::GetCodeFromTargetAddress(it.rinfo()->target_address());
124 if (target->kind() == Code::WASM_FUNCTION &&
125 target->constant_pool_offset() >= kPlaceholderMarker) {
126 // Patch direct calls to placeholder code objects.
127 uint32_t index = target->constant_pool_offset() - kPlaceholderMarker;
128 CHECK(index < function_code_.size());
129 Handle<Code> new_target = function_code_[index];
130 if (target != *new_target) {
131 CHECK_EQ(*placeholder_code_[index], target);
132 it.rinfo()->set_target_address(new_target->instruction_start(),
133 SKIP_WRITE_BARRIER,
134 SKIP_ICACHE_FLUSH);
135 modified = true;
136 }
137 }
138 }
139 }
140 if (modified) {
141 Assembler::FlushICache(isolate_, code->instruction_start(),
142 code->instruction_size());
143 }
144 }
145 };
146
147 namespace {
148 // Internal constants for the layout of the module object.
149 const int kWasmModuleInternalFieldCount = 4;
150 const int kWasmModuleFunctionTable = 0;
151 const int kWasmModuleCodeTable = 1;
152 const int kWasmMemArrayBuffer = 2;
153 const int kWasmGlobalsArrayBuffer = 3;
154
155
156 size_t AllocateGlobalsOffsets(std::vector<WasmGlobal>* globals) {
157 uint32_t offset = 0;
158 if (!globals) return 0;
159 for (WasmGlobal& global : *globals) {
160 byte size = WasmOpcodes::MemSize(global.type);
161 offset = (offset + size - 1) & ~(size - 1); // align
162 global.offset = offset;
163 offset += size;
164 }
165 return offset;
166 }
167
168
169 void LoadDataSegments(WasmModule* module, byte* mem_addr, size_t mem_size) {
170 for (const WasmDataSegment& segment : *module->data_segments) {
171 if (!segment.init) continue;
172 CHECK_LT(segment.dest_addr, mem_size);
173 CHECK_LE(segment.source_size, mem_size);
174 CHECK_LE(segment.dest_addr + segment.source_size, mem_size);
175 byte* addr = mem_addr + segment.dest_addr;
176 memcpy(addr, module->module_start + segment.source_offset,
177 segment.source_size);
178 }
179 }
180
181
182 Handle<FixedArray> BuildFunctionTable(Isolate* isolate, WasmModule* module) {
183 if (!module->function_table || module->function_table->size() == 0) {
184 return Handle<FixedArray>::null();
185 }
186 int table_size = static_cast<int>(module->function_table->size());
187 Handle<FixedArray> fixed = isolate->factory()->NewFixedArray(2 * table_size);
188 for (int i = 0; i < table_size; i++) {
189 WasmFunction* function =
190 &module->functions->at(module->function_table->at(i));
191 fixed->set(i, Smi::FromInt(function->sig_index));
192 }
193 return fixed;
194 }
195
196
197 Handle<JSArrayBuffer> NewArrayBuffer(Isolate* isolate, int size,
198 byte** backing_store) {
199 void* memory = isolate->array_buffer_allocator()->Allocate(size);
200 if (!memory) return Handle<JSArrayBuffer>::null();
201 *backing_store = reinterpret_cast<byte*>(memory);
202
203 #if DEBUG
204 // Double check the API allocator actually zero-initialized the memory.
205 for (uint32_t i = 0; i < size; i++) {
206 DCHECK_EQ(0, (*backing_store)[i]);
207 }
208 #endif
209
210 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer();
211 JSArrayBuffer::Setup(buffer, isolate, true, memory, size);
212 buffer->set_is_neuterable(false);
213 return buffer;
214 }
215 } // namespace
216
217
218 // Instantiates a wasm module as a JSObject.
219 // * allocates a backing store of {mem_size} bytes.
220 // * installs a named property "memory" for that buffer if exported
221 // * installs named properties on the object for exported functions
222 // * compiles wasm code to machine code
223 MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate,
224 Handle<JSObject> ffi,
225 Handle<JSArrayBuffer> memory) {
226 this->shared_isolate = isolate; // TODO(titzer): have a real shared isolate.
227 ErrorThrower thrower(isolate, "WasmModule::Instantiate()");
228
229 Factory* factory = isolate->factory();
230 // Memory is bigger than maximum supported size.
231 if (memory.is_null() && min_mem_size_log2 > kMaxMemSize) {
232 thrower.Error("Out of memory: wasm memory too large");
233 return MaybeHandle<JSObject>();
234 }
235
236 Handle<Map> map = factory->NewMap(
237 JS_OBJECT_TYPE,
238 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize);
239
240 //-------------------------------------------------------------------------
241 // Allocate the module object.
242 //-------------------------------------------------------------------------
243 Handle<JSObject> module = factory->NewJSObjectFromMap(map, TENURED);
244 Handle<FixedArray> code_table =
245 factory->NewFixedArray(static_cast<int>(functions->size()), TENURED);
246
247 //-------------------------------------------------------------------------
248 // Allocate the linear memory.
249 //-------------------------------------------------------------------------
250 uint32_t mem_size = 1 << min_mem_size_log2;
251 byte* mem_addr = nullptr;
252 Handle<JSArrayBuffer> mem_buffer;
253 if (!memory.is_null()) {
254 memory->set_is_neuterable(false);
255 mem_addr = reinterpret_cast<byte*>(memory->backing_store());
256 mem_size = memory->byte_length()->Number();
257 mem_buffer = memory;
258 } else {
259 mem_buffer = NewArrayBuffer(isolate, mem_size, &mem_addr);
260 if (!mem_addr) {
261 // Not enough space for backing store of memory
262 thrower.Error("Out of memory: wasm memory");
263 return MaybeHandle<JSObject>();
264 }
265 }
266
267 // Load initialized data segments.
268 LoadDataSegments(this, mem_addr, mem_size);
269
270 module->SetInternalField(kWasmMemArrayBuffer, *mem_buffer);
271
272 if (mem_export) {
273 // Export the memory as a named property.
274 Handle<String> name = factory->InternalizeUtf8String("memory");
275 JSObject::AddProperty(module, name, mem_buffer, READ_ONLY);
276 }
277
278 //-------------------------------------------------------------------------
279 // Allocate the globals area if necessary.
280 //-------------------------------------------------------------------------
281 size_t globals_size = AllocateGlobalsOffsets(globals);
282 byte* globals_addr = nullptr;
283 if (globals_size > 0) {
284 Handle<JSArrayBuffer> globals_buffer =
285 NewArrayBuffer(isolate, mem_size, &globals_addr);
286 if (!globals_addr) {
287 // Not enough space for backing store of globals.
288 thrower.Error("Out of memory: wasm globals");
289 return MaybeHandle<JSObject>();
290 }
291
292 module->SetInternalField(kWasmGlobalsArrayBuffer, *globals_buffer);
293 } else {
294 module->SetInternalField(kWasmGlobalsArrayBuffer, Smi::FromInt(0));
295 }
296
297 //-------------------------------------------------------------------------
298 // Compile all functions in the module.
299 //-------------------------------------------------------------------------
300 int index = 0;
301 WasmLinker linker(isolate, functions->size());
302 ModuleEnv module_env;
303 module_env.module = this;
304 module_env.mem_start = reinterpret_cast<uintptr_t>(mem_addr);
305 module_env.mem_end = reinterpret_cast<uintptr_t>(mem_addr) + mem_size;
306 module_env.globals_area = reinterpret_cast<uintptr_t>(globals_addr);
307 module_env.linker = &linker;
308 module_env.function_code = nullptr;
309 module_env.function_table = BuildFunctionTable(isolate, this);
310 module_env.memory = memory;
311 module_env.context = isolate->native_context();
312 module_env.asm_js = false;
313
314 // First pass: compile each function and initialize the code table.
315 for (const WasmFunction& func : *functions) {
316 if (thrower.error()) break;
317
318 const char* cstr = GetName(func.name_offset);
319 Handle<String> name = factory->InternalizeUtf8String(cstr);
320 Handle<Code> code = Handle<Code>::null();
321 Handle<JSFunction> function = Handle<JSFunction>::null();
322 if (func.external) {
323 // Lookup external function in FFI object.
324 if (!ffi.is_null()) {
325 MaybeHandle<Object> result = Object::GetProperty(ffi, name);
326 if (!result.is_null()) {
327 Handle<Object> obj = result.ToHandleChecked();
328 if (obj->IsJSFunction()) {
329 function = Handle<JSFunction>::cast(obj);
330 code = compiler::CompileWasmToJSWrapper(isolate, &module_env,
331 function, index);
332 } else {
333 thrower.Error("FFI function #%d:%s is not a JSFunction.", index,
334 cstr);
335 return MaybeHandle<JSObject>();
336 }
337 } else {
338 thrower.Error("FFI function #%d:%s not found.", index, cstr);
339 return MaybeHandle<JSObject>();
340 }
341 } else {
342 thrower.Error("FFI table is not an object.");
343 return MaybeHandle<JSObject>();
344 }
345 } else {
346 // Compile the function.
347 code = compiler::CompileWasmFunction(thrower, isolate, &module_env, func,
348 index);
349 if (code.is_null()) {
350 thrower.Error("Compilation of #%d:%s failed.", index, cstr);
351 return MaybeHandle<JSObject>();
352 }
353 if (func.exported) {
354 function = compiler::CompileJSToWasmWrapper(isolate, &module_env, name,
355 code, index);
356 }
357 }
358 if (!code.is_null()) {
359 // Install the code into the linker table.
360 linker.Finish(index, code);
361 code_table->set(index, *code);
362 }
363 if (func.exported) {
364 // Exported functions are installed as read-only properties on the module.
365 JSObject::AddProperty(module, name, function, READ_ONLY);
366 }
367 index++;
368 }
369
370 // Second pass: patch all direct call sites.
371 linker.Link(module_env.function_table, this->function_table);
372
373 module->SetInternalField(kWasmModuleFunctionTable, Smi::FromInt(0));
374 module->SetInternalField(kWasmModuleCodeTable, *code_table);
375 return module;
376 }
377
378
379 Handle<Code> ModuleEnv::GetFunctionCode(uint32_t index) {
380 DCHECK(IsValidFunction(index));
381 if (linker) return linker->GetFunctionCode(index);
382 if (function_code) return function_code->at(index);
383 return Handle<Code>::null();
384 }
385
386
387 compiler::CallDescriptor* ModuleEnv::GetCallDescriptor(Zone* zone,
388 uint32_t index) {
389 DCHECK(IsValidFunction(index));
390 // Always make a direct call to whatever is in the table at that location.
391 // A wrapper will be generated for FFI calls.
392 WasmFunction* function = &module->functions->at(index);
393 return GetWasmCallDescriptor(zone, function->sig);
394 }
395
396
397 int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start,
398 const byte* module_end, bool asm_js) {
399 HandleScope scope(isolate);
400 Zone zone;
401 // Decode the module, but don't verify function bodies, since we'll
402 // be compiling them anyway.
403 ModuleResult result =
404 DecodeWasmModule(isolate, &zone, module_start, module_end, false, false);
405 if (result.failed()) {
406 // Module verification failed. throw.
407 std::ostringstream str;
408 str << "WASM.compileRun() failed: " << result;
409 isolate->Throw(
410 *isolate->factory()->NewStringFromAsciiChecked(str.str().c_str()));
411 return -1;
412 }
413
414 int32_t retval = CompileAndRunWasmModule(isolate, result.val);
415 delete result.val;
416 return retval;
417 }
418
419
420 int32_t CompileAndRunWasmModule(Isolate* isolate, WasmModule* module) {
421 ErrorThrower thrower(isolate, "CompileAndRunWasmModule");
422
423 // Allocate temporary linear memory and globals.
424 size_t mem_size = 1 << module->min_mem_size_log2;
425 size_t globals_size = AllocateGlobalsOffsets(module->globals);
426
427 base::SmartArrayPointer<byte> mem_addr(new byte[mem_size]);
428 base::SmartArrayPointer<byte> globals_addr(new byte[globals_size]);
429
430 memset(mem_addr.get(), 0, mem_size);
431 memset(globals_addr.get(), 0, globals_size);
432
433 // Create module environment.
434 WasmLinker linker(isolate, module->functions->size());
435 ModuleEnv module_env;
436 module_env.module = module;
437 module_env.mem_start = reinterpret_cast<uintptr_t>(mem_addr.get());
438 module_env.mem_end = reinterpret_cast<uintptr_t>(mem_addr.get()) + mem_size;
439 module_env.globals_area = reinterpret_cast<uintptr_t>(globals_addr.get());
440 module_env.linker = &linker;
441 module_env.function_code = nullptr;
442 module_env.function_table = BuildFunctionTable(isolate, module);
443 module_env.asm_js = false;
444
445 // Load data segments.
446 // TODO(titzer): throw instead of crashing if segments don't fit in memory?
447 LoadDataSegments(module, mem_addr.get(), mem_size);
448
449 // Compile all functions.
450 Handle<Code> main_code = Handle<Code>::null(); // record last code.
451 int index = 0;
452 for (const WasmFunction& func : *module->functions) {
453 if (!func.external) {
454 // Compile the function and install it in the code table.
455 Handle<Code> code = compiler::CompileWasmFunction(
456 thrower, isolate, &module_env, func, index);
457 if (!code.is_null()) {
458 if (func.exported) main_code = code;
459 linker.Finish(index, code);
460 }
461 if (thrower.error()) return -1;
462 }
463 index++;
464 }
465
466 if (!main_code.is_null()) {
467 linker.Link(module_env.function_table, module->function_table);
468 #if USE_SIMULATOR && V8_TARGET_ARCH_ARM64
469 // Run the main code on arm64 simulator.
470 Simulator* simulator = Simulator::current(isolate);
471 Simulator::CallArgument args[] = {Simulator::CallArgument(0),
472 Simulator::CallArgument::End()};
473 return static_cast<int32_t>(simulator->CallInt64(main_code->entry(), args));
474 #elif USE_SIMULATOR
475 // Run the main code on simulator.
476 Simulator* simulator = Simulator::current(isolate);
477 return static_cast<int32_t>(
478 simulator->Call(main_code->entry(), 4, 0, 0, 0, 0));
479 #else
480 // Run the main code as raw machine code.
481 int32_t (*raw_func)() = reinterpret_cast<int (*)()>(main_code->entry());
482 return raw_func();
483 #endif
484 } else {
485 // No main code was found.
486 isolate->Throw(*isolate->factory()->NewStringFromStaticChars(
487 "WASM.compileRun() failed: no valid main code produced."));
488 }
489 return -1;
490 }
491 } // namespace wasm
492 } // namespace internal
493 } // namespace v8
OLDNEW
« no previous file with comments | « src/wasm/wasm-module.h ('k') | src/wasm/wasm-opcodes.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698