OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 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 "src/base/atomic-utils.h" | 5 #include "src/base/atomic-utils.h" |
6 #include "src/macro-assembler.h" | 6 #include "src/macro-assembler.h" |
7 #include "src/objects.h" | 7 #include "src/objects.h" |
8 #include "src/property-descriptor.h" | 8 #include "src/property-descriptor.h" |
9 #include "src/v8.h" | 9 #include "src/v8.h" |
10 | 10 |
11 #include "src/simulator.h" | 11 #include "src/simulator.h" |
12 | 12 |
13 #include "src/wasm/ast-decoder.h" | 13 #include "src/wasm/ast-decoder.h" |
14 #include "src/wasm/module-decoder.h" | 14 #include "src/wasm/module-decoder.h" |
| 15 #include "src/wasm/wasm-debug.h" |
15 #include "src/wasm/wasm-function-name-table.h" | 16 #include "src/wasm/wasm-function-name-table.h" |
16 #include "src/wasm/wasm-module.h" | 17 #include "src/wasm/wasm-module.h" |
17 #include "src/wasm/wasm-result.h" | 18 #include "src/wasm/wasm-result.h" |
18 | 19 |
19 #include "src/compiler/wasm-compiler.h" | 20 #include "src/compiler/wasm-compiler.h" |
20 | 21 |
21 namespace v8 { | 22 namespace v8 { |
22 namespace internal { | 23 namespace internal { |
23 namespace wasm { | 24 namespace wasm { |
24 | 25 |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 } | 199 } |
199 if (modified) { | 200 if (modified) { |
200 Assembler::FlushICache(isolate_, code->instruction_start(), | 201 Assembler::FlushICache(isolate_, code->instruction_start(), |
201 code->instruction_size()); | 202 code->instruction_size()); |
202 } | 203 } |
203 } | 204 } |
204 }; | 205 }; |
205 | 206 |
206 namespace { | 207 namespace { |
207 // Internal constants for the layout of the module object. | 208 // Internal constants for the layout of the module object. |
208 const int kWasmModuleInternalFieldCount = 5; | |
209 const int kWasmModuleFunctionTable = 0; | 209 const int kWasmModuleFunctionTable = 0; |
210 const int kWasmModuleCodeTable = 1; | 210 const int kWasmModuleCodeTable = 1; |
211 const int kWasmMemArrayBuffer = 2; | 211 const int kWasmMemArrayBuffer = 2; |
212 const int kWasmGlobalsArrayBuffer = 3; | 212 const int kWasmGlobalsArrayBuffer = 3; |
| 213 // TODO(clemensh): Remove function name array, extract names from module bytes. |
213 const int kWasmFunctionNamesArray = 4; | 214 const int kWasmFunctionNamesArray = 4; |
| 215 const int kWasmModuleBytesString = 5; |
| 216 const int kWasmDebugInfo = 6; |
| 217 const int kWasmModuleInternalFieldCount = 7; |
214 | 218 |
215 void LoadDataSegments(const WasmModule* module, byte* mem_addr, | 219 void LoadDataSegments(const WasmModule* module, byte* mem_addr, |
216 size_t mem_size) { | 220 size_t mem_size) { |
217 for (const WasmDataSegment& segment : module->data_segments) { | 221 for (const WasmDataSegment& segment : module->data_segments) { |
218 if (!segment.init) continue; | 222 if (!segment.init) continue; |
219 if (!segment.source_size) continue; | 223 if (!segment.source_size) continue; |
220 CHECK_LT(segment.dest_addr, mem_size); | 224 CHECK_LT(segment.dest_addr, mem_size); |
221 CHECK_LE(segment.source_size, mem_size); | 225 CHECK_LE(segment.source_size, mem_size); |
222 CHECK_LE(segment.dest_addr + segment.source_size, mem_size); | 226 CHECK_LE(segment.dest_addr + segment.source_size, mem_size); |
223 byte* addr = mem_addr + segment.dest_addr; | 227 byte* addr = mem_addr + segment.dest_addr; |
(...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
694 //------------------------------------------------------------------------- | 698 //------------------------------------------------------------------------- |
695 Handle<Map> map = factory->NewMap( | 699 Handle<Map> map = factory->NewMap( |
696 JS_OBJECT_TYPE, | 700 JS_OBJECT_TYPE, |
697 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize); | 701 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize); |
698 WasmModuleInstance instance(this); | 702 WasmModuleInstance instance(this); |
699 instance.context = isolate->native_context(); | 703 instance.context = isolate->native_context(); |
700 instance.js_object = factory->NewJSObjectFromMap(map, TENURED); | 704 instance.js_object = factory->NewJSObjectFromMap(map, TENURED); |
701 Handle<FixedArray> code_table = | 705 Handle<FixedArray> code_table = |
702 factory->NewFixedArray(static_cast<int>(functions.size()), TENURED); | 706 factory->NewFixedArray(static_cast<int>(functions.size()), TENURED); |
703 instance.js_object->SetInternalField(kWasmModuleCodeTable, *code_table); | 707 instance.js_object->SetInternalField(kWasmModuleCodeTable, *code_table); |
| 708 size_t module_bytes_len = |
| 709 instance.module->module_end - instance.module->module_start; |
| 710 DCHECK_LE(module_bytes_len, static_cast<size_t>(kMaxInt)); |
| 711 Vector<const uint8_t> module_bytes_vec(instance.module->module_start, |
| 712 static_cast<int>(module_bytes_len)); |
| 713 Handle<String> module_bytes_string = |
| 714 factory->NewStringFromOneByte(module_bytes_vec, TENURED) |
| 715 .ToHandleChecked(); |
| 716 instance.js_object->SetInternalField(kWasmModuleBytesString, |
| 717 *module_bytes_string); |
704 | 718 |
705 //------------------------------------------------------------------------- | 719 //------------------------------------------------------------------------- |
706 // Allocate and initialize the linear memory. | 720 // Allocate and initialize the linear memory. |
707 //------------------------------------------------------------------------- | 721 //------------------------------------------------------------------------- |
708 isolate->counters()->wasm_min_mem_pages_count()->AddSample( | 722 isolate->counters()->wasm_min_mem_pages_count()->AddSample( |
709 instance.module->min_mem_pages); | 723 instance.module->min_mem_pages); |
710 isolate->counters()->wasm_max_mem_pages_count()->AddSample( | 724 isolate->counters()->wasm_max_mem_pages_count()->AddSample( |
711 instance.module->max_mem_pages); | 725 instance.module->max_mem_pages); |
712 if (memory.is_null()) { | 726 if (memory.is_null()) { |
713 if (!AllocateMemory(&thrower, isolate, &instance)) { | 727 if (!AllocateMemory(&thrower, isolate, &instance)) { |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1003 Handle<String> GetWasmFunctionName(Isolate* isolate, Handle<Object> wasm, | 1017 Handle<String> GetWasmFunctionName(Isolate* isolate, Handle<Object> wasm, |
1004 uint32_t func_index) { | 1018 uint32_t func_index) { |
1005 Handle<Object> name_or_null = | 1019 Handle<Object> name_or_null = |
1006 GetWasmFunctionNameOrNull(isolate, wasm, func_index); | 1020 GetWasmFunctionNameOrNull(isolate, wasm, func_index); |
1007 if (!name_or_null->IsNull(isolate)) { | 1021 if (!name_or_null->IsNull(isolate)) { |
1008 return Handle<String>::cast(name_or_null); | 1022 return Handle<String>::cast(name_or_null); |
1009 } | 1023 } |
1010 return isolate->factory()->NewStringFromStaticChars("<WASM UNNAMED>"); | 1024 return isolate->factory()->NewStringFromStaticChars("<WASM UNNAMED>"); |
1011 } | 1025 } |
1012 | 1026 |
1013 bool IsWasmObject(Handle<JSObject> object) { | 1027 bool IsWasmObject(Object* object) { |
1014 // TODO(clemensh): Check wasm byte header once we store a copy of the bytes. | 1028 if (!object->IsJSObject()) return false; |
1015 return object->GetInternalFieldCount() == kWasmModuleInternalFieldCount && | 1029 JSObject* obj = JSObject::cast(object); |
1016 object->GetInternalField(kWasmModuleCodeTable)->IsFixedArray() && | 1030 if (obj->GetInternalFieldCount() != kWasmModuleInternalFieldCount || |
1017 object->GetInternalField(kWasmMemArrayBuffer)->IsJSArrayBuffer() && | 1031 !obj->GetInternalField(kWasmModuleCodeTable)->IsFixedArray() || |
1018 object->GetInternalField(kWasmFunctionNamesArray)->IsByteArray(); | 1032 !obj->GetInternalField(kWasmMemArrayBuffer)->IsJSArrayBuffer() || |
| 1033 !obj->GetInternalField(kWasmFunctionNamesArray)->IsByteArray() || |
| 1034 !obj->GetInternalField(kWasmModuleBytesString)->IsSeqOneByteString()) { |
| 1035 return false; |
| 1036 } |
| 1037 DisallowHeapAllocation no_gc; |
| 1038 SeqOneByteString* bytes = |
| 1039 SeqOneByteString::cast(obj->GetInternalField(kWasmModuleBytesString)); |
| 1040 if (bytes->length() < 4) return false; |
| 1041 if (memcmp(bytes->GetChars(), "\0asm", 4)) return false; |
| 1042 |
| 1043 // All checks passed. |
| 1044 return true; |
| 1045 } |
| 1046 |
| 1047 SeqOneByteString* GetWasmBytes(JSObject* wasm) { |
| 1048 return SeqOneByteString::cast(wasm->GetInternalField(kWasmModuleBytesString)); |
| 1049 } |
| 1050 |
| 1051 WasmDebugInfo* GetDebugInfo(JSObject* wasm) { |
| 1052 Object* info = wasm->GetInternalField(kWasmDebugInfo); |
| 1053 if (!info->IsUndefined(wasm->GetIsolate())) return WasmDebugInfo::cast(info); |
| 1054 Handle<WasmDebugInfo> new_info = WasmDebugInfo::New(handle(wasm)); |
| 1055 wasm->SetInternalField(kWasmDebugInfo, *new_info); |
| 1056 return *new_info; |
1019 } | 1057 } |
1020 | 1058 |
1021 } // namespace wasm | 1059 } // namespace wasm |
1022 } // namespace internal | 1060 } // namespace internal |
1023 } // namespace v8 | 1061 } // namespace v8 |
OLD | NEW |