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 <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 19 matching lines...) Expand all Loading... |
30 #define TRACE(...) \ | 30 #define TRACE(...) \ |
31 do { \ | 31 do { \ |
32 if (FLAG_trace_wasm_instances) PrintF(__VA_ARGS__); \ | 32 if (FLAG_trace_wasm_instances) PrintF(__VA_ARGS__); \ |
33 } while (false) | 33 } while (false) |
34 | 34 |
35 #define TRACE_CHAIN(instance) \ | 35 #define TRACE_CHAIN(instance) \ |
36 do { \ | 36 do { \ |
37 instance->PrintInstancesChain(); \ | 37 instance->PrintInstancesChain(); \ |
38 } while (false) | 38 } while (false) |
39 | 39 |
| 40 static const int kInvalidSigIndex = -1; |
| 41 |
| 42 // Collects all the data values to which a given WASM code object may be |
| 43 // specialized. |
| 44 struct Specialization { |
| 45 // The native context, which is used in JS->WASM and WASM->JS wrappers |
| 46 // and calls to the runtime. |
| 47 Handle<Context> context; |
| 48 |
| 49 // Specialization to the memory. |
| 50 byte* memory_base; |
| 51 uint32_t memory_size; |
| 52 |
| 53 // Specialization to the globals. |
| 54 byte* globals_base; |
| 55 |
| 56 // Specialization to the function table. |
| 57 uint32_t function_table_size; |
| 58 Handle<FixedArray> function_table_sigs; |
| 59 Handle<FixedArray> function_table_code; |
| 60 |
| 61 Specialization() |
| 62 : memory_base(nullptr), |
| 63 memory_size(0), |
| 64 globals_base(nullptr), |
| 65 function_table_size(0) {} |
| 66 }; |
| 67 |
40 namespace { | 68 namespace { |
41 | 69 |
42 static const int kPlaceholderMarker = 1000000000; | 70 static const int kPlaceholderMarker = 1000000000; |
43 | 71 |
44 enum JSFunctionExportInternalField { | 72 enum JSFunctionExportInternalField { |
45 kInternalModuleInstance, | 73 kInternalModuleInstance, |
46 kInternalFunctionIndex | 74 kInternalFunctionIndex |
47 }; | 75 }; |
48 | 76 |
49 // Internal constants for the layout of the module object. | 77 // Internal constants for the layout of the module object. |
(...skipping 30 matching lines...) Expand all Loading... |
80 Handle<Object> new_ref) { | 108 Handle<Object> new_ref) { |
81 for (RelocIterator it(*code, 1 << RelocInfo::EMBEDDED_OBJECT); !it.done(); | 109 for (RelocIterator it(*code, 1 << RelocInfo::EMBEDDED_OBJECT); !it.done(); |
82 it.next()) { | 110 it.next()) { |
83 if (it.rinfo()->target_object() == *old_ref) { | 111 if (it.rinfo()->target_object() == *old_ref) { |
84 it.rinfo()->set_target_object(*new_ref); | 112 it.rinfo()->set_target_object(*new_ref); |
85 } | 113 } |
86 } | 114 } |
87 } | 115 } |
88 | 116 |
89 Handle<JSArrayBuffer> NewArrayBuffer(Isolate* isolate, size_t size) { | 117 Handle<JSArrayBuffer> NewArrayBuffer(Isolate* isolate, size_t size) { |
90 if (size > (WasmModule::kMaxMemPages * WasmModule::kPageSize)) { | 118 if (size > (WasmModule::kV8MaxPages * WasmModule::kPageSize)) { |
91 // TODO(titzer): lift restriction on maximum memory allocated here. | 119 // TODO(titzer): lift restriction on maximum memory allocated here. |
92 return Handle<JSArrayBuffer>::null(); | 120 return Handle<JSArrayBuffer>::null(); |
93 } | 121 } |
94 void* memory = isolate->array_buffer_allocator()->Allocate(size); | 122 void* memory = isolate->array_buffer_allocator()->Allocate(size); |
95 if (memory == nullptr) { | 123 if (memory == nullptr) { |
96 return Handle<JSArrayBuffer>::null(); | 124 return Handle<JSArrayBuffer>::null(); |
97 } | 125 } |
98 | 126 |
99 #if DEBUG | 127 #if DEBUG |
100 // Double check the API allocator actually zero-initialized the memory. | 128 // Double check the API allocator actually zero-initialized the memory. |
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 } | 521 } |
494 int mode_mask = RelocInfo::ModeMask(RelocInfo::WASM_MEMORY_REFERENCE) | | 522 int mode_mask = RelocInfo::ModeMask(RelocInfo::WASM_MEMORY_REFERENCE) | |
495 RelocInfo::ModeMask(RelocInfo::WASM_MEMORY_SIZE_REFERENCE) | | 523 RelocInfo::ModeMask(RelocInfo::WASM_MEMORY_SIZE_REFERENCE) | |
496 RelocInfo::ModeMask(RelocInfo::WASM_GLOBAL_REFERENCE) | | 524 RelocInfo::ModeMask(RelocInfo::WASM_GLOBAL_REFERENCE) | |
497 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); | 525 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
498 | 526 |
499 // Patch code to update memory references, global references, and function | 527 // Patch code to update memory references, global references, and function |
500 // table references. | 528 // table references. |
501 Object* fct_obj = compiled_module->ptr_to_code_table(); | 529 Object* fct_obj = compiled_module->ptr_to_code_table(); |
502 if (fct_obj != nullptr && fct_obj != undefined && | 530 if (fct_obj != nullptr && fct_obj != undefined && |
503 (old_mem_size > 0 || globals_start != nullptr)) { | 531 (old_mem_size > 0 || globals_start != nullptr || function_tables)) { |
504 FixedArray* functions = FixedArray::cast(fct_obj); | 532 FixedArray* functions = FixedArray::cast(fct_obj); |
505 for (int i = 0; i < functions->length(); ++i) { | 533 for (int i = 0; i < functions->length(); ++i) { |
506 Code* code = Code::cast(functions->get(i)); | 534 Code* code = Code::cast(functions->get(i)); |
507 bool changed = false; | 535 bool changed = false; |
508 for (RelocIterator it(code, mode_mask); !it.done(); it.next()) { | 536 for (RelocIterator it(code, mode_mask); !it.done(); it.next()) { |
509 RelocInfo::Mode mode = it.rinfo()->rmode(); | 537 RelocInfo::Mode mode = it.rinfo()->rmode(); |
510 if (RelocInfo::IsWasmMemoryReference(mode) || | 538 if (RelocInfo::IsWasmMemoryReference(mode) || |
511 RelocInfo::IsWasmMemorySizeReference(mode)) { | 539 RelocInfo::IsWasmMemorySizeReference(mode)) { |
512 it.rinfo()->update_wasm_memory_reference( | 540 it.rinfo()->update_wasm_memory_reference( |
513 old_mem_address, nullptr, old_mem_size, default_mem_size); | 541 old_mem_address, nullptr, old_mem_size, default_mem_size); |
514 changed = true; | 542 changed = true; |
515 } else if (RelocInfo::IsWasmGlobalReference(mode)) { | 543 } else if (RelocInfo::IsWasmGlobalReference(mode)) { |
516 it.rinfo()->update_wasm_global_reference(globals_start, nullptr); | 544 it.rinfo()->update_wasm_global_reference(globals_start, nullptr); |
517 changed = true; | 545 changed = true; |
518 } else if (RelocInfo::IsEmbeddedObject(mode) && function_tables) { | 546 } else if (RelocInfo::IsEmbeddedObject(mode) && function_tables) { |
519 Object* old = it.rinfo()->target_object(); | 547 Object* old = it.rinfo()->target_object(); |
520 for (int i = 0; i < function_tables->length(); ++i) { | 548 for (int j = 0; j < function_tables->length(); ++j) { |
521 if (function_tables->get(i) == old) { | 549 if (function_tables->get(j) == old) { |
522 it.rinfo()->set_target_object(empty_function_tables->get(i)); | 550 it.rinfo()->set_target_object(empty_function_tables->get(j)); |
523 changed = true; | 551 changed = true; |
524 } | 552 } |
525 } | 553 } |
526 } | 554 } |
527 } | 555 } |
528 if (changed) { | 556 if (changed) { |
529 Assembler::FlushICache(isolate, code->instruction_start(), | 557 Assembler::FlushICache(isolate, code->instruction_start(), |
530 code->instruction_size()); | 558 code->instruction_size()); |
531 } | 559 } |
532 } | 560 } |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
836 Handle<String> module_bytes_string = | 864 Handle<String> module_bytes_string = |
837 factory->NewStringFromOneByte(module_bytes_vec, TENURED) | 865 factory->NewStringFromOneByte(module_bytes_vec, TENURED) |
838 .ToHandleChecked(); | 866 .ToHandleChecked(); |
839 DCHECK(module_bytes_string->IsSeqOneByteString()); | 867 DCHECK(module_bytes_string->IsSeqOneByteString()); |
840 ret->set_module_bytes(Handle<SeqOneByteString>::cast(module_bytes_string)); | 868 ret->set_module_bytes(Handle<SeqOneByteString>::cast(module_bytes_string)); |
841 } | 869 } |
842 | 870 |
843 return ret; | 871 return ret; |
844 } | 872 } |
845 | 873 |
| 874 static WasmFunction* GetWasmFunctionForImportWrapper(Isolate* isolate, |
| 875 Handle<Object> target) { |
| 876 if (target->IsJSFunction()) { |
| 877 Handle<JSFunction> func = Handle<JSFunction>::cast(target); |
| 878 Handle<Code> export_wrapper_code = handle(func->code()); |
| 879 if (export_wrapper_code->kind() == Code::JS_TO_WASM_FUNCTION) { |
| 880 Handle<JSObject> other_instance( |
| 881 JSObject::cast(func->GetInternalField(kInternalModuleInstance)), |
| 882 isolate); |
| 883 int func_index = |
| 884 Smi::cast(func->GetInternalField(kInternalFunctionIndex))->value(); |
| 885 return &GetCppModule(other_instance)->functions[func_index]; |
| 886 } |
| 887 } |
| 888 return nullptr; |
| 889 } |
| 890 |
| 891 static Handle<Code> UnwrapImportWrapper(Handle<Object> target) { |
| 892 Handle<JSFunction> func = Handle<JSFunction>::cast(target); |
| 893 Handle<Code> export_wrapper_code = handle(func->code()); |
| 894 int found = 0; |
| 895 int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET); |
| 896 Handle<Code> code; |
| 897 for (RelocIterator it(*export_wrapper_code, mask); !it.done(); it.next()) { |
| 898 RelocInfo* rinfo = it.rinfo(); |
| 899 Address target_address = rinfo->target_address(); |
| 900 Code* target = Code::GetCodeFromTargetAddress(target_address); |
| 901 if (target->kind() == Code::WASM_FUNCTION || |
| 902 target->kind() == Code::WASM_TO_JS_FUNCTION) { |
| 903 ++found; |
| 904 code = handle(target); |
| 905 } |
| 906 } |
| 907 DCHECK(found == 1); |
| 908 return code; |
| 909 } |
| 910 |
| 911 static Handle<Code> CompileImportWrapper(Isolate* isolate, int index, |
| 912 FunctionSig* sig, |
| 913 Handle<JSReceiver> target, |
| 914 Handle<String> module_name, |
| 915 MaybeHandle<String> import_name) { |
| 916 Handle<Code> code; |
| 917 WasmFunction* other_func = GetWasmFunctionForImportWrapper(isolate, target); |
| 918 if (other_func && sig->Equals(other_func->sig)) { |
| 919 // Signature matched. Unwrap the JS->WASM wrapper and return the raw |
| 920 // WASM function code. |
| 921 return UnwrapImportWrapper(target); |
| 922 } else { |
| 923 // Signature mismatch. Compile a new wrapper for the new signature. |
| 924 return compiler::CompileWasmToJSWrapper(isolate, target, sig, index, |
| 925 module_name, import_name); |
| 926 } |
| 927 } |
| 928 |
| 929 static void UpdateDispatchTablesInternal(Isolate* isolate, |
| 930 Handle<FixedArray> dispatch_tables, |
| 931 int index, WasmFunction* function, |
| 932 Handle<Code> code) { |
| 933 DCHECK_EQ(0, dispatch_tables->length() % 3); |
| 934 for (int i = 0; i < dispatch_tables->length(); i += 3) { |
| 935 Handle<Object> instance(dispatch_tables->get(i), isolate); |
| 936 WasmModule* module = GetCppModule(Handle<JSObject>::cast(instance)); |
| 937 int table_index = Smi::cast(dispatch_tables->get(i + 1))->value(); |
| 938 Handle<FixedArray> dispatch_table( |
| 939 FixedArray::cast(dispatch_tables->get(i + 2)), isolate); |
| 940 if (function) { |
| 941 // TODO(titzer): the signature might need to be copied to avoid |
| 942 // a dangling pointer in the signature map. |
| 943 int sig_index = static_cast<int>( |
| 944 module->function_tables[table_index].map.FindOrInsert(function->sig)); |
| 945 dispatch_table->set(index, Smi::FromInt(sig_index)); |
| 946 dispatch_table->set(index + (dispatch_table->length() / 2), *code); |
| 947 } else { |
| 948 Code* code = nullptr; |
| 949 dispatch_table->set(index, Smi::FromInt(-1)); |
| 950 dispatch_table->set(index + (dispatch_table->length() / 2), code); |
| 951 } |
| 952 } |
| 953 } |
| 954 |
| 955 void wasm::UpdateDispatchTables(Isolate* isolate, |
| 956 Handle<FixedArray> dispatch_tables, int index, |
| 957 Handle<JSFunction> function) { |
| 958 if (function.is_null()) { |
| 959 UpdateDispatchTablesInternal(isolate, dispatch_tables, index, nullptr, |
| 960 Handle<Code>::null()); |
| 961 } else { |
| 962 UpdateDispatchTablesInternal( |
| 963 isolate, dispatch_tables, index, |
| 964 GetWasmFunctionForImportWrapper(isolate, function), |
| 965 UnwrapImportWrapper(function)); |
| 966 } |
| 967 } |
| 968 |
846 // A helper class to simplify instantiating a module from a compiled module. | 969 // A helper class to simplify instantiating a module from a compiled module. |
847 // It closes over the {Isolate}, the {ErrorThrower}, the {WasmCompiledModule}, | 970 // It closes over the {Isolate}, the {ErrorThrower}, the {WasmCompiledModule}, |
848 // etc. | 971 // etc. |
849 class WasmInstanceBuilder { | 972 class WasmInstanceBuilder { |
850 public: | 973 public: |
851 WasmInstanceBuilder(Isolate* isolate, ErrorThrower* thrower, | 974 WasmInstanceBuilder(Isolate* isolate, ErrorThrower* thrower, |
852 Handle<JSObject> module_object, Handle<JSReceiver> ffi, | 975 Handle<JSObject> module_object, Handle<JSReceiver> ffi, |
853 Handle<JSArrayBuffer> memory) | 976 Handle<JSArrayBuffer> memory) |
854 : isolate_(isolate), | 977 : isolate_(isolate), |
855 thrower_(thrower), | 978 thrower_(thrower), |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
944 JS_OBJECT_TYPE, | 1067 JS_OBJECT_TYPE, |
945 JSObject::kHeaderSize + kWasmInstanceInternalFieldCount * kPointerSize); | 1068 JSObject::kHeaderSize + kWasmInstanceInternalFieldCount * kPointerSize); |
946 Handle<JSObject> instance = factory->NewJSObjectFromMap(map, TENURED); | 1069 Handle<JSObject> instance = factory->NewJSObjectFromMap(map, TENURED); |
947 instance->SetInternalField(kWasmMemObject, | 1070 instance->SetInternalField(kWasmMemObject, |
948 isolate_->heap()->undefined_value()); | 1071 isolate_->heap()->undefined_value()); |
949 | 1072 |
950 //-------------------------------------------------------------------------- | 1073 //-------------------------------------------------------------------------- |
951 // Set up the globals for the new instance. | 1074 // Set up the globals for the new instance. |
952 //-------------------------------------------------------------------------- | 1075 //-------------------------------------------------------------------------- |
953 MaybeHandle<JSArrayBuffer> old_globals; | 1076 MaybeHandle<JSArrayBuffer> old_globals; |
954 MaybeHandle<JSArrayBuffer> globals; | |
955 uint32_t globals_size = module_->globals_size; | 1077 uint32_t globals_size = module_->globals_size; |
956 if (globals_size > 0) { | 1078 if (globals_size > 0) { |
957 Handle<JSArrayBuffer> global_buffer = | 1079 Handle<JSArrayBuffer> global_buffer = |
958 NewArrayBuffer(isolate_, globals_size); | 1080 NewArrayBuffer(isolate_, globals_size); |
959 globals = global_buffer; | 1081 globals_ = global_buffer; |
960 if (globals.is_null()) { | 1082 if (globals_.is_null()) { |
961 thrower_->RangeError("Out of memory: wasm globals"); | 1083 thrower_->RangeError("Out of memory: wasm globals"); |
962 return nothing; | 1084 return nothing; |
963 } | 1085 } |
964 Address old_address = owner.is_null() | 1086 Address old_address = owner.is_null() |
965 ? nullptr | 1087 ? nullptr |
966 : GetGlobalStartAddressFromCodeTemplate( | 1088 : GetGlobalStartAddressFromCodeTemplate( |
967 isolate_->heap()->undefined_value(), | 1089 isolate_->heap()->undefined_value(), |
968 JSObject::cast(*owner.ToHandleChecked())); | 1090 JSObject::cast(*owner.ToHandleChecked())); |
969 RelocateGlobals(code_table, old_address, | 1091 RelocateGlobals(code_table, old_address, |
970 static_cast<Address>(global_buffer->backing_store())); | 1092 static_cast<Address>(global_buffer->backing_store())); |
971 instance->SetInternalField(kWasmGlobalsArrayBuffer, *global_buffer); | 1093 instance->SetInternalField(kWasmGlobalsArrayBuffer, *global_buffer); |
972 } | 1094 } |
973 | 1095 |
974 //-------------------------------------------------------------------------- | 1096 //-------------------------------------------------------------------------- |
| 1097 // Prepare for initialization of function tables. |
| 1098 //-------------------------------------------------------------------------- |
| 1099 int function_table_count = |
| 1100 static_cast<int>(module_->function_tables.size()); |
| 1101 table_instances_.reserve(module_->function_tables.size()); |
| 1102 for (int index = 0; index < function_table_count; ++index) { |
| 1103 table_instances_.push_back({Handle<JSObject>::null(), |
| 1104 Handle<FixedArray>::null(), |
| 1105 Handle<FixedArray>::null()}); |
| 1106 } |
| 1107 |
| 1108 //-------------------------------------------------------------------------- |
975 // Process the imports for the module. | 1109 // Process the imports for the module. |
976 //-------------------------------------------------------------------------- | 1110 //-------------------------------------------------------------------------- |
977 int num_imported_functions = ProcessImports(globals, code_table, instance); | 1111 int num_imported_functions = ProcessImports(code_table, instance); |
978 if (num_imported_functions < 0) return nothing; | 1112 if (num_imported_functions < 0) return nothing; |
979 | 1113 |
980 //-------------------------------------------------------------------------- | 1114 //-------------------------------------------------------------------------- |
981 // Process the initialization for the module's globals. | 1115 // Process the initialization for the module's globals. |
982 //-------------------------------------------------------------------------- | 1116 //-------------------------------------------------------------------------- |
983 InitGlobals(globals); | 1117 InitGlobals(); |
984 | 1118 |
985 //-------------------------------------------------------------------------- | 1119 //-------------------------------------------------------------------------- |
986 // Set up the memory for the new instance. | 1120 // Set up the memory for the new instance. |
987 //-------------------------------------------------------------------------- | 1121 //-------------------------------------------------------------------------- |
988 MaybeHandle<JSArrayBuffer> old_memory; | 1122 MaybeHandle<JSArrayBuffer> old_memory; |
989 | 1123 |
990 uint32_t min_mem_pages = module_->min_mem_pages; | 1124 uint32_t min_mem_pages = module_->min_mem_pages; |
991 isolate_->counters()->wasm_min_mem_pages_count()->AddSample(min_mem_pages); | 1125 isolate_->counters()->wasm_min_mem_pages_count()->AddSample(min_mem_pages); |
992 // TODO(wasm): re-enable counter for max_mem_pages when we use that field. | 1126 // TODO(wasm): re-enable counter for max_mem_pages when we use that field. |
993 | 1127 |
994 if (!memory_.is_null()) { | 1128 if (!memory_.is_null()) { |
995 // Set externally passed ArrayBuffer non neuterable. | 1129 // Set externally passed ArrayBuffer non neuterable. |
996 memory_->set_is_neuterable(false); | 1130 memory_->set_is_neuterable(false); |
997 } else if (min_mem_pages > 0) { | 1131 } else if (min_mem_pages > 0) { |
998 memory_ = AllocateMemory(min_mem_pages); | 1132 memory_ = AllocateMemory(min_mem_pages); |
999 if (memory_.is_null()) return nothing; // failed to allocate memory | 1133 if (memory_.is_null()) return nothing; // failed to allocate memory |
1000 } | 1134 } |
1001 | 1135 |
1002 if (!memory_.is_null()) { | 1136 if (!memory_.is_null()) { |
1003 instance->SetInternalField(kWasmMemArrayBuffer, *memory_); | 1137 instance->SetInternalField(kWasmMemArrayBuffer, *memory_); |
1004 Address mem_start = static_cast<Address>(memory_->backing_store()); | 1138 Address mem_start = static_cast<Address>(memory_->backing_store()); |
1005 uint32_t mem_size = | 1139 uint32_t mem_size = |
1006 static_cast<uint32_t>(memory_->byte_length()->Number()); | 1140 static_cast<uint32_t>(memory_->byte_length()->Number()); |
1007 LoadDataSegments(globals, mem_start, mem_size); | 1141 LoadDataSegments(mem_start, mem_size); |
1008 | 1142 |
1009 uint32_t old_mem_size = compiled_module_->mem_size(); | 1143 uint32_t old_mem_size = compiled_module_->mem_size(); |
1010 Address old_mem_start = | 1144 Address old_mem_start = |
1011 compiled_module_->has_memory() | 1145 compiled_module_->has_memory() |
1012 ? static_cast<Address>( | 1146 ? static_cast<Address>( |
1013 compiled_module_->memory()->backing_store()) | 1147 compiled_module_->memory()->backing_store()) |
1014 : nullptr; | 1148 : nullptr; |
1015 RelocateMemoryReferencesInCode(code_table, old_mem_start, mem_start, | 1149 RelocateMemoryReferencesInCode(code_table, old_mem_start, mem_start, |
1016 old_mem_size, mem_size); | 1150 old_mem_size, mem_size); |
1017 compiled_module_->set_memory(memory_); | 1151 compiled_module_->set_memory(memory_); |
(...skipping 10 matching lines...) Expand all Loading... |
1028 if (code->kind() == Code::WASM_FUNCTION) { | 1162 if (code->kind() == Code::WASM_FUNCTION) { |
1029 Handle<FixedArray> deopt_data = factory->NewFixedArray(2, TENURED); | 1163 Handle<FixedArray> deopt_data = factory->NewFixedArray(2, TENURED); |
1030 deopt_data->set(0, *weak_link); | 1164 deopt_data->set(0, *weak_link); |
1031 deopt_data->set(1, Smi::FromInt(static_cast<int>(i))); | 1165 deopt_data->set(1, Smi::FromInt(static_cast<int>(i))); |
1032 deopt_data->set_length(2); | 1166 deopt_data->set_length(2); |
1033 code->set_deoptimization_data(*deopt_data); | 1167 code->set_deoptimization_data(*deopt_data); |
1034 } | 1168 } |
1035 } | 1169 } |
1036 | 1170 |
1037 //-------------------------------------------------------------------------- | 1171 //-------------------------------------------------------------------------- |
1038 // Set up the indirect function tables for the new instance. | |
1039 //-------------------------------------------------------------------------- | |
1040 int function_table_count = | |
1041 static_cast<int>(module_->function_tables.size()); | |
1042 std::vector<InitializedTable> inited_tables; | |
1043 inited_tables.reserve(module_->function_tables.size()); | |
1044 if (function_table_count > 0) { | |
1045 Handle<FixedArray> old_function_tables = | |
1046 compiled_module_->function_tables(); | |
1047 Handle<FixedArray> new_function_tables = | |
1048 factory->NewFixedArray(function_table_count); | |
1049 for (int index = 0; index < function_table_count; ++index) { | |
1050 WasmIndirectFunctionTable& table = module_->function_tables[index]; | |
1051 uint32_t table_size = table.size; | |
1052 Handle<FixedArray> new_table = factory->NewFixedArray(table_size * 2); | |
1053 | |
1054 inited_tables.push_back({Handle<JSObject>::null(), | |
1055 Handle<FixedArray>::null(), new_table, | |
1056 std::vector<WasmFunction*>()}); | |
1057 InitializedTable& init_table = inited_tables.back(); | |
1058 if (table.exported) { | |
1059 init_table.new_entries.insert(init_table.new_entries.begin(), | |
1060 table_size, nullptr); | |
1061 } | |
1062 | |
1063 for (int i = 0; i < new_table->length(); ++i) { | |
1064 static const int kInvalidSigIndex = -1; | |
1065 // Fill the table with invalid signature indexes so that uninitialized | |
1066 // entries will always fail the signature check. | |
1067 new_table->set(i, Smi::FromInt(kInvalidSigIndex)); | |
1068 } | |
1069 for (auto table_init : module_->table_inits) { | |
1070 uint32_t base = EvalUint32InitExpr(globals, table_init.offset); | |
1071 if (base > table_size || | |
1072 (base + table_init.entries.size() > table_size)) { | |
1073 thrower_->CompileError("table initializer is out of bounds"); | |
1074 continue; | |
1075 } | |
1076 for (size_t i = 0; i < table_init.entries.size(); ++i) { | |
1077 WasmFunction* func = &module_->functions[table_init.entries[i]]; | |
1078 if (table.exported) { | |
1079 init_table.new_entries[i + base] = func; | |
1080 } | |
1081 FunctionSig* sig = func->sig; | |
1082 int32_t sig_index = table.map.Find(sig); | |
1083 new_table->set(static_cast<int>(i + base), Smi::FromInt(sig_index)); | |
1084 new_table->set(static_cast<int>(i + base + table_size), | |
1085 code_table->get(table_init.entries[i])); | |
1086 } | |
1087 } | |
1088 new_function_tables->set(static_cast<int>(index), *new_table); | |
1089 } | |
1090 // Patch all code that has references to the old indirect table. | |
1091 for (int i = 0; i < code_table->length(); ++i) { | |
1092 if (!code_table->get(i)->IsCode()) continue; | |
1093 Handle<Code> code(Code::cast(code_table->get(i)), isolate_); | |
1094 for (int j = 0; j < function_table_count; ++j) { | |
1095 ReplaceReferenceInCode( | |
1096 code, Handle<Object>(old_function_tables->get(j), isolate_), | |
1097 Handle<Object>(new_function_tables->get(j), isolate_)); | |
1098 } | |
1099 } | |
1100 compiled_module_->set_function_tables(new_function_tables); | |
1101 } | |
1102 | |
1103 //-------------------------------------------------------------------------- | |
1104 // Set up the exports object for the new instance. | 1172 // Set up the exports object for the new instance. |
1105 //-------------------------------------------------------------------------- | 1173 //-------------------------------------------------------------------------- |
1106 ProcessExports(globals, inited_tables, code_table, instance); | 1174 ProcessExports(code_table, instance); |
| 1175 |
| 1176 //-------------------------------------------------------------------------- |
| 1177 // Set up the indirect function tables for the new instance. |
| 1178 //-------------------------------------------------------------------------- |
| 1179 if (function_table_count > 0) InitializeTables(code_table, instance); |
1107 | 1180 |
1108 if (num_imported_functions > 0 || !owner.is_null()) { | 1181 if (num_imported_functions > 0 || !owner.is_null()) { |
1109 // If the code was cloned, or new imports were compiled, patch. | 1182 // If the code was cloned, or new imports were compiled, patch. |
1110 PatchDirectCalls(old_code_table, code_table, num_imported_functions); | 1183 PatchDirectCalls(old_code_table, code_table, num_imported_functions); |
1111 } | 1184 } |
1112 | 1185 |
1113 FlushICache(isolate_, code_table); | 1186 FlushICache(isolate_, code_table); |
1114 | 1187 |
1115 //-------------------------------------------------------------------------- | 1188 //-------------------------------------------------------------------------- |
1116 // Set up and link the new instance. | 1189 // Set up and link the new instance. |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1185 } | 1258 } |
1186 | 1259 |
1187 DCHECK(!isolate_->has_pending_exception()); | 1260 DCHECK(!isolate_->has_pending_exception()); |
1188 TRACE("Finishing instance %d\n", compiled_module_->instance_id()); | 1261 TRACE("Finishing instance %d\n", compiled_module_->instance_id()); |
1189 TRACE_CHAIN(WasmCompiledModule::cast(module_object_->GetInternalField(0))); | 1262 TRACE_CHAIN(WasmCompiledModule::cast(module_object_->GetInternalField(0))); |
1190 return instance; | 1263 return instance; |
1191 } | 1264 } |
1192 | 1265 |
1193 private: | 1266 private: |
1194 // Represents the initialized state of a table. | 1267 // Represents the initialized state of a table. |
1195 struct InitializedTable { | 1268 struct TableInstance { |
1196 Handle<JSObject> table_object; // WebAssembly.Table instance | 1269 Handle<JSObject> table_object; // WebAssembly.Table instance |
1197 Handle<FixedArray> js_functions; // JSFunctions exported | 1270 Handle<FixedArray> js_wrappers; // JSFunctions exported |
1198 Handle<FixedArray> dispatch_table; // internal (code, sig) pairs | 1271 Handle<FixedArray> dispatch_table; // internal (code, sig) pairs |
1199 std::vector<WasmFunction*> new_entries; // overwriting entries | |
1200 }; | 1272 }; |
1201 | 1273 |
1202 Isolate* isolate_; | 1274 Isolate* isolate_; |
1203 WasmModule* module_; | 1275 WasmModule* module_; |
1204 ErrorThrower* thrower_; | 1276 ErrorThrower* thrower_; |
1205 Handle<JSObject> module_object_; | 1277 Handle<JSObject> module_object_; |
1206 Handle<JSReceiver> ffi_; | 1278 Handle<JSReceiver> ffi_; |
1207 Handle<JSArrayBuffer> memory_; | 1279 Handle<JSArrayBuffer> memory_; |
| 1280 Handle<JSArrayBuffer> globals_; |
1208 Handle<WasmCompiledModule> compiled_module_; | 1281 Handle<WasmCompiledModule> compiled_module_; |
| 1282 std::vector<TableInstance> table_instances_; |
| 1283 std::vector<Handle<JSFunction>> js_wrappers_; |
1209 | 1284 |
1210 // Helper routine to print out errors with imports (FFI). | 1285 // Helper routine to print out errors with imports (FFI). |
1211 MaybeHandle<JSFunction> ReportFFIError(const char* error, uint32_t index, | 1286 MaybeHandle<JSFunction> ReportFFIError(const char* error, uint32_t index, |
1212 Handle<String> module_name, | 1287 Handle<String> module_name, |
1213 MaybeHandle<String> function_name) { | 1288 MaybeHandle<String> function_name) { |
1214 Handle<String> function_name_handle; | 1289 Handle<String> function_name_handle; |
1215 if (function_name.ToHandle(&function_name_handle)) { | 1290 if (function_name.ToHandle(&function_name_handle)) { |
1216 thrower_->TypeError( | 1291 thrower_->TypeError( |
1217 "Import #%d module=\"%.*s\" function=\"%.*s\" error: %s", index, | 1292 "Import #%d module=\"%.*s\" function=\"%.*s\" error: %s", index, |
1218 module_name->length(), module_name->ToCString().get(), | 1293 module_name->length(), module_name->ToCString().get(), |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1257 import_name); | 1332 import_name); |
1258 } | 1333 } |
1259 } else { | 1334 } else { |
1260 // No function specified. Use the "default export". | 1335 // No function specified. Use the "default export". |
1261 result = module; | 1336 result = module; |
1262 } | 1337 } |
1263 | 1338 |
1264 return result; | 1339 return result; |
1265 } | 1340 } |
1266 | 1341 |
1267 uint32_t EvalUint32InitExpr(MaybeHandle<JSArrayBuffer> globals, | 1342 uint32_t EvalUint32InitExpr(WasmInitExpr& expr) { |
1268 WasmInitExpr& expr) { | |
1269 switch (expr.kind) { | 1343 switch (expr.kind) { |
1270 case WasmInitExpr::kI32Const: | 1344 case WasmInitExpr::kI32Const: |
1271 return expr.val.i32_const; | 1345 return expr.val.i32_const; |
1272 case WasmInitExpr::kGlobalIndex: { | 1346 case WasmInitExpr::kGlobalIndex: { |
1273 uint32_t offset = module_->globals[expr.val.global_index].offset; | 1347 uint32_t offset = module_->globals[expr.val.global_index].offset; |
1274 return *reinterpret_cast<uint32_t*>(raw_buffer_ptr(globals, offset)); | 1348 return *reinterpret_cast<uint32_t*>(raw_buffer_ptr(globals_, offset)); |
1275 } | 1349 } |
1276 default: | 1350 default: |
1277 UNREACHABLE(); | 1351 UNREACHABLE(); |
1278 return 0; | 1352 return 0; |
1279 } | 1353 } |
1280 } | 1354 } |
1281 | 1355 |
1282 // Load data segments into the memory. | 1356 // Load data segments into the memory. |
1283 void LoadDataSegments(MaybeHandle<JSArrayBuffer> globals, Address mem_addr, | 1357 void LoadDataSegments(Address mem_addr, size_t mem_size) { |
1284 size_t mem_size) { | |
1285 Handle<SeqOneByteString> module_bytes = compiled_module_->module_bytes(); | 1358 Handle<SeqOneByteString> module_bytes = compiled_module_->module_bytes(); |
1286 for (auto segment : module_->data_segments) { | 1359 for (auto segment : module_->data_segments) { |
1287 uint32_t dest_offset = EvalUint32InitExpr(globals, segment.dest_addr); | 1360 uint32_t dest_offset = EvalUint32InitExpr(segment.dest_addr); |
1288 uint32_t source_size = segment.source_size; | 1361 uint32_t source_size = segment.source_size; |
1289 if (dest_offset >= mem_size || source_size >= mem_size || | 1362 if (dest_offset >= mem_size || source_size >= mem_size || |
1290 dest_offset >= (mem_size - source_size)) { | 1363 dest_offset >= (mem_size - source_size)) { |
1291 thrower_->RangeError("data segment does not fit into memory"); | 1364 thrower_->RangeError("data segment does not fit into memory"); |
1292 } | 1365 } |
1293 byte* dest = mem_addr + dest_offset; | 1366 byte* dest = mem_addr + dest_offset; |
1294 const byte* src = reinterpret_cast<const byte*>( | 1367 const byte* src = reinterpret_cast<const byte*>( |
1295 module_bytes->GetCharsAddress() + segment.source_offset); | 1368 module_bytes->GetCharsAddress() + segment.source_offset); |
1296 memcpy(dest, src, source_size); | 1369 memcpy(dest, src, source_size); |
1297 } | 1370 } |
1298 } | 1371 } |
1299 | 1372 |
1300 Handle<Code> CompileImportWrapper(int index, const WasmImport& import, | 1373 void WriteGlobalValue(WasmGlobal& global, Handle<Object> value) { |
1301 Handle<JSReceiver> target, | |
1302 Handle<String> module_name, | |
1303 MaybeHandle<String> import_name) { | |
1304 FunctionSig* sig = module_->functions[import.index].sig; | |
1305 Handle<Code> code; | |
1306 bool is_match = false; | |
1307 Handle<Code> export_wrapper_code; | |
1308 if (target->IsJSFunction()) { | |
1309 Handle<JSFunction> func = Handle<JSFunction>::cast(target); | |
1310 export_wrapper_code = handle(func->code()); | |
1311 if (export_wrapper_code->kind() == Code::JS_TO_WASM_FUNCTION) { | |
1312 // Compare signature of other exported wasm function. | |
1313 Handle<JSObject> other_instance( | |
1314 JSObject::cast(func->GetInternalField(kInternalModuleInstance)), | |
1315 isolate_); | |
1316 int func_index = | |
1317 Smi::cast(func->GetInternalField(kInternalFunctionIndex))->value(); | |
1318 FunctionSig* other_sig = | |
1319 GetCppModule(other_instance)->functions[func_index].sig; | |
1320 is_match = sig->Equals(other_sig); | |
1321 } | |
1322 } | |
1323 if (is_match) { | |
1324 // Signature matched. Unwrap the JS->WASM wrapper and return the naked | |
1325 // WASM function code. | |
1326 int wasm_count = 0; | |
1327 int const mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET); | |
1328 for (RelocIterator it(*export_wrapper_code, mask); !it.done(); | |
1329 it.next()) { | |
1330 RelocInfo* rinfo = it.rinfo(); | |
1331 Address target_address = rinfo->target_address(); | |
1332 Code* target = Code::GetCodeFromTargetAddress(target_address); | |
1333 if (target->kind() == Code::WASM_FUNCTION) { | |
1334 ++wasm_count; | |
1335 code = handle(target); | |
1336 } | |
1337 } | |
1338 DCHECK(wasm_count == 1); | |
1339 return code; | |
1340 } else { | |
1341 // Signature mismatch. Compile a new wrapper for the new signature. | |
1342 return compiler::CompileWasmToJSWrapper(isolate_, target, sig, index, | |
1343 module_name, import_name); | |
1344 } | |
1345 } | |
1346 | |
1347 void WriteGlobalValue(WasmGlobal& global, MaybeHandle<JSArrayBuffer> globals, | |
1348 Handle<Object> value) { | |
1349 double num = 0; | 1374 double num = 0; |
1350 if (value->IsSmi()) { | 1375 if (value->IsSmi()) { |
1351 num = Smi::cast(*value)->value(); | 1376 num = Smi::cast(*value)->value(); |
1352 } else if (value->IsHeapNumber()) { | 1377 } else if (value->IsHeapNumber()) { |
1353 num = HeapNumber::cast(*value)->value(); | 1378 num = HeapNumber::cast(*value)->value(); |
1354 } else { | 1379 } else { |
1355 UNREACHABLE(); | 1380 UNREACHABLE(); |
1356 } | 1381 } |
1357 TRACE("init [globals+%u] = %lf, type = %s\n", global.offset, num, | 1382 TRACE("init [globals+%u] = %lf, type = %s\n", global.offset, num, |
1358 WasmOpcodes::TypeName(global.type)); | 1383 WasmOpcodes::TypeName(global.type)); |
1359 switch (global.type) { | 1384 switch (global.type) { |
1360 case kAstI32: | 1385 case kAstI32: |
1361 *GetRawGlobalPtr<int32_t>(global, globals) = static_cast<int32_t>(num); | 1386 *GetRawGlobalPtr<int32_t>(global) = static_cast<int32_t>(num); |
1362 break; | 1387 break; |
1363 case kAstI64: | 1388 case kAstI64: |
1364 // TODO(titzer): initialization of imported i64 globals. | 1389 // TODO(titzer): initialization of imported i64 globals. |
1365 UNREACHABLE(); | 1390 UNREACHABLE(); |
1366 break; | 1391 break; |
1367 case kAstF32: | 1392 case kAstF32: |
1368 *GetRawGlobalPtr<float>(global, globals) = static_cast<float>(num); | 1393 *GetRawGlobalPtr<float>(global) = static_cast<float>(num); |
1369 break; | 1394 break; |
1370 case kAstF64: | 1395 case kAstF64: |
1371 *GetRawGlobalPtr<double>(global, globals) = static_cast<double>(num); | 1396 *GetRawGlobalPtr<double>(global) = static_cast<double>(num); |
1372 break; | 1397 break; |
1373 default: | 1398 default: |
1374 UNREACHABLE(); | 1399 UNREACHABLE(); |
1375 } | 1400 } |
1376 } | 1401 } |
1377 | 1402 |
1378 // Process the imports, including functions, tables, globals, and memory, in | 1403 // Process the imports, including functions, tables, globals, and memory, in |
1379 // order, loading them from the {ffi_} object. Returns the number of imported | 1404 // order, loading them from the {ffi_} object. Returns the number of imported |
1380 // functions. | 1405 // functions. |
1381 int ProcessImports(MaybeHandle<JSArrayBuffer> globals, | 1406 int ProcessImports(Handle<FixedArray> code_table, Handle<JSObject> instance) { |
1382 Handle<FixedArray> code_table, Handle<JSObject> instance) { | |
1383 int num_imported_functions = 0; | 1407 int num_imported_functions = 0; |
| 1408 int num_imported_tables = 0; |
1384 for (int index = 0; index < static_cast<int>(module_->import_table.size()); | 1409 for (int index = 0; index < static_cast<int>(module_->import_table.size()); |
1385 ++index) { | 1410 ++index) { |
1386 WasmImport& import = module_->import_table[index]; | 1411 WasmImport& import = module_->import_table[index]; |
1387 Handle<String> module_name = | 1412 Handle<String> module_name = |
1388 ExtractStringFromModuleBytes(isolate_, compiled_module_, | 1413 ExtractStringFromModuleBytes(isolate_, compiled_module_, |
1389 import.module_name_offset, | 1414 import.module_name_offset, |
1390 import.module_name_length) | 1415 import.module_name_length) |
1391 .ToHandleChecked(); | 1416 .ToHandleChecked(); |
1392 Handle<String> function_name = Handle<String>::null(); | 1417 Handle<String> function_name = Handle<String>::null(); |
1393 if (import.field_name_length > 0) { | 1418 if (import.field_name_length > 0) { |
(...skipping 11 matching lines...) Expand all Loading... |
1405 case kExternalFunction: { | 1430 case kExternalFunction: { |
1406 // Function imports must be callable. | 1431 // Function imports must be callable. |
1407 Handle<Object> function = result.ToHandleChecked(); | 1432 Handle<Object> function = result.ToHandleChecked(); |
1408 if (!function->IsCallable()) { | 1433 if (!function->IsCallable()) { |
1409 ReportFFIError("function import requires a callable", index, | 1434 ReportFFIError("function import requires a callable", index, |
1410 module_name, function_name); | 1435 module_name, function_name); |
1411 return -1; | 1436 return -1; |
1412 } | 1437 } |
1413 | 1438 |
1414 Handle<Code> import_wrapper = CompileImportWrapper( | 1439 Handle<Code> import_wrapper = CompileImportWrapper( |
1415 index, import, Handle<JSReceiver>::cast(function), module_name, | 1440 isolate_, index, module_->functions[import.index].sig, |
1416 function_name); | 1441 Handle<JSReceiver>::cast(function), module_name, function_name); |
1417 code_table->set(num_imported_functions, *import_wrapper); | 1442 code_table->set(num_imported_functions, *import_wrapper); |
1418 RecordStats(isolate_, *import_wrapper); | 1443 RecordStats(isolate_, *import_wrapper); |
1419 num_imported_functions++; | 1444 num_imported_functions++; |
1420 break; | 1445 break; |
1421 } | 1446 } |
1422 case kExternalTable: | 1447 case kExternalTable: { |
1423 // TODO(titzer): Table imports must be a WebAssembly.Table. | 1448 Handle<Object> value = result.ToHandleChecked(); |
| 1449 if (!WasmJs::IsWasmTableObject(isolate_, value)) { |
| 1450 ReportFFIError("table import requires a WebAssembly.Table", index, |
| 1451 module_name, function_name); |
| 1452 return -1; |
| 1453 } |
| 1454 WasmIndirectFunctionTable& table = |
| 1455 module_->function_tables[num_imported_tables]; |
| 1456 TableInstance& table_instance = table_instances_[num_imported_tables]; |
| 1457 table_instance.table_object = Handle<JSObject>::cast(value); |
| 1458 table_instance.js_wrappers = WasmJs::GetWasmTableFunctions( |
| 1459 isolate_, table_instance.table_object); |
| 1460 |
| 1461 // TODO(titzer): import table size must match exactly for now. |
| 1462 int table_size = table_instance.js_wrappers->length(); |
| 1463 if (table_size != table.min_size) { |
| 1464 thrower_->TypeError( |
| 1465 "table import %d is wrong size (%d), expected %u", index, |
| 1466 table_size, table.min_size); |
| 1467 return -1; |
| 1468 } |
| 1469 |
| 1470 // Allocate a new dispatch table. |
| 1471 table_instance.dispatch_table = |
| 1472 isolate_->factory()->NewFixedArray(table_size * 2); |
| 1473 for (int i = 0; i < table_size * 2; ++i) { |
| 1474 table_instance.dispatch_table->set(i, |
| 1475 Smi::FromInt(kInvalidSigIndex)); |
| 1476 } |
| 1477 // Initialize the dispatch table with the (foreign) JS functions |
| 1478 // that are already in the table. |
| 1479 for (int i = 0; i < table_size; ++i) { |
| 1480 Handle<Object> val(table_instance.js_wrappers->get(i), isolate_); |
| 1481 if (!val->IsJSFunction()) continue; |
| 1482 WasmFunction* function = |
| 1483 GetWasmFunctionForImportWrapper(isolate_, val); |
| 1484 if (function == nullptr) { |
| 1485 thrower_->TypeError("table import %d[%d] is not a WASM function", |
| 1486 index, i); |
| 1487 return -1; |
| 1488 } |
| 1489 int sig_index = table.map.FindOrInsert(function->sig); |
| 1490 table_instance.dispatch_table->set(i, Smi::FromInt(sig_index)); |
| 1491 table_instance.dispatch_table->set(i + table_size, |
| 1492 *UnwrapImportWrapper(val)); |
| 1493 } |
| 1494 |
| 1495 num_imported_tables++; |
1424 break; | 1496 break; |
| 1497 } |
1425 case kExternalMemory: { | 1498 case kExternalMemory: { |
1426 Handle<Object> object = result.ToHandleChecked(); | 1499 Handle<Object> object = result.ToHandleChecked(); |
1427 if (!WasmJs::IsWasmMemoryObject(isolate_, object)) { | 1500 if (!WasmJs::IsWasmMemoryObject(isolate_, object)) { |
1428 ReportFFIError("memory import must be a WebAssembly.Memory object", | 1501 ReportFFIError("memory import must be a WebAssembly.Memory object", |
1429 index, module_name, function_name); | 1502 index, module_name, function_name); |
1430 return -1; | 1503 return -1; |
1431 } | 1504 } |
1432 instance->SetInternalField(kWasmMemObject, *object); | 1505 instance->SetInternalField(kWasmMemObject, *object); |
1433 memory_ = WasmJs::GetWasmMemoryArrayBuffer(isolate_, object); | 1506 memory_ = WasmJs::GetWasmMemoryArrayBuffer(isolate_, object); |
1434 break; | 1507 break; |
1435 } | 1508 } |
1436 case kExternalGlobal: { | 1509 case kExternalGlobal: { |
1437 // Global imports are converted to numbers and written into the | 1510 // Global imports are converted to numbers and written into the |
1438 // {globals} array buffer. | 1511 // {globals_} array buffer. |
1439 Handle<Object> object = result.ToHandleChecked(); | 1512 Handle<Object> object = result.ToHandleChecked(); |
1440 MaybeHandle<Object> number = Object::ToNumber(object); | 1513 MaybeHandle<Object> number = Object::ToNumber(object); |
1441 if (number.is_null()) { | 1514 if (number.is_null()) { |
1442 ReportFFIError("global import could not be converted to number", | 1515 ReportFFIError("global import could not be converted to number", |
1443 index, module_name, function_name); | 1516 index, module_name, function_name); |
1444 return -1; | 1517 return -1; |
1445 } | 1518 } |
1446 Handle<Object> val = number.ToHandleChecked(); | 1519 Handle<Object> val = number.ToHandleChecked(); |
1447 WriteGlobalValue(module_->globals[import.index], globals, val); | 1520 WriteGlobalValue(module_->globals[import.index], val); |
1448 break; | 1521 break; |
1449 } | 1522 } |
1450 default: | 1523 default: |
1451 UNREACHABLE(); | 1524 UNREACHABLE(); |
1452 break; | 1525 break; |
1453 } | 1526 } |
1454 } | 1527 } |
1455 return num_imported_functions; | 1528 return num_imported_functions; |
1456 } | 1529 } |
1457 | 1530 |
1458 template <typename T> | 1531 template <typename T> |
1459 T* GetRawGlobalPtr(WasmGlobal& global, MaybeHandle<JSArrayBuffer> globals) { | 1532 T* GetRawGlobalPtr(WasmGlobal& global) { |
1460 return reinterpret_cast<T*>(raw_buffer_ptr(globals, global.offset)); | 1533 return reinterpret_cast<T*>(raw_buffer_ptr(globals_, global.offset)); |
1461 } | 1534 } |
1462 | 1535 |
1463 // Process initialization of globals. | 1536 // Process initialization of globals. |
1464 void InitGlobals(MaybeHandle<JSArrayBuffer> globals) { | 1537 void InitGlobals() { |
1465 for (auto global : module_->globals) { | 1538 for (auto global : module_->globals) { |
1466 switch (global.init.kind) { | 1539 switch (global.init.kind) { |
1467 case WasmInitExpr::kI32Const: | 1540 case WasmInitExpr::kI32Const: |
1468 *GetRawGlobalPtr<int32_t>(global, globals) = | 1541 *GetRawGlobalPtr<int32_t>(global) = global.init.val.i32_const; |
1469 global.init.val.i32_const; | |
1470 break; | 1542 break; |
1471 case WasmInitExpr::kI64Const: | 1543 case WasmInitExpr::kI64Const: |
1472 *GetRawGlobalPtr<int64_t>(global, globals) = | 1544 *GetRawGlobalPtr<int64_t>(global) = global.init.val.i64_const; |
1473 global.init.val.i64_const; | |
1474 break; | 1545 break; |
1475 case WasmInitExpr::kF32Const: | 1546 case WasmInitExpr::kF32Const: |
1476 *GetRawGlobalPtr<float>(global, globals) = global.init.val.f32_const; | 1547 *GetRawGlobalPtr<float>(global) = global.init.val.f32_const; |
1477 break; | 1548 break; |
1478 case WasmInitExpr::kF64Const: | 1549 case WasmInitExpr::kF64Const: |
1479 *GetRawGlobalPtr<double>(global, globals) = global.init.val.f64_const; | 1550 *GetRawGlobalPtr<double>(global) = global.init.val.f64_const; |
1480 break; | 1551 break; |
1481 case WasmInitExpr::kGlobalIndex: { | 1552 case WasmInitExpr::kGlobalIndex: { |
1482 // Initialize with another global. | 1553 // Initialize with another global. |
1483 uint32_t new_offset = global.offset; | 1554 uint32_t new_offset = global.offset; |
1484 uint32_t old_offset = | 1555 uint32_t old_offset = |
1485 module_->globals[global.init.val.global_index].offset; | 1556 module_->globals[global.init.val.global_index].offset; |
1486 TRACE("init [globals+%u] = [globals+%d]\n", global.offset, | 1557 TRACE("init [globals+%u] = [globals+%d]\n", global.offset, |
1487 old_offset); | 1558 old_offset); |
1488 size_t size = (global.type == kAstI64 || global.type == kAstF64) | 1559 size_t size = (global.type == kAstI64 || global.type == kAstF64) |
1489 ? sizeof(double) | 1560 ? sizeof(double) |
1490 : sizeof(int32_t); | 1561 : sizeof(int32_t); |
1491 memcpy(raw_buffer_ptr(globals, new_offset), | 1562 memcpy(raw_buffer_ptr(globals_, new_offset), |
1492 raw_buffer_ptr(globals, old_offset), size); | 1563 raw_buffer_ptr(globals_, old_offset), size); |
1493 break; | 1564 break; |
1494 } | 1565 } |
1495 case WasmInitExpr::kNone: | 1566 case WasmInitExpr::kNone: |
1496 // Happens with imported globals. | 1567 // Happens with imported globals. |
1497 break; | 1568 break; |
1498 default: | 1569 default: |
1499 UNREACHABLE(); | 1570 UNREACHABLE(); |
1500 break; | 1571 break; |
1501 } | 1572 } |
1502 } | 1573 } |
1503 } | 1574 } |
1504 | 1575 |
1505 // Allocate memory for a module instance as a new JSArrayBuffer. | 1576 // Allocate memory for a module instance as a new JSArrayBuffer. |
1506 Handle<JSArrayBuffer> AllocateMemory(uint32_t min_mem_pages) { | 1577 Handle<JSArrayBuffer> AllocateMemory(uint32_t min_mem_pages) { |
1507 if (min_mem_pages > WasmModule::kMaxMemPages) { | 1578 if (min_mem_pages > WasmModule::kV8MaxPages) { |
1508 thrower_->RangeError("Out of memory: wasm memory too large"); | 1579 thrower_->RangeError("Out of memory: wasm memory too large"); |
1509 return Handle<JSArrayBuffer>::null(); | 1580 return Handle<JSArrayBuffer>::null(); |
1510 } | 1581 } |
1511 Handle<JSArrayBuffer> mem_buffer = | 1582 Handle<JSArrayBuffer> mem_buffer = |
1512 NewArrayBuffer(isolate_, min_mem_pages * WasmModule::kPageSize); | 1583 NewArrayBuffer(isolate_, min_mem_pages * WasmModule::kPageSize); |
1513 | 1584 |
1514 if (mem_buffer.is_null()) { | 1585 if (mem_buffer.is_null()) { |
1515 thrower_->RangeError("Out of memory: wasm memory"); | 1586 thrower_->RangeError("Out of memory: wasm memory"); |
1516 } | 1587 } |
1517 return mem_buffer; | 1588 return mem_buffer; |
1518 } | 1589 } |
1519 | 1590 |
1520 // Process the exports, creating wrappers for functions, tables, memories, | 1591 // Process the exports, creating wrappers for functions, tables, memories, |
1521 // and globals. | 1592 // and globals. |
1522 void ProcessExports(MaybeHandle<JSArrayBuffer> globals, | 1593 void ProcessExports(Handle<FixedArray> code_table, |
1523 std::vector<InitializedTable>& inited_tables, | |
1524 Handle<FixedArray> code_table, | |
1525 Handle<JSObject> instance) { | 1594 Handle<JSObject> instance) { |
1526 if (module_->export_table.size() == 0) return; | 1595 bool needs_wrappers = module_->num_exported_functions > 0; |
1527 | 1596 for (auto table_instance : table_instances_) { |
1528 // Allocate a table to cache the exported JSFunctions if needed. | 1597 if (!table_instance.js_wrappers.is_null()) { |
1529 bool has_exported_functions = module_->num_exported_functions > 0; | 1598 needs_wrappers = true; |
1530 if (!has_exported_functions) { | 1599 break; |
1531 for (auto table : module_->function_tables) { | |
1532 if (table.exported) { | |
1533 has_exported_functions = true; | |
1534 break; | |
1535 } | |
1536 } | 1600 } |
1537 } | 1601 } |
1538 Handle<FixedArray> exported_functions = | 1602 for (auto table : module_->function_tables) { |
1539 has_exported_functions | 1603 if (table.exported) { |
1540 ? isolate_->factory()->NewFixedArray( | 1604 needs_wrappers = true; |
1541 static_cast<int>(module_->functions.size())) | 1605 break; |
1542 : Handle<FixedArray>::null(); | 1606 } |
| 1607 } |
| 1608 if (needs_wrappers) { |
| 1609 // Fill the table to cache the exported JSFunction wrappers. |
| 1610 js_wrappers_.insert(js_wrappers_.begin(), module_->functions.size(), |
| 1611 Handle<JSFunction>::null()); |
| 1612 } |
1543 | 1613 |
1544 Handle<JSObject> exports_object = instance; | 1614 Handle<JSObject> exports_object = instance; |
1545 if (module_->origin == kWasmOrigin) { | 1615 if (module_->export_table.size() > 0 && module_->origin == kWasmOrigin) { |
1546 // Create the "exports" object. | 1616 // Create the "exports" object. |
1547 Handle<JSFunction> object_function = Handle<JSFunction>( | 1617 Handle<JSFunction> object_function = Handle<JSFunction>( |
1548 isolate_->native_context()->object_function(), isolate_); | 1618 isolate_->native_context()->object_function(), isolate_); |
1549 exports_object = | 1619 exports_object = |
1550 isolate_->factory()->NewJSObject(object_function, TENURED); | 1620 isolate_->factory()->NewJSObject(object_function, TENURED); |
1551 Handle<String> exports_name = | 1621 Handle<String> exports_name = |
1552 isolate_->factory()->InternalizeUtf8String("exports"); | 1622 isolate_->factory()->InternalizeUtf8String("exports"); |
1553 JSObject::AddProperty(instance, exports_name, exports_object, READ_ONLY); | 1623 JSObject::AddProperty(instance, exports_name, exports_object, READ_ONLY); |
1554 } | 1624 } |
1555 | 1625 |
1556 PropertyDescriptor desc; | 1626 PropertyDescriptor desc; |
1557 desc.set_writable(false); | 1627 desc.set_writable(false); |
1558 | 1628 |
| 1629 // Process each export in the export table. |
1559 int func_index = 0; | 1630 int func_index = 0; |
1560 for (auto exp : module_->export_table) { | 1631 for (auto exp : module_->export_table) { |
1561 Handle<String> name = | 1632 Handle<String> name = |
1562 ExtractStringFromModuleBytes(isolate_, compiled_module_, | 1633 ExtractStringFromModuleBytes(isolate_, compiled_module_, |
1563 exp.name_offset, exp.name_length) | 1634 exp.name_offset, exp.name_length) |
1564 .ToHandleChecked(); | 1635 .ToHandleChecked(); |
1565 switch (exp.kind) { | 1636 switch (exp.kind) { |
1566 case kExternalFunction: { | 1637 case kExternalFunction: { |
1567 // Wrap and export the code as a JSFunction. | 1638 // Wrap and export the code as a JSFunction. |
1568 WasmFunction& function = module_->functions[exp.index]; | 1639 WasmFunction& function = module_->functions[exp.index]; |
1569 int export_index = | 1640 int export_index = |
1570 static_cast<int>(module_->functions.size() + func_index); | 1641 static_cast<int>(module_->functions.size() + func_index); |
1571 Handle<Object> value(exported_functions->get(exp.index), isolate_); | 1642 Handle<JSFunction> js_function = js_wrappers_[exp.index]; |
1572 Handle<JSFunction> js_function; | 1643 if (js_function.is_null()) { |
1573 if (value->IsJSFunction()) { | |
1574 // There already is a JSFunction for this WASM function. | |
1575 js_function = Handle<JSFunction>::cast(value); | |
1576 } else { | |
1577 // Wrap the exported code as a JSFunction. | 1644 // Wrap the exported code as a JSFunction. |
1578 Handle<Code> export_code = | 1645 Handle<Code> export_code = |
1579 code_table->GetValueChecked<Code>(isolate_, export_index); | 1646 code_table->GetValueChecked<Code>(isolate_, export_index); |
1580 js_function = | 1647 js_function = |
1581 WrapExportCodeAsJSFunction(isolate_, export_code, name, | 1648 WrapExportCodeAsJSFunction(isolate_, export_code, name, |
1582 function.sig, func_index, instance); | 1649 function.sig, func_index, instance); |
1583 exported_functions->set(exp.index, *js_function); | 1650 js_wrappers_[exp.index] = js_function; |
1584 } | 1651 } |
1585 desc.set_value(js_function); | 1652 desc.set_value(js_function); |
1586 func_index++; | 1653 func_index++; |
1587 break; | 1654 break; |
1588 } | 1655 } |
1589 case kExternalTable: { | 1656 case kExternalTable: { |
1590 InitializedTable& init_table = inited_tables[exp.index]; | 1657 // Export a table as a WebAssembly.Table object. |
| 1658 TableInstance& table_instance = table_instances_[exp.index]; |
1591 WasmIndirectFunctionTable& table = | 1659 WasmIndirectFunctionTable& table = |
1592 module_->function_tables[exp.index]; | 1660 module_->function_tables[exp.index]; |
1593 if (init_table.table_object.is_null()) { | 1661 if (table_instance.table_object.is_null()) { |
1594 init_table.table_object = WasmJs::CreateWasmTableObject( | 1662 table_instance.table_object = WasmJs::CreateWasmTableObject( |
1595 isolate_, table.size, table.max_size != 0, table.max_size, | 1663 isolate_, table.min_size, table.has_max, table.max_size, |
1596 &init_table.js_functions); | 1664 &table_instance.js_wrappers); |
1597 } | 1665 } |
1598 desc.set_value(init_table.table_object); | 1666 desc.set_value(table_instance.table_object); |
1599 break; | 1667 break; |
1600 } | 1668 } |
1601 case kExternalMemory: { | 1669 case kExternalMemory: { |
1602 // Export the memory as a WebAssembly.Memory object. | 1670 // Export the memory as a WebAssembly.Memory object. |
1603 Handle<Object> memory_object( | 1671 Handle<Object> memory_object( |
1604 instance->GetInternalField(kWasmMemObject), isolate_); | 1672 instance->GetInternalField(kWasmMemObject), isolate_); |
1605 if (memory_object->IsUndefined(isolate_)) { | 1673 if (memory_object->IsUndefined(isolate_)) { |
1606 // If there was no imported WebAssembly.Memory object, create one. | 1674 // If there was no imported WebAssembly.Memory object, create one. |
1607 Handle<JSArrayBuffer> buffer( | 1675 Handle<JSArrayBuffer> buffer( |
1608 JSArrayBuffer::cast( | 1676 JSArrayBuffer::cast( |
1609 instance->GetInternalField(kWasmMemArrayBuffer)), | 1677 instance->GetInternalField(kWasmMemArrayBuffer)), |
1610 isolate_); | 1678 isolate_); |
1611 memory_object = | 1679 memory_object = |
1612 WasmJs::CreateWasmMemoryObject(isolate_, buffer, false, 0); | 1680 WasmJs::CreateWasmMemoryObject(isolate_, buffer, false, 0); |
1613 instance->SetInternalField(kWasmMemObject, *memory_object); | 1681 instance->SetInternalField(kWasmMemObject, *memory_object); |
1614 } | 1682 } |
1615 | 1683 |
1616 desc.set_value(memory_object); | 1684 desc.set_value(memory_object); |
1617 break; | 1685 break; |
1618 } | 1686 } |
1619 case kExternalGlobal: { | 1687 case kExternalGlobal: { |
1620 // Export the value of the global variable as a number. | 1688 // Export the value of the global variable as a number. |
1621 WasmGlobal& global = module_->globals[exp.index]; | 1689 WasmGlobal& global = module_->globals[exp.index]; |
1622 double num = 0; | 1690 double num = 0; |
1623 switch (global.type) { | 1691 switch (global.type) { |
1624 case kAstI32: | 1692 case kAstI32: |
1625 num = *GetRawGlobalPtr<int32_t>(global, globals); | 1693 num = *GetRawGlobalPtr<int32_t>(global); |
1626 break; | 1694 break; |
1627 case kAstF32: | 1695 case kAstF32: |
1628 num = *GetRawGlobalPtr<float>(global, globals); | 1696 num = *GetRawGlobalPtr<float>(global); |
1629 break; | 1697 break; |
1630 case kAstF64: | 1698 case kAstF64: |
1631 num = *GetRawGlobalPtr<double>(global, globals); | 1699 num = *GetRawGlobalPtr<double>(global); |
1632 break; | 1700 break; |
1633 default: | 1701 default: |
1634 UNREACHABLE(); | 1702 UNREACHABLE(); |
1635 } | 1703 } |
1636 desc.set_value(isolate_->factory()->NewNumber(num)); | 1704 desc.set_value(isolate_->factory()->NewNumber(num)); |
1637 break; | 1705 break; |
1638 } | 1706 } |
1639 default: | 1707 default: |
1640 UNREACHABLE(); | 1708 UNREACHABLE(); |
1641 break; | 1709 break; |
1642 } | 1710 } |
1643 | 1711 |
1644 v8::Maybe<bool> status = JSReceiver::DefineOwnProperty( | 1712 v8::Maybe<bool> status = JSReceiver::DefineOwnProperty( |
1645 isolate_, exports_object, name, &desc, Object::THROW_ON_ERROR); | 1713 isolate_, exports_object, name, &desc, Object::THROW_ON_ERROR); |
1646 if (!status.IsJust()) { | 1714 if (!status.IsJust()) { |
1647 thrower_->TypeError("export of %.*s failed.", name->length(), | 1715 thrower_->TypeError("export of %.*s failed.", name->length(), |
1648 name->ToCString().get()); | 1716 name->ToCString().get()); |
1649 return; | 1717 return; |
1650 } | 1718 } |
1651 } | 1719 } |
| 1720 } |
1652 | 1721 |
1653 // Fill out the contents of WebAssembly.Table instances. | 1722 void InitializeTables(Handle<FixedArray> code_table, |
1654 if (!exported_functions.is_null()) { | 1723 Handle<JSObject> instance) { |
1655 // TODO(titzer): We compile JS->WASM wrappers for any function that is a | 1724 Handle<FixedArray> old_function_tables = |
1656 // member of an exported indirect table that is not itself exported. This | 1725 compiled_module_->function_tables(); |
1657 // should be done at compile time and cached instead. | 1726 int function_table_count = |
1658 WasmInstance temp_instance(module_); | 1727 static_cast<int>(module_->function_tables.size()); |
1659 temp_instance.context = isolate_->native_context(); | 1728 Handle<FixedArray> new_function_tables = |
1660 temp_instance.mem_size = 0; | 1729 isolate_->factory()->NewFixedArray(function_table_count); |
1661 temp_instance.mem_start = nullptr; | 1730 for (int index = 0; index < function_table_count; ++index) { |
1662 temp_instance.globals_start = nullptr; | 1731 WasmIndirectFunctionTable& table = module_->function_tables[index]; |
| 1732 TableInstance& table_instance = table_instances_[index]; |
| 1733 int table_size = static_cast<int>(table.min_size); |
1663 | 1734 |
1664 ModuleEnv module_env; | 1735 if (table_instance.dispatch_table.is_null()) { |
1665 module_env.module = module_; | 1736 // Create a new dispatch table if necessary. |
1666 module_env.instance = &temp_instance; | 1737 table_instance.dispatch_table = |
1667 module_env.origin = module_->origin; | 1738 isolate_->factory()->NewFixedArray(table_size * 2); |
| 1739 for (int i = 0; i < table_size; ++i) { |
| 1740 // Fill the table with invalid signature indexes so that |
| 1741 // uninitialized entries will always fail the signature check. |
| 1742 table_instance.dispatch_table->set(i, Smi::FromInt(kInvalidSigIndex)); |
| 1743 } |
| 1744 } |
1668 | 1745 |
1669 // Fill the exported tables with JSFunctions. | 1746 new_function_tables->set(static_cast<int>(index), |
1670 for (auto inited_table : inited_tables) { | 1747 *table_instance.dispatch_table); |
1671 if (inited_table.js_functions.is_null()) continue; | 1748 |
1672 for (int i = 0; i < static_cast<int>(inited_table.new_entries.size()); | 1749 Handle<FixedArray> all_dispatch_tables; |
1673 i++) { | 1750 if (!table_instance.table_object.is_null()) { |
1674 if (inited_table.new_entries[i] == nullptr) continue; | 1751 // Get the existing dispatch table(s) with the WebAssembly.Table object. |
1675 int func_index = | 1752 all_dispatch_tables = WasmJs::AddWasmTableDispatchTable( |
1676 static_cast<int>(inited_table.new_entries[i]->func_index); | 1753 isolate_, table_instance.table_object, Handle<JSObject>::null(), |
1677 if (!exported_functions->get(func_index)->IsJSFunction()) { | 1754 index, Handle<FixedArray>::null()); |
1678 // No JSFunction entry yet exists for this function. Create one. | 1755 } |
| 1756 |
| 1757 // TODO(titzer): this does redundant work if there are multiple tables, |
| 1758 // since initializations are not sorted by table index. |
| 1759 for (auto table_init : module_->table_inits) { |
| 1760 uint32_t base = EvalUint32InitExpr(table_init.offset); |
| 1761 if (base > static_cast<uint32_t>(table_size) || |
| 1762 (base + table_init.entries.size() > |
| 1763 static_cast<uint32_t>(table_size))) { |
| 1764 thrower_->CompileError("table initializer is out of bounds"); |
| 1765 continue; |
| 1766 } |
| 1767 for (int i = 0; i < static_cast<int>(table_init.entries.size()); ++i) { |
| 1768 uint32_t func_index = table_init.entries[i]; |
| 1769 WasmFunction* function = &module_->functions[func_index]; |
| 1770 int table_index = static_cast<int>(i + base); |
| 1771 int32_t sig_index = table.map.Find(function->sig); |
| 1772 DCHECK_GE(sig_index, 0); |
| 1773 table_instance.dispatch_table->set(table_index, |
| 1774 Smi::FromInt(sig_index)); |
| 1775 table_instance.dispatch_table->set(table_index + table_size, |
| 1776 code_table->get(func_index)); |
| 1777 |
| 1778 if (!all_dispatch_tables.is_null()) { |
1679 Handle<Code> wasm_code(Code::cast(code_table->get(func_index)), | 1779 Handle<Code> wasm_code(Code::cast(code_table->get(func_index)), |
1680 isolate_); | 1780 isolate_); |
1681 Handle<Code> wrapper_code = compiler::CompileJSToWasmWrapper( | 1781 if (js_wrappers_[func_index].is_null()) { |
1682 isolate_, &module_env, wasm_code, func_index); | 1782 // No JSFunction entry yet exists for this function. Create one. |
1683 Handle<JSFunction> js_function = WrapExportCodeAsJSFunction( | 1783 // TODO(titzer): We compile JS->WASM wrappers for functions are |
1684 isolate_, wrapper_code, isolate_->factory()->empty_string(), | 1784 // not exported but are in an exported table. This should be done |
1685 module_->functions[func_index].sig, func_index, instance); | 1785 // at module compile time and cached instead. |
1686 exported_functions->set(func_index, *js_function); | 1786 WasmInstance temp_instance(module_); |
| 1787 temp_instance.context = isolate_->native_context(); |
| 1788 temp_instance.mem_size = 0; |
| 1789 temp_instance.mem_start = nullptr; |
| 1790 temp_instance.globals_start = nullptr; |
| 1791 |
| 1792 ModuleEnv module_env; |
| 1793 module_env.module = module_; |
| 1794 module_env.instance = &temp_instance; |
| 1795 module_env.origin = module_->origin; |
| 1796 |
| 1797 Handle<Code> wrapper_code = compiler::CompileJSToWasmWrapper( |
| 1798 isolate_, &module_env, wasm_code, func_index); |
| 1799 Handle<JSFunction> js_function = WrapExportCodeAsJSFunction( |
| 1800 isolate_, wrapper_code, isolate_->factory()->empty_string(), |
| 1801 function->sig, func_index, instance); |
| 1802 js_wrappers_[func_index] = js_function; |
| 1803 } |
| 1804 table_instance.js_wrappers->set(table_index, |
| 1805 *js_wrappers_[func_index]); |
| 1806 |
| 1807 UpdateDispatchTablesInternal(isolate_, all_dispatch_tables, |
| 1808 table_index, function, wasm_code); |
1687 } | 1809 } |
1688 inited_table.js_functions->set(i, | |
1689 exported_functions->get(func_index)); | |
1690 } | 1810 } |
1691 } | 1811 } |
| 1812 |
| 1813 // TODO(titzer): we add the new dispatch table at the end to avoid |
| 1814 // redundant work and also because the new instance is not yet fully |
| 1815 // initialized. |
| 1816 if (!table_instance.table_object.is_null()) { |
| 1817 // Add the new dispatch table to the WebAssembly.Table object. |
| 1818 all_dispatch_tables = WasmJs::AddWasmTableDispatchTable( |
| 1819 isolate_, table_instance.table_object, instance, index, |
| 1820 table_instance.dispatch_table); |
| 1821 } |
1692 } | 1822 } |
| 1823 // Patch all code that has references to the old indirect tables. |
| 1824 for (int i = 0; i < code_table->length(); ++i) { |
| 1825 if (!code_table->get(i)->IsCode()) continue; |
| 1826 Handle<Code> code(Code::cast(code_table->get(i)), isolate_); |
| 1827 for (int j = 0; j < function_table_count; ++j) { |
| 1828 ReplaceReferenceInCode( |
| 1829 code, Handle<Object>(old_function_tables->get(j), isolate_), |
| 1830 Handle<Object>(new_function_tables->get(j), isolate_)); |
| 1831 } |
| 1832 } |
| 1833 compiled_module_->set_function_tables(new_function_tables); |
1693 } | 1834 } |
1694 }; | 1835 }; |
1695 | 1836 |
1696 // Instantiates a WASM module, creating a WebAssembly.Instance from a | 1837 // Instantiates a WASM module, creating a WebAssembly.Instance from a |
1697 // WebAssembly.Module. | 1838 // WebAssembly.Module. |
1698 MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate, | 1839 MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate, |
1699 ErrorThrower* thrower, | 1840 ErrorThrower* thrower, |
1700 Handle<JSObject> wasm_module, | 1841 Handle<JSObject> wasm_module, |
1701 Handle<JSReceiver> ffi, | 1842 Handle<JSReceiver> ffi, |
1702 Handle<JSArrayBuffer> memory) { | 1843 Handle<JSArrayBuffer> memory) { |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1953 GetInstanceMemory(isolate, instance); | 2094 GetInstanceMemory(isolate, instance); |
1954 Handle<JSArrayBuffer> buffer; | 2095 Handle<JSArrayBuffer> buffer; |
1955 if (!maybe_mem_buffer.ToHandle(&buffer)) { | 2096 if (!maybe_mem_buffer.ToHandle(&buffer)) { |
1956 return 0; | 2097 return 0; |
1957 } else { | 2098 } else { |
1958 return buffer->byte_length()->Number() / WasmModule::kPageSize; | 2099 return buffer->byte_length()->Number() / WasmModule::kPageSize; |
1959 } | 2100 } |
1960 } | 2101 } |
1961 | 2102 |
1962 uint32_t GetMaxInstanceMemorySize(Isolate* isolate, Handle<JSObject> instance) { | 2103 uint32_t GetMaxInstanceMemorySize(Isolate* isolate, Handle<JSObject> instance) { |
1963 uint32_t max_pages = WasmModule::kMaxMemPages; | 2104 uint32_t max_pages = WasmModule::kV8MaxPages; |
1964 Handle<Object> memory_object(instance->GetInternalField(kWasmMemObject), | 2105 Handle<Object> memory_object(instance->GetInternalField(kWasmMemObject), |
1965 isolate); | 2106 isolate); |
1966 if (memory_object->IsUndefined(isolate)) return max_pages; | 2107 if (memory_object->IsUndefined(isolate)) return max_pages; |
1967 return WasmJs::GetWasmMemoryMaximumSize(isolate, memory_object); | 2108 return WasmJs::GetWasmMemoryMaximumSize(isolate, memory_object); |
1968 } | 2109 } |
1969 | 2110 |
1970 int32_t wasm::GrowInstanceMemory(Isolate* isolate, Handle<JSObject> instance, | 2111 int32_t wasm::GrowInstanceMemory(Isolate* isolate, Handle<JSObject> instance, |
1971 uint32_t pages) { | 2112 uint32_t pages) { |
1972 if (!IsWasmInstance(*instance)) return -1; | 2113 if (!IsWasmInstance(*instance)) return -1; |
1973 if (pages == 0) return GetInstanceMemorySize(isolate, instance); | 2114 if (pages == 0) return GetInstanceMemorySize(isolate, instance); |
1974 uint32_t max_pages = GetMaxInstanceMemorySize(isolate, instance); | 2115 uint32_t max_pages = GetMaxInstanceMemorySize(isolate, instance); |
1975 if (WasmModule::kMaxMemPages < max_pages) return -1; | 2116 if (WasmModule::kV8MaxPages < max_pages) return -1; |
1976 | 2117 |
1977 Address old_mem_start = nullptr; | 2118 Address old_mem_start = nullptr; |
1978 uint32_t old_size = 0, new_size = 0; | 2119 uint32_t old_size = 0, new_size = 0; |
1979 | 2120 |
1980 MaybeHandle<JSArrayBuffer> maybe_mem_buffer = | 2121 MaybeHandle<JSArrayBuffer> maybe_mem_buffer = |
1981 GetInstanceMemory(isolate, instance); | 2122 GetInstanceMemory(isolate, instance); |
1982 Handle<JSArrayBuffer> old_buffer; | 2123 Handle<JSArrayBuffer> old_buffer; |
1983 if (!maybe_mem_buffer.ToHandle(&old_buffer) || | 2124 if (!maybe_mem_buffer.ToHandle(&old_buffer) || |
1984 old_buffer->backing_store() == nullptr) { | 2125 old_buffer->backing_store() == nullptr) { |
1985 // If module object does not have linear memory associated with it, | 2126 // If module object does not have linear memory associated with it, |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2091 CHECK_NOT_NULL(result.val); | 2232 CHECK_NOT_NULL(result.val); |
2092 module = const_cast<WasmModule*>(result.val); | 2233 module = const_cast<WasmModule*>(result.val); |
2093 } | 2234 } |
2094 | 2235 |
2095 Handle<WasmModuleWrapper> module_wrapper = | 2236 Handle<WasmModuleWrapper> module_wrapper = |
2096 WasmModuleWrapper::New(isolate, module); | 2237 WasmModuleWrapper::New(isolate, module); |
2097 | 2238 |
2098 compiled_module->set_module_wrapper(module_wrapper); | 2239 compiled_module->set_module_wrapper(module_wrapper); |
2099 DCHECK(WasmCompiledModule::IsWasmCompiledModule(*compiled_module)); | 2240 DCHECK(WasmCompiledModule::IsWasmCompiledModule(*compiled_module)); |
2100 } | 2241 } |
OLD | NEW |