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

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

Issue 2340623003: [wasm] Strongly typed compiled module (Closed)
Patch Set: Fix build failure Created 4 years, 2 months 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') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <memory> 5 #include <memory>
6 6
7 #include "src/base/atomic-utils.h" 7 #include "src/base/atomic-utils.h"
8 #include "src/code-stubs.h" 8 #include "src/code-stubs.h"
9 9
10 #include "src/macro-assembler.h" 10 #include "src/macro-assembler.h"
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 kWasmGlobalsArrayBuffer, 147 kWasmGlobalsArrayBuffer,
148 // TODO(clemensh): Remove function name array, extract names from module 148 // TODO(clemensh): Remove function name array, extract names from module
149 // bytes. 149 // bytes.
150 kWasmFunctionNamesArray, 150 kWasmFunctionNamesArray,
151 kWasmModuleBytesString, 151 kWasmModuleBytesString,
152 kWasmDebugInfo, 152 kWasmDebugInfo,
153 kWasmNumImportedFunctions, 153 kWasmNumImportedFunctions,
154 kWasmModuleInternalFieldCount 154 kWasmModuleInternalFieldCount
155 }; 155 };
156 156
157 // TODO(mtrofin): Unnecessary once we stop using JS Heap for wasm code.
158 // For now, each field is expected to have the type commented by its side.
159 // The elements typed as "maybe" are optional. The others are mandatory. Since
160 // the compiled module is either obtained from the current v8 instance, or from
161 // a snapshot produced by a compatible (==identical) v8 instance, we simply
162 // fail at instantiation time, in the face of invalid data.
163 enum WasmCompiledModule {
164 kCodeTable, // FixedArray of Code
165 kImportData, // maybe FixedArray of FixedArray respecting the
166 // WasmImportData structure.
167 kExportData, // maybe FixedArray of FixedArray of WasmExportData
168 // structure
169 kStartupData, // maybe FixedArray of WasmExportData structure
170 kTableOfIndirectFunctionTables, // maybe FixedArray of FixedArray of
171 // WasmIndirectFunctionTableData
172 kModuleBytes, // maybe String
173 kFunctionNameTable, // maybe ByteArray
174 kMinRequiredMemory, // Smi. an uint32_t
175 // The following 2 are either together present or absent:
176 kDataSegmentsInfo, // maybe FixedArray of FixedArray respecting the
177 // WasmSegmentInfo structure
178 kDataSegments, // maybe ByteArray.
179
180 kGlobalsSize, // Smi. an uint32_t
181 kMemSize, // Smi.an uint32_t
182 kMemStart, // MaybeHandle<ArrayBuffer>
183 kExportMem, // Smi. bool
184 kOrigin, // Smi. ModuleOrigin
185 kNextInstance, // WeakCell. See compiled code cloning.
186 kPrevInstance, // WeakCell. See compiled code cloning.
187 kOwningInstance, // WeakCell, pointing to the owning instance.
188 kModuleObject, // WeakCell, pointing to the module object.
189 kWasmCompiledModuleSize // Sentinel value.
190 };
191
192 enum WasmImportData { 157 enum WasmImportData {
193 kModuleName, // String 158 kModuleName, // String
194 kFunctionName, // maybe String 159 kFunctionName, // maybe String
195 kOutputCount, // Smi. an uint32_t 160 kOutputCount, // Smi. an uint32_t
196 kSignature, // ByteArray. A copy of the data in FunctionSig 161 kSignature, // ByteArray. A copy of the data in FunctionSig
197 kWasmImportDataSize // Sentinel value. 162 kWasmImportDataSize // Sentinel value.
198 }; 163 };
199 164
200 enum WasmExportData { 165 enum WasmExportData {
201 kExportName, // String 166 kExportName, // String
(...skipping 12 matching lines...) Expand all
214 enum WasmIndirectFunctionTableData { 179 enum WasmIndirectFunctionTableData {
215 kSize, // Smi. an uint32_t 180 kSize, // Smi. an uint32_t
216 kTable, // FixedArray of indirect function table 181 kTable, // FixedArray of indirect function table
217 kWasmIndirectFunctionTableDataSize // Sentinel value. 182 kWasmIndirectFunctionTableDataSize // Sentinel value.
218 }; 183 };
219 184
220 uint32_t GetMinModuleMemSize(const WasmModule* module) { 185 uint32_t GetMinModuleMemSize(const WasmModule* module) {
221 return WasmModule::kPageSize * module->min_mem_pages; 186 return WasmModule::kPageSize * module->min_mem_pages;
222 } 187 }
223 188
224 void LoadDataSegments(Handle<FixedArray> compiled_module, Address mem_addr, 189 void LoadDataSegments(Handle<WasmCompiledModule> compiled_module,
225 size_t mem_size) { 190 Address mem_addr, size_t mem_size) {
226 Isolate* isolate = compiled_module->GetIsolate(); 191 CHECK(compiled_module->has_data_segments() ==
227 MaybeHandle<ByteArray> maybe_data = 192 compiled_module->has_data_segments_info());
228 compiled_module->GetValue<ByteArray>(isolate, kDataSegments);
229 MaybeHandle<FixedArray> maybe_segments =
230 compiled_module->GetValue<FixedArray>(isolate, kDataSegmentsInfo);
231 193
232 // We either have both or neither.
233 CHECK(maybe_data.is_null() == maybe_segments.is_null());
234 // If we have neither, we're done. 194 // If we have neither, we're done.
235 if (maybe_data.is_null()) return; 195 if (!compiled_module->has_data_segments()) return;
236 196
237 Handle<ByteArray> data = maybe_data.ToHandleChecked(); 197 Handle<ByteArray> data = compiled_module->data_segments();
238 Handle<FixedArray> segments = maybe_segments.ToHandleChecked(); 198 Handle<FixedArray> segments = compiled_module->data_segments_info();
239 199
240 uint32_t last_extraction_pos = 0; 200 uint32_t last_extraction_pos = 0;
241 for (int i = 0; i < segments->length(); ++i) { 201 for (int i = 0; i < segments->length(); ++i) {
242 Handle<ByteArray> segment = 202 Handle<ByteArray> segment =
243 Handle<ByteArray>(ByteArray::cast(segments->get(i))); 203 Handle<ByteArray>(ByteArray::cast(segments->get(i)));
244 uint32_t dest_addr = static_cast<uint32_t>(segment->get_int(kDestAddr)); 204 uint32_t dest_addr = static_cast<uint32_t>(segment->get_int(kDestAddr));
245 uint32_t source_size = static_cast<uint32_t>(segment->get_int(kSourceSize)); 205 uint32_t source_size = static_cast<uint32_t>(segment->get_int(kSourceSize));
246 CHECK_LT(dest_addr, mem_size); 206 CHECK_LT(dest_addr, mem_size);
247 CHECK_LE(source_size, mem_size); 207 CHECK_LE(source_size, mem_size);
248 CHECK_LE(dest_addr, mem_size - source_size); 208 CHECK_LE(dest_addr, mem_size - source_size);
249 byte* addr = mem_addr + dest_addr; 209 byte* addr = mem_addr + dest_addr;
250 data->copy_out(last_extraction_pos, addr, source_size); 210 data->copy_out(last_extraction_pos, addr, source_size);
251 last_extraction_pos += source_size; 211 last_extraction_pos += source_size;
252 } 212 }
253 } 213 }
254 214
255 void SaveDataSegmentInfo(Factory* factory, const WasmModule* module, 215 void SaveDataSegmentInfo(Factory* factory, const WasmModule* module,
256 Handle<FixedArray> compiled_module) { 216 Handle<WasmCompiledModule> compiled_module) {
257 Handle<FixedArray> segments = factory->NewFixedArray( 217 Handle<FixedArray> segments = factory->NewFixedArray(
258 static_cast<int>(module->data_segments.size()), TENURED); 218 static_cast<int>(module->data_segments.size()), TENURED);
259 uint32_t data_size = 0; 219 uint32_t data_size = 0;
260 for (const WasmDataSegment& segment : module->data_segments) { 220 for (const WasmDataSegment& segment : module->data_segments) {
261 if (segment.source_size == 0) continue; 221 if (segment.source_size == 0) continue;
262 data_size += segment.source_size; 222 data_size += segment.source_size;
263 } 223 }
264 Handle<ByteArray> data = factory->NewByteArray(data_size, TENURED); 224 Handle<ByteArray> data = factory->NewByteArray(data_size, TENURED);
265 225
266 uint32_t last_insertion_pos = 0; 226 uint32_t last_insertion_pos = 0;
267 for (uint32_t i = 0; i < module->data_segments.size(); ++i) { 227 for (uint32_t i = 0; i < module->data_segments.size(); ++i) {
268 const WasmDataSegment& segment = module->data_segments[i]; 228 const WasmDataSegment& segment = module->data_segments[i];
269 if (segment.source_size == 0) continue; 229 if (segment.source_size == 0) continue;
270 Handle<ByteArray> js_segment = 230 Handle<ByteArray> js_segment =
271 factory->NewByteArray(kWasmSegmentInfoSize * sizeof(uint32_t), TENURED); 231 factory->NewByteArray(kWasmSegmentInfoSize * sizeof(uint32_t), TENURED);
272 // TODO(titzer): add support for global offsets for dest_addr 232 // TODO(titzer): add support for global offsets for dest_addr
273 CHECK_EQ(WasmInitExpr::kI32Const, segment.dest_addr.kind); 233 CHECK_EQ(WasmInitExpr::kI32Const, segment.dest_addr.kind);
274 js_segment->set_int(kDestAddr, segment.dest_addr.val.i32_const); 234 js_segment->set_int(kDestAddr, segment.dest_addr.val.i32_const);
275 js_segment->set_int(kSourceSize, segment.source_size); 235 js_segment->set_int(kSourceSize, segment.source_size);
276 segments->set(i, *js_segment); 236 segments->set(i, *js_segment);
277 data->copy_in(last_insertion_pos, 237 data->copy_in(last_insertion_pos,
278 module->module_start + segment.source_offset, 238 module->module_start + segment.source_offset,
279 segment.source_size); 239 segment.source_size);
280 last_insertion_pos += segment.source_size; 240 last_insertion_pos += segment.source_size;
281 } 241 }
282 compiled_module->set(kDataSegmentsInfo, *segments); 242 compiled_module->set_data_segments_info(segments);
283 compiled_module->set(kDataSegments, *data); 243 compiled_module->set_data_segments(data);
284 } 244 }
285 245
286 void PatchFunctionTable(Handle<Code> code, 246 void PatchFunctionTable(Handle<Code> code,
287 Handle<FixedArray> old_indirect_table, 247 Handle<FixedArray> old_indirect_table,
288 Handle<FixedArray> new_indirect_table) { 248 Handle<FixedArray> new_indirect_table) {
289 for (RelocIterator it(*code, 1 << RelocInfo::EMBEDDED_OBJECT); !it.done(); 249 for (RelocIterator it(*code, 1 << RelocInfo::EMBEDDED_OBJECT); !it.done();
290 it.next()) { 250 it.next()) {
291 if (it.rinfo()->target_object() == *old_indirect_table) { 251 if (it.rinfo()->target_object() == *old_indirect_table) {
292 it.rinfo()->set_target_object(*new_indirect_table); 252 it.rinfo()->set_target_object(*new_indirect_table);
293 } 253 }
(...skipping 587 matching lines...) Expand 10 before | Expand all | Expand 10 after
881 if (new_code != old_code) { 841 if (new_code != old_code) {
882 it.rinfo()->set_target_address(new_code->instruction_start(), 842 it.rinfo()->set_target_address(new_code->instruction_start(),
883 UPDATE_WRITE_BARRIER, 843 UPDATE_WRITE_BARRIER,
884 SKIP_ICACHE_FLUSH); 844 SKIP_ICACHE_FLUSH);
885 } 845 }
886 } 846 }
887 } 847 }
888 } 848 }
889 } 849 }
890 850
891 #define GET_COMPILED_MODULE_WEAK_RELATION_OR_NULL(Field) \
892 WeakCell* Get##Field(const FixedArray* compiled_module) { \
893 Object* obj = compiled_module->get(k##Field); \
894 DCHECK_NOT_NULL(obj); \
895 if (obj->IsWeakCell()) { \
896 return WeakCell::cast(obj); \
897 } else { \
898 return nullptr; \
899 } \
900 }
901
902 GET_COMPILED_MODULE_WEAK_RELATION_OR_NULL(NextInstance)
903 GET_COMPILED_MODULE_WEAK_RELATION_OR_NULL(PrevInstance)
904 GET_COMPILED_MODULE_WEAK_RELATION_OR_NULL(OwningInstance)
905 GET_COMPILED_MODULE_WEAK_RELATION_OR_NULL(ModuleObject)
906
907 static void ResetCompiledModule(Isolate* isolate, JSObject* owner, 851 static void ResetCompiledModule(Isolate* isolate, JSObject* owner,
908 FixedArray* compiled_module) { 852 WasmCompiledModule* compiled_module) {
909 Object* undefined = *isolate->factory()->undefined_value(); 853 Object* undefined = *isolate->factory()->undefined_value();
910 Object* mem_size_obj = compiled_module->get(kMemSize); 854 uint32_t old_mem_size = compiled_module->mem_size();
911 DCHECK(mem_size_obj->IsMutableHeapNumber()); 855 Object* mem_start = compiled_module->ptr_to_mem_start();
912 uint32_t old_mem_size =
913 static_cast<uint32_t>(HeapNumber::cast(mem_size_obj)->value());
914 Object* mem_start = compiled_module->get(kMemStart);
915 Address old_mem_address = nullptr; 856 Address old_mem_address = nullptr;
916 Address globals_start = 857 Address globals_start =
917 GetGlobalStartAddressFromCodeTemplate(undefined, owner); 858 GetGlobalStartAddressFromCodeTemplate(undefined, owner);
918 859
919 if (old_mem_size > 0) { 860 if (old_mem_size > 0) {
920 CHECK_NE(mem_start, undefined); 861 CHECK_NE(mem_start, undefined);
921 old_mem_address = 862 old_mem_address =
922 static_cast<Address>(JSArrayBuffer::cast(mem_start)->backing_store()); 863 static_cast<Address>(JSArrayBuffer::cast(mem_start)->backing_store());
923 } 864 }
924 int mode_mask = RelocInfo::ModeMask(RelocInfo::WASM_MEMORY_REFERENCE) | 865 int mode_mask = RelocInfo::ModeMask(RelocInfo::WASM_MEMORY_REFERENCE) |
925 RelocInfo::ModeMask(RelocInfo::WASM_MEMORY_SIZE_REFERENCE) | 866 RelocInfo::ModeMask(RelocInfo::WASM_MEMORY_SIZE_REFERENCE) |
926 RelocInfo::ModeMask(RelocInfo::WASM_GLOBAL_REFERENCE); 867 RelocInfo::ModeMask(RelocInfo::WASM_GLOBAL_REFERENCE);
927 868
928 Object* fct_obj = compiled_module->get(kCodeTable); 869 Object* fct_obj = compiled_module->ptr_to_code_table();
929 if (fct_obj != nullptr && fct_obj != undefined && 870 if (fct_obj != nullptr && fct_obj != undefined &&
930 (old_mem_size > 0 || globals_start != nullptr)) { 871 (old_mem_size > 0 || globals_start != nullptr)) {
931 FixedArray* functions = FixedArray::cast(fct_obj); 872 FixedArray* functions = FixedArray::cast(fct_obj);
932 for (int i = 0; i < functions->length(); ++i) { 873 for (int i = 0; i < functions->length(); ++i) {
933 Code* code = Code::cast(functions->get(i)); 874 Code* code = Code::cast(functions->get(i));
934 bool changed = false; 875 bool changed = false;
935 for (RelocIterator it(code, mode_mask); !it.done(); it.next()) { 876 for (RelocIterator it(code, mode_mask); !it.done(); it.next()) {
936 RelocInfo::Mode mode = it.rinfo()->rmode(); 877 RelocInfo::Mode mode = it.rinfo()->rmode();
937 if (RelocInfo::IsWasmMemoryReference(mode) || 878 if (RelocInfo::IsWasmMemoryReference(mode) ||
938 RelocInfo::IsWasmMemorySizeReference(mode)) { 879 RelocInfo::IsWasmMemorySizeReference(mode)) {
939 it.rinfo()->update_wasm_memory_reference(old_mem_address, nullptr, 880 it.rinfo()->update_wasm_memory_reference(old_mem_address, nullptr,
940 old_mem_size, old_mem_size); 881 old_mem_size, old_mem_size);
941 changed = true; 882 changed = true;
942 } else { 883 } else {
943 CHECK(RelocInfo::IsWasmGlobalReference(mode)); 884 CHECK(RelocInfo::IsWasmGlobalReference(mode));
944 it.rinfo()->update_wasm_global_reference(globals_start, nullptr); 885 it.rinfo()->update_wasm_global_reference(globals_start, nullptr);
945 changed = true; 886 changed = true;
946 } 887 }
947 } 888 }
948 if (changed) { 889 if (changed) {
949 Assembler::FlushICache(isolate, code->instruction_start(), 890 Assembler::FlushICache(isolate, code->instruction_start(),
950 code->instruction_size()); 891 code->instruction_size());
951 } 892 }
952 } 893 }
953 } 894 }
954 compiled_module->set(kOwningInstance, undefined); 895 compiled_module->reset_weak_owning_instance();
955 compiled_module->set(kMemStart, undefined); 896 compiled_module->reset_mem_start();
956 } 897 }
957 898
958 static void InstanceFinalizer(const v8::WeakCallbackInfo<void>& data) { 899 static void InstanceFinalizer(const v8::WeakCallbackInfo<void>& data) {
959 JSObject** p = reinterpret_cast<JSObject**>(data.GetParameter()); 900 JSObject** p = reinterpret_cast<JSObject**>(data.GetParameter());
960 JSObject* owner = *p; 901 JSObject* owner = *p;
961 FixedArray* compiled_module = 902 WasmCompiledModule* compiled_module =
962 FixedArray::cast(owner->GetInternalField(kWasmCompiledModule)); 903 WasmCompiledModule::cast(owner->GetInternalField(kWasmCompiledModule));
963 Isolate* isolate = reinterpret_cast<Isolate*>(data.GetIsolate()); 904 Isolate* isolate = reinterpret_cast<Isolate*>(data.GetIsolate());
964 Object* undefined = *isolate->factory()->undefined_value(); 905 DCHECK(compiled_module->has_weak_module_object());
965 WeakCell* weak_module_obj = GetModuleObject(compiled_module); 906 WeakCell* weak_module_obj = compiled_module->ptr_to_weak_module_object();
966 DCHECK_NOT_NULL(weak_module_obj); 907
967 // weak_module_obj may have been cleared, meaning the module object 908 // weak_module_obj may have been cleared, meaning the module object
968 // was GC-ed. In that case, there won't be any new instances created, 909 // was GC-ed. In that case, there won't be any new instances created,
969 // and we don't need to maintain the links between instances. 910 // and we don't need to maintain the links between instances.
970 if (!weak_module_obj->cleared()) { 911 if (!weak_module_obj->cleared()) {
971 JSObject* module_obj = JSObject::cast(weak_module_obj->value()); 912 JSObject* module_obj = JSObject::cast(weak_module_obj->value());
972 FixedArray* current_template = 913 WasmCompiledModule* current_template =
973 FixedArray::cast(module_obj->GetInternalField(0)); 914 WasmCompiledModule::cast(module_obj->GetInternalField(0));
974 DCHECK_NULL(GetPrevInstance(current_template)); 915 DCHECK(!current_template->has_weak_prev_instance());
975 WeakCell* next = GetNextInstance(compiled_module); 916 WeakCell* next = compiled_module->ptr_to_weak_next_instance();
976 WeakCell* prev = GetPrevInstance(compiled_module); 917 WeakCell* prev = compiled_module->ptr_to_weak_prev_instance();
977 918
978 if (current_template == compiled_module) { 919 if (current_template == compiled_module) {
979 if (next == nullptr) { 920 if (next == nullptr) {
980 ResetCompiledModule(isolate, owner, compiled_module); 921 ResetCompiledModule(isolate, owner, compiled_module);
981 } else { 922 } else {
982 DCHECK(next->value()->IsFixedArray()); 923 DCHECK(next->value()->IsFixedArray());
983 module_obj->SetInternalField(0, next->value()); 924 module_obj->SetInternalField(0, next->value());
984 DCHECK_NULL(prev); 925 DCHECK_NULL(prev);
985 FixedArray::cast(next->value())->set(kPrevInstance, undefined); 926 WasmCompiledModule::cast(next->value())->reset_weak_prev_instance();
986 } 927 }
987 } else { 928 } else {
988 DCHECK(!(prev == nullptr && next == nullptr)); 929 DCHECK(!(prev == nullptr && next == nullptr));
989 // the only reason prev or next would be cleared is if the 930 // the only reason prev or next would be cleared is if the
990 // respective objects got collected, but if that happened, 931 // respective objects got collected, but if that happened,
991 // we would have relinked the list. 932 // we would have relinked the list.
992 if (prev != nullptr) { 933 if (prev != nullptr) {
993 DCHECK(!prev->cleared()); 934 DCHECK(!prev->cleared());
994 FixedArray::cast(prev->value()) 935 if (next == nullptr) {
995 ->set(kNextInstance, next == nullptr ? undefined : next); 936 WasmCompiledModule::cast(prev->value())->reset_weak_next_instance();
937 } else {
938 WasmCompiledModule::cast(prev->value())
939 ->set_ptr_to_weak_next_instance(next);
940 }
996 } 941 }
997 if (next != nullptr) { 942 if (next != nullptr) {
998 DCHECK(!next->cleared()); 943 DCHECK(!next->cleared());
999 FixedArray::cast(next->value()) 944 if (prev == nullptr) {
1000 ->set(kPrevInstance, prev == nullptr ? undefined : prev); 945 WasmCompiledModule::cast(next->value())->reset_weak_prev_instance();
946 } else {
947 WasmCompiledModule::cast(next->value())
948 ->set_ptr_to_weak_prev_instance(prev);
949 }
1001 } 950 }
1002 } 951 }
1003 } 952 }
1004 GlobalHandles::Destroy(reinterpret_cast<Object**>(p)); 953 GlobalHandles::Destroy(reinterpret_cast<Object**>(p));
1005 } 954 }
1006 955
1007 } // namespace 956 } // namespace
1008 957
1009 MaybeHandle<FixedArray> WasmModule::CompileFunctions( 958 MaybeHandle<WasmCompiledModule> WasmModule::CompileFunctions(
1010 Isolate* isolate, ErrorThrower* thrower) const { 959 Isolate* isolate, ErrorThrower* thrower) const {
1011 Factory* factory = isolate->factory(); 960 Factory* factory = isolate->factory();
1012 961
1013 MaybeHandle<FixedArray> nothing; 962 MaybeHandle<WasmCompiledModule> nothing;
1014 963
1015 WasmModuleInstance temp_instance(this); 964 WasmModuleInstance temp_instance(this);
1016 temp_instance.context = isolate->native_context(); 965 temp_instance.context = isolate->native_context();
1017 temp_instance.mem_size = GetMinModuleMemSize(this); 966 temp_instance.mem_size = GetMinModuleMemSize(this);
1018 temp_instance.mem_start = nullptr; 967 temp_instance.mem_start = nullptr;
1019 temp_instance.globals_start = nullptr; 968 temp_instance.globals_start = nullptr;
1020 969
1021 MaybeHandle<FixedArray> indirect_table = 970 MaybeHandle<FixedArray> indirect_table =
1022 function_tables.size() 971 function_tables.size()
1023 ? factory->NewFixedArray(static_cast<int>(function_tables.size()), 972 ? factory->NewFixedArray(static_cast<int>(function_tables.size()),
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
1094 // TODO(mtrofin): do we need to flush the cache here? 1043 // TODO(mtrofin): do we need to flush the cache here?
1095 Assembler::FlushICache(isolate, code->instruction_start(), 1044 Assembler::FlushICache(isolate, code->instruction_start(),
1096 code->instruction_size()); 1045 code->instruction_size());
1097 } 1046 }
1098 } 1047 }
1099 1048
1100 // Create the compiled module object, and populate with compiled functions 1049 // Create the compiled module object, and populate with compiled functions
1101 // and information needed at instantiation time. This object needs to be 1050 // and information needed at instantiation time. This object needs to be
1102 // serializable. Instantiation may occur off a deserialized version of this 1051 // serializable. Instantiation may occur off a deserialized version of this
1103 // object. 1052 // object.
1104 Handle<FixedArray> ret = 1053 Handle<WasmCompiledModule> ret = WasmCompiledModule::New(isolate);
1105 factory->NewFixedArray(kWasmCompiledModuleSize, TENURED); 1054 ret->set_code_table(code_table);
1106 ret->set(kCodeTable, *code_table);
1107 if (!indirect_table.is_null()) { 1055 if (!indirect_table.is_null()) {
1108 ret->set(kTableOfIndirectFunctionTables, *indirect_table.ToHandleChecked()); 1056 ret->set_indirect_function_tables(indirect_table.ToHandleChecked());
1109 } 1057 }
1110 Handle<FixedArray> import_data = GetImportsData(factory, this); 1058 Handle<FixedArray> import_data = GetImportsData(factory, this);
1111 ret->set(kImportData, *import_data); 1059 ret->set_import_data(import_data);
1112 1060
1113 // Compile exported function wrappers. 1061 // Compile exported function wrappers.
1114 int export_size = static_cast<int>(num_exported_functions); 1062 int export_size = static_cast<int>(num_exported_functions);
1115 if (export_size > 0) { 1063 if (export_size > 0) {
1116 Handle<FixedArray> exports = factory->NewFixedArray(export_size, TENURED); 1064 Handle<FixedArray> exports = factory->NewFixedArray(export_size, TENURED);
1117 int index = -1; 1065 int index = -1;
1118 1066
1119 for (const WasmExport& exp : export_table) { 1067 for (const WasmExport& exp : export_table) {
1120 if (exp.kind != kExternalFunction) 1068 if (exp.kind != kExternalFunction)
1121 continue; // skip non-function exports. 1069 continue; // skip non-function exports.
(...skipping 17 matching lines...) Expand all
1139 if (thrower->error()) return nothing; 1087 if (thrower->error()) return nothing;
1140 export_data->set(kExportName, *name); 1088 export_data->set(kExportName, *name);
1141 export_data->set(kExportArity, 1089 export_data->set(kExportArity,
1142 Smi::FromInt(static_cast<int>( 1090 Smi::FromInt(static_cast<int>(
1143 functions[exp.index].sig->parameter_count()))); 1091 functions[exp.index].sig->parameter_count())));
1144 export_data->set(kExportedFunctionIndex, 1092 export_data->set(kExportedFunctionIndex,
1145 Smi::FromInt(static_cast<int>(exp.index))); 1093 Smi::FromInt(static_cast<int>(exp.index)));
1146 exports->set(index, *export_data); 1094 exports->set(index, *export_data);
1147 code_table->set(static_cast<int>(functions.size() + index), *export_code); 1095 code_table->set(static_cast<int>(functions.size() + index), *export_code);
1148 } 1096 }
1149 ret->set(kExportData, *exports); 1097 ret->set_exports(exports);
1150 } 1098 }
1151 1099
1152 // Record data for startup function. 1100 // Record data for startup function.
1153 if (start_function_index >= 0) { 1101 if (start_function_index >= 0) {
1154 HandleScope scope(isolate); 1102 HandleScope scope(isolate);
1155 Handle<FixedArray> startup_data = 1103 Handle<FixedArray> startup_data =
1156 factory->NewFixedArray(kWasmExportDataSize, TENURED); 1104 factory->NewFixedArray(kWasmExportDataSize, TENURED);
1157 startup_data->set(kExportArity, Smi::FromInt(0)); 1105 startup_data->set(kExportArity, Smi::FromInt(0));
1158 startup_data->set(kExportedFunctionIndex, 1106 startup_data->set(kExportedFunctionIndex,
1159 Smi::FromInt(start_function_index)); 1107 Smi::FromInt(start_function_index));
1160 ret->set(kStartupData, *startup_data); 1108 ret->set_startup_function(startup_data);
1161 } 1109 }
1162 1110
1163 // TODO(wasm): saving the module bytes for debugging is wasteful. We should 1111 // TODO(wasm): saving the module bytes for debugging is wasteful. We should
1164 // consider downloading this on-demand. 1112 // consider downloading this on-demand.
1165 { 1113 {
1166 size_t module_bytes_len = module_end - module_start; 1114 size_t module_bytes_len = module_end - module_start;
1167 DCHECK_LE(module_bytes_len, static_cast<size_t>(kMaxInt)); 1115 DCHECK_LE(module_bytes_len, static_cast<size_t>(kMaxInt));
1168 Vector<const uint8_t> module_bytes_vec(module_start, 1116 Vector<const uint8_t> module_bytes_vec(module_start,
1169 static_cast<int>(module_bytes_len)); 1117 static_cast<int>(module_bytes_len));
1170 Handle<String> module_bytes_string = 1118 Handle<String> module_bytes_string =
1171 factory->NewStringFromOneByte(module_bytes_vec, TENURED) 1119 factory->NewStringFromOneByte(module_bytes_vec, TENURED)
1172 .ToHandleChecked(); 1120 .ToHandleChecked();
1173 ret->set(kModuleBytes, *module_bytes_string); 1121 ret->set_module_bytes(module_bytes_string);
1174 } 1122 }
1175 1123
1176 Handle<ByteArray> function_name_table = 1124 Handle<ByteArray> function_name_table =
1177 BuildFunctionNamesTable(isolate, module_env.module); 1125 BuildFunctionNamesTable(isolate, module_env.module);
1178 ret->set(kFunctionNameTable, *function_name_table); 1126 ret->set_function_names(function_name_table);
1179 ret->set(kMinRequiredMemory, Smi::FromInt(min_mem_pages)); 1127 ret->set_min_required_memory(min_mem_pages);
1180 if (data_segments.size() > 0) SaveDataSegmentInfo(factory, this, ret); 1128 if (data_segments.size() > 0) SaveDataSegmentInfo(factory, this, ret);
1181 ret->set(kGlobalsSize, Smi::FromInt(globals_size)); 1129 ret->set_globals_size(globals_size);
1182 ret->set(kExportMem, Smi::FromInt(mem_export)); 1130 ret->set_export_memory(mem_export);
1183 ret->set(kOrigin, Smi::FromInt(origin)); 1131 ret->set_origin(origin);
1184 Handle<HeapNumber> size_as_object = factory->NewHeapNumber( 1132 ret->set_mem_size(temp_instance.mem_size);
1185 static_cast<double>(temp_instance.mem_size), MUTABLE, TENURED);
1186 ret->set(kMemSize, *size_as_object);
1187 return ret; 1133 return ret;
1188 } 1134 }
1189 1135
1190 void PatchJSWrapper(Isolate* isolate, Handle<Code> wrapper, 1136 void PatchJSWrapper(Isolate* isolate, Handle<Code> wrapper,
1191 Handle<Code> new_target) { 1137 Handle<Code> new_target) {
1192 AllowDeferredHandleDereference embedding_raw_address; 1138 AllowDeferredHandleDereference embedding_raw_address;
1193 bool seen = false; 1139 bool seen = false;
1194 for (RelocIterator it(*wrapper, 1 << RelocInfo::CODE_TARGET); !it.done(); 1140 for (RelocIterator it(*wrapper, 1 << RelocInfo::CODE_TARGET); !it.done();
1195 it.next()) { 1141 it.next()) {
1196 Code* target = Code::GetCodeFromTargetAddress(it.rinfo()->target_address()); 1142 Code* target = Code::GetCodeFromTargetAddress(it.rinfo()->target_address());
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1244 Handle<JSReceiver> ffi, 1190 Handle<JSReceiver> ffi,
1245 Handle<JSArrayBuffer> memory) { 1191 Handle<JSArrayBuffer> memory) {
1246 MaybeHandle<JSObject> nothing; 1192 MaybeHandle<JSObject> nothing;
1247 HistogramTimerScope wasm_instantiate_module_time_scope( 1193 HistogramTimerScope wasm_instantiate_module_time_scope(
1248 isolate->counters()->wasm_instantiate_module_time()); 1194 isolate->counters()->wasm_instantiate_module_time());
1249 Factory* factory = isolate->factory(); 1195 Factory* factory = isolate->factory();
1250 1196
1251 //-------------------------------------------------------------------------- 1197 //--------------------------------------------------------------------------
1252 // Reuse the compiled module (if no owner), otherwise clone. 1198 // Reuse the compiled module (if no owner), otherwise clone.
1253 //-------------------------------------------------------------------------- 1199 //--------------------------------------------------------------------------
1254 Handle<FixedArray> compiled_module; 1200 Handle<WasmCompiledModule> compiled_module;
1255 Handle<FixedArray> code_table; 1201 Handle<FixedArray> code_table;
1256 Handle<FixedArray> old_code_table; 1202 Handle<FixedArray> old_code_table;
1257 Handle<JSObject> owner; 1203 Handle<JSObject> owner;
1258 // If we don't clone, this will be null(). Otherwise, this will 1204 // If we don't clone, this will be null(). Otherwise, this will
1259 // be a weak link to the original. If we lose the original to GC, 1205 // be a weak link to the original. If we lose the original to GC,
1260 // this will be a cleared. We'll link the instances chain last. 1206 // this will be a cleared. We'll link the instances chain last.
1261 MaybeHandle<WeakCell> link_to_original; 1207 MaybeHandle<WeakCell> link_to_original;
1262 1208
1263 { 1209 {
1264 Handle<FixedArray> original( 1210 Handle<WasmCompiledModule> original(
1265 FixedArray::cast(module_object->GetInternalField(0)), isolate); 1211 WasmCompiledModule::cast(module_object->GetInternalField(0)), isolate);
1266 // Always make a new copy of the code_table, since the old_code_table 1212 // Always make a new copy of the code_table, since the old_code_table
1267 // may still have placeholders for imports. 1213 // may still have placeholders for imports.
1268 old_code_table = original->GetValueChecked<FixedArray>(isolate, kCodeTable); 1214 old_code_table = original->code_table();
1269 code_table = factory->CopyFixedArray(old_code_table); 1215 code_table = factory->CopyFixedArray(old_code_table);
1270 1216
1271 WeakCell* tmp = GetOwningInstance(*original); 1217 if (original->has_weak_owning_instance()) {
1272 if (tmp != nullptr) { 1218 WeakCell* tmp = original->ptr_to_weak_owning_instance();
1273 DCHECK(!tmp->cleared()); 1219 DCHECK(!tmp->cleared());
1274 // There is already an owner, clone everything. 1220 // There is already an owner, clone everything.
1275 owner = Handle<JSObject>(JSObject::cast(tmp->value()), isolate); 1221 owner = Handle<JSObject>(JSObject::cast(tmp->value()), isolate);
1276 // Insert the latest clone in front. 1222 // Insert the latest clone in front.
1277 compiled_module = factory->CopyFixedArray(original); 1223 compiled_module = original->Clone(isolate);
1278 // Replace the strong reference to point to the new instance here. 1224 // Replace the strong reference to point to the new instance here.
1279 // This allows any of the other instances, including the original, 1225 // This allows any of the other instances, including the original,
1280 // to be collected. 1226 // to be collected.
1281 module_object->SetInternalField(0, *compiled_module); 1227 module_object->SetInternalField(0, *compiled_module);
1282 Handle<WeakCell> weak_link_to_wasm_obj = 1228 compiled_module->set_weak_module_object(original->weak_module_object());
1283 original->GetValueChecked<WeakCell>(isolate, kModuleObject);
1284
1285 compiled_module->set(kModuleObject, *weak_link_to_wasm_obj);
1286 link_to_original = factory->NewWeakCell(original); 1229 link_to_original = factory->NewWeakCell(original);
1287 // Don't link to original here. We remember the original 1230 // Don't link to original here. We remember the original
1288 // as a weak link. If that link isn't clear by the time we finish 1231 // as a weak link. If that link isn't clear by the time we finish
1289 // instantiating this instance, then we link it at that time. 1232 // instantiating this instance, then we link it at that time.
1290 compiled_module->set(kNextInstance, *factory->undefined_value()); 1233 compiled_module->reset_weak_next_instance();
1291 1234
1292 // Clone the code for WASM functions and exports. 1235 // Clone the code for WASM functions and exports.
1293 for (int i = 0; i < code_table->length(); ++i) { 1236 for (int i = 0; i < code_table->length(); ++i) {
1294 Handle<Code> orig_code = code_table->GetValueChecked<Code>(isolate, i); 1237 Handle<Code> orig_code = code_table->GetValueChecked<Code>(isolate, i);
1295 switch (orig_code->kind()) { 1238 switch (orig_code->kind()) {
1296 case Code::WASM_TO_JS_FUNCTION: 1239 case Code::WASM_TO_JS_FUNCTION:
1297 // Imports will be overwritten with newly compiled wrappers. 1240 // Imports will be overwritten with newly compiled wrappers.
1298 break; 1241 break;
1299 case Code::JS_TO_WASM_FUNCTION: 1242 case Code::JS_TO_WASM_FUNCTION:
1300 case Code::WASM_FUNCTION: { 1243 case Code::WASM_FUNCTION: {
1301 Handle<Code> code = factory->CopyCode(orig_code); 1244 Handle<Code> code = factory->CopyCode(orig_code);
1302 code_table->set(i, *code); 1245 code_table->set(i, *code);
1303 break; 1246 break;
1304 } 1247 }
1305 default: 1248 default:
1306 UNREACHABLE(); 1249 UNREACHABLE();
1307 } 1250 }
1308 } 1251 }
1309 Handle<HeapNumber> size_as_object = factory->NewHeapNumber( 1252 compiled_module->set_mem_size(original->mem_size());
1310 static_cast<double>(
1311 compiled_module->GetValueChecked<HeapNumber>(isolate, kMemSize)
1312 ->value()),
1313 MUTABLE, TENURED);
1314 compiled_module->set(kMemSize, *size_as_object);
1315 RecordStats(isolate, code_table); 1253 RecordStats(isolate, code_table);
1316 } else { 1254 } else {
1317 // There was no owner, so we can reuse the original. 1255 // There was no owner, so we can reuse the original.
1318 compiled_module = original; 1256 compiled_module = original;
1319 } 1257 }
1320 compiled_module->set(kCodeTable, *code_table); 1258 compiled_module->set_code_table(code_table);
1321 } 1259 }
1322 1260
1323 //-------------------------------------------------------------------------- 1261 //--------------------------------------------------------------------------
1324 // Allocate the instance object. 1262 // Allocate the instance object.
1325 //-------------------------------------------------------------------------- 1263 //--------------------------------------------------------------------------
1326 Handle<Map> map = factory->NewMap( 1264 Handle<Map> map = factory->NewMap(
1327 JS_OBJECT_TYPE, 1265 JS_OBJECT_TYPE,
1328 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize); 1266 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize);
1329 Handle<JSObject> instance = factory->NewJSObjectFromMap(map, TENURED); 1267 Handle<JSObject> instance = factory->NewJSObjectFromMap(map, TENURED);
1330 instance->SetInternalField(kWasmModuleCodeTable, *code_table); 1268 instance->SetInternalField(kWasmModuleCodeTable, *code_table);
1331 1269
1332 //-------------------------------------------------------------------------- 1270 //--------------------------------------------------------------------------
1333 // Set up the memory for the new instance. 1271 // Set up the memory for the new instance.
1334 //-------------------------------------------------------------------------- 1272 //--------------------------------------------------------------------------
1335 MaybeHandle<JSArrayBuffer> old_memory; 1273 MaybeHandle<JSArrayBuffer> old_memory;
1336 // TODO(titzer): handle imported memory properly. 1274 // TODO(titzer): handle imported memory properly.
1337 1275
1338 uint32_t min_mem_pages = static_cast<uint32_t>( 1276 uint32_t min_mem_pages = compiled_module->min_required_memory();
1339 Smi::cast(compiled_module->get(kMinRequiredMemory))->value());
1340 isolate->counters()->wasm_min_mem_pages_count()->AddSample(min_mem_pages); 1277 isolate->counters()->wasm_min_mem_pages_count()->AddSample(min_mem_pages);
1341 // TODO(wasm): re-enable counter for max_mem_pages when we use that field. 1278 // TODO(wasm): re-enable counter for max_mem_pages when we use that field.
1342 1279
1343 if (memory.is_null() && min_mem_pages > 0) { 1280 if (memory.is_null() && min_mem_pages > 0) {
1344 memory = AllocateMemory(thrower, isolate, min_mem_pages); 1281 memory = AllocateMemory(thrower, isolate, min_mem_pages);
1345 if (memory.is_null()) return nothing; // failed to allocate memory 1282 if (memory.is_null()) return nothing; // failed to allocate memory
1346 } 1283 }
1347 1284
1348 if (!memory.is_null()) { 1285 if (!memory.is_null()) {
1349 instance->SetInternalField(kWasmMemArrayBuffer, *memory); 1286 instance->SetInternalField(kWasmMemArrayBuffer, *memory);
1350 Address mem_start = static_cast<Address>(memory->backing_store()); 1287 Address mem_start = static_cast<Address>(memory->backing_store());
1351 uint32_t mem_size = static_cast<uint32_t>(memory->byte_length()->Number()); 1288 uint32_t mem_size = static_cast<uint32_t>(memory->byte_length()->Number());
1352 LoadDataSegments(compiled_module, mem_start, mem_size); 1289 LoadDataSegments(compiled_module, mem_start, mem_size);
1353 1290
1354 uint32_t old_mem_size = static_cast<uint32_t>( 1291 uint32_t old_mem_size = compiled_module->mem_size();
1355 compiled_module->GetValueChecked<HeapNumber>(isolate, kMemSize)
1356 ->value());
1357 MaybeHandle<JSArrayBuffer> old_mem =
1358 compiled_module->GetValue<JSArrayBuffer>(isolate, kMemStart);
1359 Address old_mem_start = 1292 Address old_mem_start =
1360 old_mem.is_null() 1293 compiled_module->has_mem_start()
1361 ? nullptr 1294 ? static_cast<Address>(
1362 : static_cast<Address>(old_mem.ToHandleChecked()->backing_store()); 1295 compiled_module->mem_start()->backing_store())
1296 : nullptr;
1363 RelocateInstanceCode(instance, old_mem_start, mem_start, old_mem_size, 1297 RelocateInstanceCode(instance, old_mem_start, mem_start, old_mem_size,
1364 mem_size); 1298 mem_size);
1365 compiled_module->GetValueChecked<HeapNumber>(isolate, kMemSize) 1299 compiled_module->set_mem_size(mem_size);
1366 ->set_value(static_cast<double>(mem_size)); 1300 compiled_module->set_mem_start(memory);
1367 compiled_module->set(kMemStart, *memory);
1368 } 1301 }
1369 1302
1370 //-------------------------------------------------------------------------- 1303 //--------------------------------------------------------------------------
1371 // Set up the globals for the new instance. 1304 // Set up the globals for the new instance.
1372 //-------------------------------------------------------------------------- 1305 //--------------------------------------------------------------------------
1373 MaybeHandle<JSArrayBuffer> old_globals; 1306 MaybeHandle<JSArrayBuffer> old_globals;
1374 MaybeHandle<JSArrayBuffer> globals; 1307 MaybeHandle<JSArrayBuffer> globals;
1375 uint32_t globals_size = static_cast<uint32_t>( 1308 uint32_t globals_size = compiled_module->globals_size();
1376 Smi::cast(compiled_module->get(kGlobalsSize))->value());
1377 if (globals_size > 0) { 1309 if (globals_size > 0) {
1378 Handle<JSArrayBuffer> global_buffer = NewArrayBuffer(isolate, globals_size); 1310 Handle<JSArrayBuffer> global_buffer = NewArrayBuffer(isolate, globals_size);
1379 globals = global_buffer; 1311 globals = global_buffer;
1380 if (globals.is_null()) { 1312 if (globals.is_null()) {
1381 thrower->Error("Out of memory: wasm globals"); 1313 thrower->Error("Out of memory: wasm globals");
1382 return nothing; 1314 return nothing;
1383 } 1315 }
1384 Address old_address = 1316 Address old_address =
1385 owner.is_null() ? nullptr : GetGlobalStartAddressFromCodeTemplate( 1317 owner.is_null() ? nullptr : GetGlobalStartAddressFromCodeTemplate(
1386 *isolate->factory()->undefined_value(), 1318 *isolate->factory()->undefined_value(),
1387 JSObject::cast(*owner)); 1319 JSObject::cast(*owner));
1388 RelocateGlobals(instance, old_address, 1320 RelocateGlobals(instance, old_address,
1389 static_cast<Address>(global_buffer->backing_store())); 1321 static_cast<Address>(global_buffer->backing_store()));
1390 instance->SetInternalField(kWasmGlobalsArrayBuffer, *global_buffer); 1322 instance->SetInternalField(kWasmGlobalsArrayBuffer, *global_buffer);
1391 } 1323 }
1392 1324
1393 //-------------------------------------------------------------------------- 1325 //--------------------------------------------------------------------------
1394 // Compile the import wrappers for the new instance. 1326 // Compile the import wrappers for the new instance.
1395 //-------------------------------------------------------------------------- 1327 //--------------------------------------------------------------------------
1396 // TODO(titzer): handle imported globals and function tables. 1328 // TODO(titzer): handle imported globals and function tables.
1397 Handle<FixedArray> import_data;
1398 int num_imported_functions = 0; 1329 int num_imported_functions = 0;
1399 if (compiled_module->GetValue<FixedArray>(isolate, kImportData) 1330 if (compiled_module->has_import_data()) {
1400 .ToHandle(&import_data)) { 1331 Handle<FixedArray> import_data = compiled_module->import_data();
1401 num_imported_functions = import_data->length(); 1332 num_imported_functions = import_data->length();
1402 for (int index = 0; index < num_imported_functions; index++) { 1333 for (int index = 0; index < num_imported_functions; index++) {
1403 Handle<Code> import_wrapper = 1334 Handle<Code> import_wrapper =
1404 CompileImportWrapper(isolate, ffi, index, import_data, thrower); 1335 CompileImportWrapper(isolate, ffi, index, import_data, thrower);
1405 if (thrower->error()) return nothing; 1336 if (thrower->error()) return nothing;
1406 code_table->set(index, *import_wrapper); 1337 code_table->set(index, *import_wrapper);
1407 RecordStats(isolate, *import_wrapper); 1338 RecordStats(isolate, *import_wrapper);
1408 } 1339 }
1409 } 1340 }
1410 1341
1411 //-------------------------------------------------------------------------- 1342 //--------------------------------------------------------------------------
1412 // Set up the debug support for the new instance. 1343 // Set up the debug support for the new instance.
1413 //-------------------------------------------------------------------------- 1344 //--------------------------------------------------------------------------
1414 MaybeHandle<String> module_bytes_string = 1345 // TODO(wasm): avoid referencing this stuff from the instance, use it off
1415 compiled_module->GetValue<String>(isolate, kModuleBytes); 1346 // the compiled module instead. See the following 3 assignments:
1416 if (!module_bytes_string.is_null()) { 1347 if (compiled_module->has_module_bytes()) {
1417 instance->SetInternalField(kWasmModuleBytesString, 1348 instance->SetInternalField(kWasmModuleBytesString,
1418 *module_bytes_string.ToHandleChecked()); 1349 compiled_module->ptr_to_module_bytes());
1419 } 1350 }
1420 1351
1421 MaybeHandle<ByteArray> function_name_table = 1352 if (compiled_module->has_function_names()) {
1422 compiled_module->GetValue<ByteArray>(isolate, kFunctionNameTable); 1353 instance->SetInternalField(kWasmFunctionNamesArray,
1423 if (!function_name_table.is_null()) { 1354 compiled_module->ptr_to_function_names());
1424 Handle<ByteArray> handle = function_name_table.ToHandleChecked();
1425 instance->SetInternalField(kWasmFunctionNamesArray, *handle);
1426 } 1355 }
1427 1356
1428 { 1357 {
1429 Handle<Object> handle = factory->NewNumber(num_imported_functions); 1358 Handle<Object> handle = factory->NewNumber(num_imported_functions);
1430 instance->SetInternalField(kWasmNumImportedFunctions, *handle); 1359 instance->SetInternalField(kWasmNumImportedFunctions, *handle);
1431 } 1360 }
1432 1361
1433 //-------------------------------------------------------------------------- 1362 //--------------------------------------------------------------------------
1434 // Set up the runtime support for the new instance. 1363 // Set up the runtime support for the new instance.
1435 //-------------------------------------------------------------------------- 1364 //--------------------------------------------------------------------------
(...skipping 15 matching lines...) Expand all
1451 //-------------------------------------------------------------------------- 1380 //--------------------------------------------------------------------------
1452 // Set up the indirect function tables for the new instance. 1381 // Set up the indirect function tables for the new instance.
1453 //-------------------------------------------------------------------------- 1382 //--------------------------------------------------------------------------
1454 { 1383 {
1455 std::vector<Handle<Code>> functions( 1384 std::vector<Handle<Code>> functions(
1456 static_cast<size_t>(code_table->length())); 1385 static_cast<size_t>(code_table->length()));
1457 for (int i = 0; i < code_table->length(); ++i) { 1386 for (int i = 0; i < code_table->length(); ++i) {
1458 functions[i] = code_table->GetValueChecked<Code>(isolate, i); 1387 functions[i] = code_table->GetValueChecked<Code>(isolate, i);
1459 } 1388 }
1460 1389
1461 MaybeHandle<FixedArray> maybe_indirect_tables = 1390 if (compiled_module->has_indirect_function_tables()) {
1462 compiled_module->GetValue<FixedArray>(isolate, 1391 Handle<FixedArray> indirect_tables_template =
1463 kTableOfIndirectFunctionTables); 1392 compiled_module->indirect_function_tables();
1464 Handle<FixedArray> indirect_tables_template;
1465 if (maybe_indirect_tables.ToHandle(&indirect_tables_template)) {
1466 Handle<FixedArray> to_replace = 1393 Handle<FixedArray> to_replace =
1467 owner.is_null() ? indirect_tables_template 1394 owner.is_null() ? indirect_tables_template
1468 : handle(FixedArray::cast(owner->GetInternalField( 1395 : handle(FixedArray::cast(owner->GetInternalField(
1469 kWasmModuleFunctionTable))); 1396 kWasmModuleFunctionTable)));
1470 Handle<FixedArray> indirect_tables = SetupIndirectFunctionTable( 1397 Handle<FixedArray> indirect_tables = SetupIndirectFunctionTable(
1471 isolate, code_table, indirect_tables_template, to_replace); 1398 isolate, code_table, indirect_tables_template, to_replace);
1472 for (int i = 0; i < indirect_tables->length(); ++i) { 1399 for (int i = 0; i < indirect_tables->length(); ++i) {
1473 Handle<FixedArray> metadata = 1400 Handle<FixedArray> metadata =
1474 indirect_tables->GetValueChecked<FixedArray>(isolate, i); 1401 indirect_tables->GetValueChecked<FixedArray>(isolate, i);
1475 uint32_t size = Smi::cast(metadata->get(kSize))->value(); 1402 uint32_t size = Smi::cast(metadata->get(kSize))->value();
1476 Handle<FixedArray> table = 1403 Handle<FixedArray> table =
1477 metadata->GetValueChecked<FixedArray>(isolate, kTable); 1404 metadata->GetValueChecked<FixedArray>(isolate, kTable);
1478 wasm::PopulateFunctionTable(table, size, &functions); 1405 wasm::PopulateFunctionTable(table, size, &functions);
1479 } 1406 }
1480 instance->SetInternalField(kWasmModuleFunctionTable, *indirect_tables); 1407 instance->SetInternalField(kWasmModuleFunctionTable, *indirect_tables);
1481 } 1408 }
1482 } 1409 }
1483 1410
1484 //-------------------------------------------------------------------------- 1411 //--------------------------------------------------------------------------
1485 // Set up the exports object for the new instance. 1412 // Set up the exports object for the new instance.
1486 //-------------------------------------------------------------------------- 1413 //--------------------------------------------------------------------------
1487 bool mem_export = 1414 bool mem_export = compiled_module->export_memory();
1488 static_cast<bool>(Smi::cast(compiled_module->get(kExportMem))->value()); 1415 ModuleOrigin origin = compiled_module->origin();
1489 ModuleOrigin origin = static_cast<ModuleOrigin>(
1490 Smi::cast(compiled_module->get(kOrigin))->value());
1491 1416
1492 MaybeHandle<FixedArray> maybe_exports = 1417 if (compiled_module->has_exports() || mem_export) {
1493 compiled_module->GetValue<FixedArray>(isolate, kExportData);
1494 if (!maybe_exports.is_null() || mem_export) {
1495 PropertyDescriptor desc; 1418 PropertyDescriptor desc;
1496 desc.set_writable(false); 1419 desc.set_writable(false);
1497 1420
1498 Handle<JSObject> exports_object = instance; 1421 Handle<JSObject> exports_object = instance;
1499 if (origin == kWasmOrigin) { 1422 if (origin == kWasmOrigin) {
1500 // Create the "exports" object. 1423 // Create the "exports" object.
1501 Handle<JSFunction> object_function = Handle<JSFunction>( 1424 Handle<JSFunction> object_function = Handle<JSFunction>(
1502 isolate->native_context()->object_function(), isolate); 1425 isolate->native_context()->object_function(), isolate);
1503 exports_object = factory->NewJSObject(object_function, TENURED); 1426 exports_object = factory->NewJSObject(object_function, TENURED);
1504 Handle<String> exports_name = factory->InternalizeUtf8String("exports"); 1427 Handle<String> exports_name = factory->InternalizeUtf8String("exports");
1505 JSObject::AddProperty(instance, exports_name, exports_object, READ_ONLY); 1428 JSObject::AddProperty(instance, exports_name, exports_object, READ_ONLY);
1506 } 1429 }
1507 Handle<FixedArray> exports;
1508 int first_export = -1; 1430 int first_export = -1;
1509 // TODO(wasm): another iteration over the code objects. 1431 // TODO(wasm): another iteration over the code objects.
1510 for (int i = 0; i < code_table->length(); i++) { 1432 for (int i = 0; i < code_table->length(); i++) {
1511 Handle<Code> code = code_table->GetValueChecked<Code>(isolate, i); 1433 Handle<Code> code = code_table->GetValueChecked<Code>(isolate, i);
1512 if (code->kind() == Code::JS_TO_WASM_FUNCTION) { 1434 if (code->kind() == Code::JS_TO_WASM_FUNCTION) {
1513 first_export = i; 1435 first_export = i;
1514 break; 1436 break;
1515 } 1437 }
1516 } 1438 }
1517 if (maybe_exports.ToHandle(&exports)) { 1439 if (compiled_module->has_exports()) {
1440 Handle<FixedArray> exports = compiled_module->exports();
1518 int export_size = exports->length(); 1441 int export_size = exports->length();
1519 for (int i = 0; i < export_size; ++i) { 1442 for (int i = 0; i < export_size; ++i) {
1520 Handle<FixedArray> export_data = 1443 Handle<FixedArray> export_data =
1521 exports->GetValueChecked<FixedArray>(isolate, i); 1444 exports->GetValueChecked<FixedArray>(isolate, i);
1522 Handle<String> name = 1445 Handle<String> name =
1523 export_data->GetValueChecked<String>(isolate, kExportName); 1446 export_data->GetValueChecked<String>(isolate, kExportName);
1524 int arity = Smi::cast(export_data->get(kExportArity))->value(); 1447 int arity = Smi::cast(export_data->get(kExportArity))->value();
1525 MaybeHandle<ByteArray> signature = 1448 MaybeHandle<ByteArray> signature =
1526 export_data->GetValue<ByteArray>(isolate, kExportedSignature); 1449 export_data->GetValue<ByteArray>(isolate, kExportedSignature);
1527 Handle<Code> export_code = 1450 Handle<Code> export_code =
(...skipping 25 matching lines...) Expand all
1553 if (num_imported_functions > 0 || !owner.is_null()) { 1476 if (num_imported_functions > 0 || !owner.is_null()) {
1554 // If the code was cloned, or new imports were compiled, patch. 1477 // If the code was cloned, or new imports were compiled, patch.
1555 PatchDirectCalls(old_code_table, code_table, num_imported_functions); 1478 PatchDirectCalls(old_code_table, code_table, num_imported_functions);
1556 } 1479 }
1557 1480
1558 FlushICache(isolate, code_table); 1481 FlushICache(isolate, code_table);
1559 1482
1560 //-------------------------------------------------------------------------- 1483 //--------------------------------------------------------------------------
1561 // Run the start function if one was specified. 1484 // Run the start function if one was specified.
1562 //-------------------------------------------------------------------------- 1485 //--------------------------------------------------------------------------
1563 Handle<FixedArray> startup_data; 1486 if (compiled_module->has_startup_function()) {
1564 if (compiled_module->GetValue<FixedArray>(isolate, kStartupData) 1487 Handle<FixedArray> startup_data = compiled_module->startup_function();
1565 .ToHandle(&startup_data)) {
1566 HandleScope scope(isolate); 1488 HandleScope scope(isolate);
1567 int32_t start_index = 1489 int32_t start_index =
1568 startup_data->GetValueChecked<Smi>(isolate, kExportedFunctionIndex) 1490 startup_data->GetValueChecked<Smi>(isolate, kExportedFunctionIndex)
1569 ->value(); 1491 ->value();
1570 Handle<Code> startup_code = 1492 Handle<Code> startup_code =
1571 code_table->GetValueChecked<Code>(isolate, start_index); 1493 code_table->GetValueChecked<Code>(isolate, start_index);
1572 int arity = Smi::cast(startup_data->get(kExportArity))->value(); 1494 int arity = Smi::cast(startup_data->get(kExportArity))->value();
1573 MaybeHandle<ByteArray> startup_signature = 1495 MaybeHandle<ByteArray> startup_signature =
1574 startup_data->GetValue<ByteArray>(isolate, kExportedSignature); 1496 startup_data->GetValue<ByteArray>(isolate, kExportedSignature);
1575 Handle<JSFunction> startup_fct = WrapExportCodeAsJSFunction( 1497 Handle<JSFunction> startup_fct = WrapExportCodeAsJSFunction(
1576 isolate, startup_code, factory->InternalizeUtf8String("start"), arity, 1498 isolate, startup_code, factory->InternalizeUtf8String("start"), arity,
1577 startup_signature, instance); 1499 startup_signature, instance);
1578 RecordStats(isolate, *startup_code); 1500 RecordStats(isolate, *startup_code);
1579 // Call the JS function. 1501 // Call the JS function.
1580 Handle<Object> undefined = isolate->factory()->undefined_value(); 1502 Handle<Object> undefined = isolate->factory()->undefined_value();
1581 MaybeHandle<Object> retval = 1503 MaybeHandle<Object> retval =
1582 Execution::Call(isolate, startup_fct, undefined, 0, nullptr); 1504 Execution::Call(isolate, startup_fct, undefined, 0, nullptr);
1583 1505
1584 if (retval.is_null()) { 1506 if (retval.is_null()) {
1585 thrower->Error("WASM.instantiateModule(): start function failed"); 1507 thrower->Error("WASM.instantiateModule(): start function failed");
1586 return nothing; 1508 return nothing;
1587 } 1509 }
1588 } 1510 }
1589 1511
1590 DCHECK(wasm::IsWasmObject(*instance)); 1512 DCHECK(wasm::IsWasmObject(*instance));
1591 1513
1592 if (!compiled_module->GetValue<WeakCell>(isolate, kModuleObject).is_null()) { 1514 if (compiled_module->has_weak_module_object()) {
1593 instance->SetInternalField(kWasmCompiledModule, *compiled_module); 1515 instance->SetInternalField(kWasmCompiledModule, *compiled_module);
1594 Handle<WeakCell> link_to_owner = factory->NewWeakCell(instance); 1516 Handle<WeakCell> link_to_owner = factory->NewWeakCell(instance);
1595 1517
1596 Handle<Object> global_handle = isolate->global_handles()->Create(*instance); 1518 Handle<Object> global_handle = isolate->global_handles()->Create(*instance);
1597 Handle<WeakCell> link_to_clone = factory->NewWeakCell(compiled_module); 1519 Handle<WeakCell> link_to_clone = factory->NewWeakCell(compiled_module);
1598 { 1520 {
1599 DisallowHeapAllocation no_gc; 1521 DisallowHeapAllocation no_gc;
1600 compiled_module->set(kOwningInstance, *link_to_owner); 1522 compiled_module->set_weak_owning_instance(link_to_owner);
1601 Handle<WeakCell> next; 1523 Handle<WeakCell> next;
1602 if (link_to_original.ToHandle(&next) && !next->cleared()) { 1524 if (link_to_original.ToHandle(&next) && !next->cleared()) {
1603 FixedArray* original = FixedArray::cast(next->value()); 1525 WasmCompiledModule* original = WasmCompiledModule::cast(next->value());
1604 DCHECK_NOT_NULL(GetOwningInstance(original)); 1526 DCHECK(original->has_weak_owning_instance());
1605 DCHECK(!GetOwningInstance(original)->cleared()); 1527 DCHECK(!original->weak_owning_instance()->cleared());
1606 compiled_module->set(kNextInstance, *next); 1528 compiled_module->set_weak_next_instance(next);
1607 original->set(kPrevInstance, *link_to_clone); 1529 original->set_weak_prev_instance(link_to_clone);
1608 } 1530 }
1609 GlobalHandles::MakeWeak(global_handle.location(), 1531 GlobalHandles::MakeWeak(global_handle.location(),
1610 global_handle.location(), &InstanceFinalizer, 1532 global_handle.location(), &InstanceFinalizer,
1611 v8::WeakCallbackType::kFinalizer); 1533 v8::WeakCallbackType::kFinalizer);
1612 } 1534 }
1613 } 1535 }
1614 1536
1615 return instance; 1537 return instance;
1616 } 1538 }
1617 1539
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
1780 Handle<Map> map = isolate->factory()->NewMap( 1702 Handle<Map> map = isolate->factory()->NewMap(
1781 JS_OBJECT_TYPE, JSObject::kHeaderSize + kPointerSize); 1703 JS_OBJECT_TYPE, JSObject::kHeaderSize + kPointerSize);
1782 module_obj = isolate->factory()->NewJSObjectFromMap(map, TENURED); 1704 module_obj = isolate->factory()->NewJSObjectFromMap(map, TENURED);
1783 } 1705 }
1784 module_obj->SetInternalField(0, *compiled_module); 1706 module_obj->SetInternalField(0, *compiled_module);
1785 if (origin == ModuleOrigin::kWasmOrigin) { 1707 if (origin == ModuleOrigin::kWasmOrigin) {
1786 Handle<Symbol> module_sym(isolate->native_context()->wasm_module_sym()); 1708 Handle<Symbol> module_sym(isolate->native_context()->wasm_module_sym());
1787 Object::SetProperty(module_obj, module_sym, module_obj, STRICT).Check(); 1709 Object::SetProperty(module_obj, module_sym, module_obj, STRICT).Check();
1788 } 1710 }
1789 Handle<WeakCell> link_to_module = isolate->factory()->NewWeakCell(module_obj); 1711 Handle<WeakCell> link_to_module = isolate->factory()->NewWeakCell(module_obj);
1790 compiled_module->set(kModuleObject, *link_to_module); 1712 WasmCompiledModule::cast(*compiled_module)
1713 ->set_weak_module_object(link_to_module);
1791 return module_obj; 1714 return module_obj;
1792 } 1715 }
1793 1716
1794 MaybeHandle<JSObject> CreateModuleObjectFromBytes(Isolate* isolate, 1717 MaybeHandle<JSObject> CreateModuleObjectFromBytes(Isolate* isolate,
1795 const byte* start, 1718 const byte* start,
1796 const byte* end, 1719 const byte* end,
1797 ErrorThrower* thrower, 1720 ErrorThrower* thrower,
1798 ModuleOrigin origin) { 1721 ModuleOrigin origin) {
1799 MaybeHandle<JSObject> nothing; 1722 MaybeHandle<JSObject> nothing;
1800 Zone zone(isolate->allocator()); 1723 Zone zone(isolate->allocator());
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1833 if (mem->IsUndefined(isolate)) return MaybeHandle<JSArrayBuffer>(); 1756 if (mem->IsUndefined(isolate)) return MaybeHandle<JSArrayBuffer>();
1834 return Handle<JSArrayBuffer>(JSArrayBuffer::cast(mem)); 1757 return Handle<JSArrayBuffer>(JSArrayBuffer::cast(mem));
1835 } 1758 }
1836 1759
1837 void SetInstanceMemory(Handle<JSObject> instance, JSArrayBuffer* buffer) { 1760 void SetInstanceMemory(Handle<JSObject> instance, JSArrayBuffer* buffer) {
1838 DisallowHeapAllocation no_gc; 1761 DisallowHeapAllocation no_gc;
1839 DCHECK(IsWasmObject(*instance)); 1762 DCHECK(IsWasmObject(*instance));
1840 instance->SetInternalField(kWasmMemArrayBuffer, buffer); 1763 instance->SetInternalField(kWasmMemArrayBuffer, buffer);
1841 Object* module = instance->GetInternalField(kWasmCompiledModule); 1764 Object* module = instance->GetInternalField(kWasmCompiledModule);
1842 if (module->IsFixedArray()) { 1765 if (module->IsFixedArray()) {
1843 HeapNumber::cast(FixedArray::cast(module)->get(kMemSize)) 1766 WasmCompiledModule::cast(module)->set_mem_size(
1844 ->set_value(buffer->byte_length()->Number()); 1767 buffer->byte_length()->Number());
1845 } 1768 }
1846 } 1769 }
1847 1770
1848 namespace testing { 1771 namespace testing {
1849 1772
1850 void ValidateInstancesChain(Isolate* isolate, Handle<JSObject> module_obj, 1773 void ValidateInstancesChain(Isolate* isolate, Handle<JSObject> module_obj,
1851 int instance_count) { 1774 int instance_count) {
1852 CHECK_GE(instance_count, 0); 1775 CHECK_GE(instance_count, 0);
1853 DisallowHeapAllocation no_gc; 1776 DisallowHeapAllocation no_gc;
1854 FixedArray* compiled_module = 1777 WasmCompiledModule* compiled_module =
1855 FixedArray::cast(module_obj->GetInternalField(0)); 1778 WasmCompiledModule::cast(module_obj->GetInternalField(0));
1856 CHECK_EQ(JSObject::cast(GetModuleObject(compiled_module)->value()), 1779 CHECK_EQ(
1857 *module_obj); 1780 JSObject::cast(compiled_module->ptr_to_weak_module_object()->value()),
1781 *module_obj);
1858 Object* prev = nullptr; 1782 Object* prev = nullptr;
1859 int found_instances = GetOwningInstance(compiled_module) == nullptr ? 0 : 1; 1783 int found_instances = compiled_module->has_weak_owning_instance() ? 1 : 0;
1860 FixedArray* current_instance = compiled_module; 1784 WasmCompiledModule* current_instance = compiled_module;
1861 while (GetNextInstance(current_instance) != nullptr) { 1785 while (current_instance->has_weak_next_instance()) {
1862 CHECK((prev == nullptr && GetPrevInstance(current_instance) == nullptr) || 1786 CHECK((prev == nullptr && !current_instance->has_weak_prev_instance()) ||
1863 GetPrevInstance(current_instance)->value() == prev); 1787 current_instance->ptr_to_weak_prev_instance()->value() == prev);
1864 CHECK_EQ(GetModuleObject(current_instance)->value(), *module_obj); 1788 CHECK_EQ(current_instance->ptr_to_weak_module_object()->value(),
1865 CHECK(IsWasmObject(GetOwningInstance(current_instance)->value())); 1789 *module_obj);
1790 CHECK(
1791 IsWasmObject(current_instance->ptr_to_weak_owning_instance()->value()));
1866 prev = current_instance; 1792 prev = current_instance;
1867 current_instance = 1793 current_instance = WasmCompiledModule::cast(
1868 FixedArray::cast(GetNextInstance(current_instance)->value()); 1794 current_instance->ptr_to_weak_next_instance()->value());
1869 ++found_instances; 1795 ++found_instances;
1870 CHECK_LE(found_instances, instance_count); 1796 CHECK_LE(found_instances, instance_count);
1871 } 1797 }
1872 CHECK_EQ(found_instances, instance_count); 1798 CHECK_EQ(found_instances, instance_count);
1873 } 1799 }
1874 1800
1875 void ValidateModuleState(Isolate* isolate, Handle<JSObject> module_obj) { 1801 void ValidateModuleState(Isolate* isolate, Handle<JSObject> module_obj) {
1876 DisallowHeapAllocation no_gc; 1802 DisallowHeapAllocation no_gc;
1877 FixedArray* compiled_module = 1803 WasmCompiledModule* compiled_module =
1878 FixedArray::cast(module_obj->GetInternalField(0)); 1804 WasmCompiledModule::cast(module_obj->GetInternalField(0));
1879 CHECK_NOT_NULL(GetModuleObject(compiled_module)); 1805 CHECK(compiled_module->has_weak_module_object());
1880 CHECK_EQ(GetModuleObject(compiled_module)->value(), *module_obj); 1806 CHECK_EQ(compiled_module->ptr_to_weak_module_object()->value(), *module_obj);
1881 CHECK_NULL(GetPrevInstance(compiled_module)); 1807 CHECK(!compiled_module->has_weak_prev_instance());
1882 CHECK_NULL(GetNextInstance(compiled_module)); 1808 CHECK(!compiled_module->has_weak_next_instance());
1883 CHECK_NULL(GetOwningInstance(compiled_module)); 1809 CHECK(!compiled_module->has_weak_owning_instance());
1884 } 1810 }
1885 1811
1886 void ValidateOrphanedInstance(Isolate* isolate, Handle<JSObject> instance) { 1812 void ValidateOrphanedInstance(Isolate* isolate, Handle<JSObject> instance) {
1887 DisallowHeapAllocation no_gc; 1813 DisallowHeapAllocation no_gc;
1888 CHECK(IsWasmObject(*instance)); 1814 CHECK(IsWasmObject(*instance));
1889 FixedArray* compiled_module = 1815 WasmCompiledModule* compiled_module =
1890 FixedArray::cast(instance->GetInternalField(kWasmCompiledModule)); 1816 WasmCompiledModule::cast(instance->GetInternalField(kWasmCompiledModule));
1891 CHECK_NOT_NULL(GetModuleObject(compiled_module)); 1817 CHECK(compiled_module->has_weak_module_object());
1892 CHECK(GetModuleObject(compiled_module)->cleared()); 1818 CHECK(compiled_module->ptr_to_weak_module_object()->cleared());
1893 } 1819 }
1894 1820
1895 } // namespace testing 1821 } // namespace testing
1896 } // namespace wasm 1822 } // namespace wasm
1897 } // namespace internal 1823 } // namespace internal
1898 } // namespace v8 1824 } // namespace v8
OLDNEW
« no previous file with comments | « src/wasm/wasm-module.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698