Chromium Code Reviews| 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 734 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 835 Handle<String> module_bytes_string = | 863 Handle<String> module_bytes_string = |
| 836 factory->NewStringFromOneByte(module_bytes_vec, TENURED) | 864 factory->NewStringFromOneByte(module_bytes_vec, TENURED) |
| 837 .ToHandleChecked(); | 865 .ToHandleChecked(); |
| 838 DCHECK(module_bytes_string->IsSeqOneByteString()); | 866 DCHECK(module_bytes_string->IsSeqOneByteString()); |
| 839 ret->set_module_bytes(Handle<SeqOneByteString>::cast(module_bytes_string)); | 867 ret->set_module_bytes(Handle<SeqOneByteString>::cast(module_bytes_string)); |
| 840 } | 868 } |
| 841 | 869 |
| 842 return ret; | 870 return ret; |
| 843 } | 871 } |
| 844 | 872 |
| 873 static WasmFunction* GetWasmFunctionForImportWrapper(Isolate* isolate, | |
| 874 Handle<Object> target) { | |
| 875 if (target->IsJSFunction()) { | |
| 876 Handle<JSFunction> func = Handle<JSFunction>::cast(target); | |
| 877 Handle<Code> export_wrapper_code = handle(func->code()); | |
| 878 if (export_wrapper_code->kind() == Code::JS_TO_WASM_FUNCTION) { | |
| 879 Handle<JSObject> other_instance( | |
| 880 JSObject::cast(func->GetInternalField(kInternalModuleInstance)), | |
| 881 isolate); | |
| 882 int func_index = | |
| 883 Smi::cast(func->GetInternalField(kInternalFunctionIndex))->value(); | |
| 884 return &GetCppModule(other_instance)->functions[func_index]; | |
| 885 } | |
| 886 } | |
| 887 return nullptr; | |
| 888 } | |
| 889 | |
| 890 static Handle<Code> UnwrapImportWrapper(Handle<Object> target) { | |
| 891 Handle<JSFunction> func = Handle<JSFunction>::cast(target); | |
| 892 Handle<Code> export_wrapper_code = handle(func->code()); | |
| 893 int found = 0; | |
| 894 int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET); | |
| 895 Handle<Code> code; | |
| 896 for (RelocIterator it(*export_wrapper_code, mask); !it.done(); it.next()) { | |
| 897 RelocInfo* rinfo = it.rinfo(); | |
| 898 Address target_address = rinfo->target_address(); | |
| 899 Code* target = Code::GetCodeFromTargetAddress(target_address); | |
| 900 if (target->kind() == Code::WASM_FUNCTION || | |
| 901 target->kind() == Code::WASM_TO_JS_FUNCTION) { | |
| 902 ++found; | |
| 903 code = handle(target); | |
| 904 } | |
| 905 } | |
| 906 DCHECK(found == 1); | |
| 907 return code; | |
| 908 } | |
| 909 | |
| 910 static void UpdateDispatchTablesInternal(Isolate* isolate, | |
| 911 Handle<FixedArray> dispatch_tables, | |
| 912 int index, WasmFunction* function, | |
| 913 Handle<Code> code) { | |
| 914 DCHECK_EQ(0, dispatch_tables->length() % 3); | |
| 915 for (int i = 0; i < dispatch_tables->length(); i += 3) { | |
| 916 Handle<Object> instance(dispatch_tables->get(i), isolate); | |
| 917 WasmModule* module = GetCppModule(Handle<JSObject>::cast(instance)); | |
| 918 int table_index = Smi::cast(dispatch_tables->get(i + 1))->value(); | |
| 919 Handle<FixedArray> dispatch_table( | |
| 920 FixedArray::cast(dispatch_tables->get(i + 2)), isolate); | |
| 921 if (function) { | |
| 922 // TODO(titzer): the signature might need to be copied to avoid | |
| 923 // a dangling pointer in the signature map. | |
| 924 int sig_index = static_cast<int>( | |
| 925 module->function_tables[table_index].map.FindOrInsert(function->sig)); | |
| 926 dispatch_table->set(index, Smi::FromInt(sig_index)); | |
| 927 dispatch_table->set(index + (dispatch_table->length() / 2), *code); | |
| 928 } else { | |
| 929 Code* code = nullptr; | |
| 930 dispatch_table->set(index, Smi::FromInt(-1)); | |
| 931 dispatch_table->set(index + (dispatch_table->length() / 2), code); | |
| 932 } | |
| 933 } | |
| 934 } | |
| 935 | |
| 936 void wasm::UpdateDispatchTables(Isolate* isolate, | |
| 937 Handle<FixedArray> dispatch_tables, int index, | |
| 938 Handle<JSFunction> function) { | |
| 939 if (function.is_null()) { | |
| 940 UpdateDispatchTablesInternal(isolate, dispatch_tables, index, nullptr, | |
| 941 Handle<Code>::null()); | |
| 942 } else { | |
| 943 UpdateDispatchTablesInternal( | |
| 944 isolate, dispatch_tables, index, | |
| 945 GetWasmFunctionForImportWrapper(isolate, function), | |
|
bradnelson
2016/10/28 16:59:01
Seems like CompileImportWrapper below could get us
titzer
2016/10/28 17:29:08
Done.
| |
| 946 UnwrapImportWrapper(function)); | |
| 947 } | |
| 948 } | |
| 949 | |
| 845 // A helper class to simplify instantiating a module from a compiled module. | 950 // A helper class to simplify instantiating a module from a compiled module. |
| 846 // It closes over the {Isolate}, the {ErrorThrower}, the {WasmCompiledModule}, | 951 // It closes over the {Isolate}, the {ErrorThrower}, the {WasmCompiledModule}, |
| 847 // etc. | 952 // etc. |
| 848 class WasmInstanceBuilder { | 953 class WasmInstanceBuilder { |
| 849 public: | 954 public: |
| 850 WasmInstanceBuilder(Isolate* isolate, ErrorThrower* thrower, | 955 WasmInstanceBuilder(Isolate* isolate, ErrorThrower* thrower, |
| 851 Handle<JSObject> module_object, Handle<JSReceiver> ffi, | 956 Handle<JSObject> module_object, Handle<JSReceiver> ffi, |
| 852 Handle<JSArrayBuffer> memory) | 957 Handle<JSArrayBuffer> memory) |
| 853 : isolate_(isolate), | 958 : isolate_(isolate), |
| 854 thrower_(thrower), | 959 thrower_(thrower), |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 943 JS_OBJECT_TYPE, | 1048 JS_OBJECT_TYPE, |
| 944 JSObject::kHeaderSize + kWasmInstanceInternalFieldCount * kPointerSize); | 1049 JSObject::kHeaderSize + kWasmInstanceInternalFieldCount * kPointerSize); |
| 945 Handle<JSObject> instance = factory->NewJSObjectFromMap(map, TENURED); | 1050 Handle<JSObject> instance = factory->NewJSObjectFromMap(map, TENURED); |
| 946 instance->SetInternalField(kWasmMemObject, | 1051 instance->SetInternalField(kWasmMemObject, |
| 947 isolate_->heap()->undefined_value()); | 1052 isolate_->heap()->undefined_value()); |
| 948 | 1053 |
| 949 //-------------------------------------------------------------------------- | 1054 //-------------------------------------------------------------------------- |
| 950 // Set up the globals for the new instance. | 1055 // Set up the globals for the new instance. |
| 951 //-------------------------------------------------------------------------- | 1056 //-------------------------------------------------------------------------- |
| 952 MaybeHandle<JSArrayBuffer> old_globals; | 1057 MaybeHandle<JSArrayBuffer> old_globals; |
| 953 MaybeHandle<JSArrayBuffer> globals; | |
| 954 uint32_t globals_size = module_->globals_size; | 1058 uint32_t globals_size = module_->globals_size; |
| 955 if (globals_size > 0) { | 1059 if (globals_size > 0) { |
| 956 Handle<JSArrayBuffer> global_buffer = | 1060 Handle<JSArrayBuffer> global_buffer = |
| 957 NewArrayBuffer(isolate_, globals_size); | 1061 NewArrayBuffer(isolate_, globals_size); |
| 958 globals = global_buffer; | 1062 globals_ = global_buffer; |
| 959 if (globals.is_null()) { | 1063 if (globals_.is_null()) { |
| 960 thrower_->RangeError("Out of memory: wasm globals"); | 1064 thrower_->RangeError("Out of memory: wasm globals"); |
| 961 return nothing; | 1065 return nothing; |
| 962 } | 1066 } |
| 963 Address old_address = owner.is_null() | 1067 Address old_address = owner.is_null() |
| 964 ? nullptr | 1068 ? nullptr |
| 965 : GetGlobalStartAddressFromCodeTemplate( | 1069 : GetGlobalStartAddressFromCodeTemplate( |
| 966 isolate_->heap()->undefined_value(), | 1070 isolate_->heap()->undefined_value(), |
| 967 JSObject::cast(*owner.ToHandleChecked())); | 1071 JSObject::cast(*owner.ToHandleChecked())); |
| 968 RelocateGlobals(code_table, old_address, | 1072 RelocateGlobals(code_table, old_address, |
| 969 static_cast<Address>(global_buffer->backing_store())); | 1073 static_cast<Address>(global_buffer->backing_store())); |
| 970 instance->SetInternalField(kWasmGlobalsArrayBuffer, *global_buffer); | 1074 instance->SetInternalField(kWasmGlobalsArrayBuffer, *global_buffer); |
| 971 } | 1075 } |
| 972 | 1076 |
| 973 //-------------------------------------------------------------------------- | 1077 //-------------------------------------------------------------------------- |
| 1078 // Prepare for initialization of function tables. | |
| 1079 //-------------------------------------------------------------------------- | |
| 1080 int function_table_count = | |
| 1081 static_cast<int>(module_->function_tables.size()); | |
| 1082 table_instances_.reserve(module_->function_tables.size()); | |
| 1083 for (int index = 0; index < function_table_count; ++index) { | |
| 1084 table_instances_.push_back({Handle<JSObject>::null(), | |
| 1085 Handle<FixedArray>::null(), | |
| 1086 Handle<FixedArray>::null()}); | |
| 1087 } | |
| 1088 | |
| 1089 //-------------------------------------------------------------------------- | |
| 974 // Process the imports for the module. | 1090 // Process the imports for the module. |
| 975 //-------------------------------------------------------------------------- | 1091 //-------------------------------------------------------------------------- |
| 976 int num_imported_functions = ProcessImports(globals, code_table, instance); | 1092 int num_imported_functions = ProcessImports(code_table, instance); |
| 977 if (num_imported_functions < 0) return nothing; | 1093 if (num_imported_functions < 0) return nothing; |
| 978 | 1094 |
| 979 //-------------------------------------------------------------------------- | 1095 //-------------------------------------------------------------------------- |
| 980 // Process the initialization for the module's globals. | 1096 // Process the initialization for the module's globals. |
| 981 //-------------------------------------------------------------------------- | 1097 //-------------------------------------------------------------------------- |
| 982 InitGlobals(globals); | 1098 InitGlobals(); |
| 983 | 1099 |
| 984 //-------------------------------------------------------------------------- | 1100 //-------------------------------------------------------------------------- |
| 985 // Set up the memory for the new instance. | 1101 // Set up the memory for the new instance. |
| 986 //-------------------------------------------------------------------------- | 1102 //-------------------------------------------------------------------------- |
| 987 MaybeHandle<JSArrayBuffer> old_memory; | 1103 MaybeHandle<JSArrayBuffer> old_memory; |
| 988 | 1104 |
| 989 uint32_t min_mem_pages = module_->min_mem_pages; | 1105 uint32_t min_mem_pages = module_->min_mem_pages; |
| 990 isolate_->counters()->wasm_min_mem_pages_count()->AddSample(min_mem_pages); | 1106 isolate_->counters()->wasm_min_mem_pages_count()->AddSample(min_mem_pages); |
| 991 // TODO(wasm): re-enable counter for max_mem_pages when we use that field. | 1107 // TODO(wasm): re-enable counter for max_mem_pages when we use that field. |
| 992 | 1108 |
| 993 if (!memory_.is_null()) { | 1109 if (!memory_.is_null()) { |
| 994 // Set externally passed ArrayBuffer non neuterable. | 1110 // Set externally passed ArrayBuffer non neuterable. |
| 995 memory_->set_is_neuterable(false); | 1111 memory_->set_is_neuterable(false); |
| 996 } else if (min_mem_pages > 0) { | 1112 } else if (min_mem_pages > 0) { |
| 997 memory_ = AllocateMemory(min_mem_pages); | 1113 memory_ = AllocateMemory(min_mem_pages); |
| 998 if (memory_.is_null()) return nothing; // failed to allocate memory | 1114 if (memory_.is_null()) return nothing; // failed to allocate memory |
| 999 } | 1115 } |
| 1000 | 1116 |
| 1001 if (!memory_.is_null()) { | 1117 if (!memory_.is_null()) { |
| 1002 instance->SetInternalField(kWasmMemArrayBuffer, *memory_); | 1118 instance->SetInternalField(kWasmMemArrayBuffer, *memory_); |
| 1003 Address mem_start = static_cast<Address>(memory_->backing_store()); | 1119 Address mem_start = static_cast<Address>(memory_->backing_store()); |
| 1004 uint32_t mem_size = | 1120 uint32_t mem_size = |
| 1005 static_cast<uint32_t>(memory_->byte_length()->Number()); | 1121 static_cast<uint32_t>(memory_->byte_length()->Number()); |
| 1006 LoadDataSegments(globals, mem_start, mem_size); | 1122 LoadDataSegments(mem_start, mem_size); |
| 1007 | 1123 |
| 1008 uint32_t old_mem_size = compiled_module_->mem_size(); | 1124 uint32_t old_mem_size = compiled_module_->mem_size(); |
| 1009 Address old_mem_start = | 1125 Address old_mem_start = |
| 1010 compiled_module_->has_memory() | 1126 compiled_module_->has_memory() |
| 1011 ? static_cast<Address>( | 1127 ? static_cast<Address>( |
| 1012 compiled_module_->memory()->backing_store()) | 1128 compiled_module_->memory()->backing_store()) |
| 1013 : nullptr; | 1129 : nullptr; |
| 1014 RelocateMemoryReferencesInCode(code_table, old_mem_start, mem_start, | 1130 RelocateMemoryReferencesInCode(code_table, old_mem_start, mem_start, |
| 1015 old_mem_size, mem_size); | 1131 old_mem_size, mem_size); |
| 1016 compiled_module_->set_memory(memory_); | 1132 compiled_module_->set_memory(memory_); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 1027 if (code->kind() == Code::WASM_FUNCTION) { | 1143 if (code->kind() == Code::WASM_FUNCTION) { |
| 1028 Handle<FixedArray> deopt_data = factory->NewFixedArray(2, TENURED); | 1144 Handle<FixedArray> deopt_data = factory->NewFixedArray(2, TENURED); |
| 1029 deopt_data->set(0, *weak_link); | 1145 deopt_data->set(0, *weak_link); |
| 1030 deopt_data->set(1, Smi::FromInt(static_cast<int>(i))); | 1146 deopt_data->set(1, Smi::FromInt(static_cast<int>(i))); |
| 1031 deopt_data->set_length(2); | 1147 deopt_data->set_length(2); |
| 1032 code->set_deoptimization_data(*deopt_data); | 1148 code->set_deoptimization_data(*deopt_data); |
| 1033 } | 1149 } |
| 1034 } | 1150 } |
| 1035 | 1151 |
| 1036 //-------------------------------------------------------------------------- | 1152 //-------------------------------------------------------------------------- |
| 1037 // Set up the indirect function tables for the new instance. | |
| 1038 //-------------------------------------------------------------------------- | |
| 1039 int function_table_count = | |
| 1040 static_cast<int>(module_->function_tables.size()); | |
| 1041 std::vector<InitializedTable> inited_tables; | |
| 1042 inited_tables.reserve(module_->function_tables.size()); | |
| 1043 if (function_table_count > 0) { | |
| 1044 Handle<FixedArray> old_function_tables = | |
| 1045 compiled_module_->function_tables(); | |
| 1046 Handle<FixedArray> new_function_tables = | |
| 1047 factory->NewFixedArray(function_table_count); | |
| 1048 for (int index = 0; index < function_table_count; ++index) { | |
| 1049 WasmIndirectFunctionTable& table = module_->function_tables[index]; | |
| 1050 uint32_t table_size = table.size; | |
| 1051 Handle<FixedArray> new_table = factory->NewFixedArray(table_size * 2); | |
| 1052 | |
| 1053 inited_tables.push_back({Handle<JSObject>::null(), | |
| 1054 Handle<FixedArray>::null(), new_table, | |
| 1055 std::vector<WasmFunction*>()}); | |
| 1056 InitializedTable& init_table = inited_tables.back(); | |
| 1057 if (table.exported) { | |
| 1058 init_table.new_entries.insert(init_table.new_entries.begin(), | |
| 1059 table_size, nullptr); | |
| 1060 } | |
| 1061 | |
| 1062 for (int i = 0; i < new_table->length(); ++i) { | |
| 1063 static const int kInvalidSigIndex = -1; | |
| 1064 // Fill the table with invalid signature indexes so that uninitialized | |
| 1065 // entries will always fail the signature check. | |
| 1066 new_table->set(i, Smi::FromInt(kInvalidSigIndex)); | |
| 1067 } | |
| 1068 for (auto table_init : module_->table_inits) { | |
| 1069 uint32_t base = EvalUint32InitExpr(globals, table_init.offset); | |
| 1070 if (base > table_size || | |
| 1071 (base + table_init.entries.size() > table_size)) { | |
| 1072 thrower_->CompileError("table initializer is out of bounds"); | |
| 1073 continue; | |
| 1074 } | |
| 1075 for (size_t i = 0; i < table_init.entries.size(); ++i) { | |
| 1076 WasmFunction* func = &module_->functions[table_init.entries[i]]; | |
| 1077 if (table.exported) { | |
| 1078 init_table.new_entries[i + base] = func; | |
| 1079 } | |
| 1080 FunctionSig* sig = func->sig; | |
| 1081 int32_t sig_index = table.map.Find(sig); | |
| 1082 new_table->set(static_cast<int>(i + base), Smi::FromInt(sig_index)); | |
| 1083 new_table->set(static_cast<int>(i + base + table_size), | |
| 1084 code_table->get(table_init.entries[i])); | |
| 1085 } | |
| 1086 } | |
| 1087 new_function_tables->set(static_cast<int>(index), *new_table); | |
| 1088 } | |
| 1089 // Patch all code that has references to the old indirect table. | |
| 1090 for (int i = 0; i < code_table->length(); ++i) { | |
| 1091 if (!code_table->get(i)->IsCode()) continue; | |
| 1092 Handle<Code> code(Code::cast(code_table->get(i)), isolate_); | |
| 1093 for (int j = 0; j < function_table_count; ++j) { | |
| 1094 ReplaceReferenceInCode( | |
| 1095 code, Handle<Object>(old_function_tables->get(j), isolate_), | |
| 1096 Handle<Object>(new_function_tables->get(j), isolate_)); | |
| 1097 } | |
| 1098 } | |
| 1099 compiled_module_->set_function_tables(new_function_tables); | |
| 1100 } | |
| 1101 | |
| 1102 //-------------------------------------------------------------------------- | |
| 1103 // Set up the exports object for the new instance. | 1153 // Set up the exports object for the new instance. |
| 1104 //-------------------------------------------------------------------------- | 1154 //-------------------------------------------------------------------------- |
| 1105 ProcessExports(globals, inited_tables, code_table, instance); | 1155 ProcessExports(code_table, instance); |
| 1156 | |
| 1157 //-------------------------------------------------------------------------- | |
| 1158 // Set up the indirect function tables for the new instance. | |
| 1159 //-------------------------------------------------------------------------- | |
| 1160 if (function_table_count > 0) InitializeTables(code_table, instance); | |
| 1106 | 1161 |
| 1107 if (num_imported_functions > 0 || !owner.is_null()) { | 1162 if (num_imported_functions > 0 || !owner.is_null()) { |
| 1108 // If the code was cloned, or new imports were compiled, patch. | 1163 // If the code was cloned, or new imports were compiled, patch. |
| 1109 PatchDirectCalls(old_code_table, code_table, num_imported_functions); | 1164 PatchDirectCalls(old_code_table, code_table, num_imported_functions); |
| 1110 } | 1165 } |
| 1111 | 1166 |
| 1112 FlushICache(isolate_, code_table); | 1167 FlushICache(isolate_, code_table); |
| 1113 | 1168 |
| 1114 //-------------------------------------------------------------------------- | 1169 //-------------------------------------------------------------------------- |
| 1115 // Set up and link the new instance. | 1170 // Set up and link the new instance. |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1184 } | 1239 } |
| 1185 | 1240 |
| 1186 DCHECK(!isolate_->has_pending_exception()); | 1241 DCHECK(!isolate_->has_pending_exception()); |
| 1187 TRACE("Finishing instance %d\n", compiled_module_->instance_id()); | 1242 TRACE("Finishing instance %d\n", compiled_module_->instance_id()); |
| 1188 TRACE_CHAIN(WasmCompiledModule::cast(module_object_->GetInternalField(0))); | 1243 TRACE_CHAIN(WasmCompiledModule::cast(module_object_->GetInternalField(0))); |
| 1189 return instance; | 1244 return instance; |
| 1190 } | 1245 } |
| 1191 | 1246 |
| 1192 private: | 1247 private: |
| 1193 // Represents the initialized state of a table. | 1248 // Represents the initialized state of a table. |
| 1194 struct InitializedTable { | 1249 struct TableInstance { |
| 1195 Handle<JSObject> table_object; // WebAssembly.Table instance | 1250 Handle<JSObject> table_object; // WebAssembly.Table instance |
| 1196 Handle<FixedArray> js_functions; // JSFunctions exported | 1251 Handle<FixedArray> js_wrappers; // JSFunctions exported |
| 1197 Handle<FixedArray> dispatch_table; // internal (code, sig) pairs | 1252 Handle<FixedArray> dispatch_table; // internal (code, sig) pairs |
| 1198 std::vector<WasmFunction*> new_entries; // overwriting entries | |
| 1199 }; | 1253 }; |
| 1200 | 1254 |
| 1201 Isolate* isolate_; | 1255 Isolate* isolate_; |
| 1202 WasmModule* module_; | 1256 WasmModule* module_; |
| 1203 ErrorThrower* thrower_; | 1257 ErrorThrower* thrower_; |
| 1204 Handle<JSObject> module_object_; | 1258 Handle<JSObject> module_object_; |
| 1205 Handle<JSReceiver> ffi_; | 1259 Handle<JSReceiver> ffi_; |
| 1206 Handle<JSArrayBuffer> memory_; | 1260 Handle<JSArrayBuffer> memory_; |
| 1261 Handle<JSArrayBuffer> globals_; | |
| 1207 Handle<WasmCompiledModule> compiled_module_; | 1262 Handle<WasmCompiledModule> compiled_module_; |
| 1263 std::vector<TableInstance> table_instances_; | |
| 1264 std::vector<Handle<JSFunction>> js_wrappers_; | |
| 1208 | 1265 |
| 1209 // Helper routine to print out errors with imports (FFI). | 1266 // Helper routine to print out errors with imports (FFI). |
| 1210 MaybeHandle<JSFunction> ReportFFIError(const char* error, uint32_t index, | 1267 MaybeHandle<JSFunction> ReportFFIError(const char* error, uint32_t index, |
| 1211 Handle<String> module_name, | 1268 Handle<String> module_name, |
| 1212 MaybeHandle<String> function_name) { | 1269 MaybeHandle<String> function_name) { |
| 1213 Handle<String> function_name_handle; | 1270 Handle<String> function_name_handle; |
| 1214 if (function_name.ToHandle(&function_name_handle)) { | 1271 if (function_name.ToHandle(&function_name_handle)) { |
| 1215 thrower_->TypeError( | 1272 thrower_->TypeError( |
| 1216 "Import #%d module=\"%.*s\" function=\"%.*s\" error: %s", index, | 1273 "Import #%d module=\"%.*s\" function=\"%.*s\" error: %s", index, |
| 1217 module_name->length(), module_name->ToCString().get(), | 1274 module_name->length(), module_name->ToCString().get(), |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1256 import_name); | 1313 import_name); |
| 1257 } | 1314 } |
| 1258 } else { | 1315 } else { |
| 1259 // No function specified. Use the "default export". | 1316 // No function specified. Use the "default export". |
| 1260 result = module; | 1317 result = module; |
| 1261 } | 1318 } |
| 1262 | 1319 |
| 1263 return result; | 1320 return result; |
| 1264 } | 1321 } |
| 1265 | 1322 |
| 1266 uint32_t EvalUint32InitExpr(MaybeHandle<JSArrayBuffer> globals, | 1323 uint32_t EvalUint32InitExpr(WasmInitExpr& expr) { |
| 1267 WasmInitExpr& expr) { | |
| 1268 switch (expr.kind) { | 1324 switch (expr.kind) { |
| 1269 case WasmInitExpr::kI32Const: | 1325 case WasmInitExpr::kI32Const: |
| 1270 return expr.val.i32_const; | 1326 return expr.val.i32_const; |
| 1271 case WasmInitExpr::kGlobalIndex: { | 1327 case WasmInitExpr::kGlobalIndex: { |
| 1272 uint32_t offset = module_->globals[expr.val.global_index].offset; | 1328 uint32_t offset = module_->globals[expr.val.global_index].offset; |
| 1273 return *reinterpret_cast<uint32_t*>(raw_buffer_ptr(globals, offset)); | 1329 return *reinterpret_cast<uint32_t*>(raw_buffer_ptr(globals_, offset)); |
| 1274 } | 1330 } |
| 1275 default: | 1331 default: |
| 1276 UNREACHABLE(); | 1332 UNREACHABLE(); |
| 1277 return 0; | 1333 return 0; |
| 1278 } | 1334 } |
| 1279 } | 1335 } |
| 1280 | 1336 |
| 1281 // Load data segments into the memory. | 1337 // Load data segments into the memory. |
| 1282 void LoadDataSegments(MaybeHandle<JSArrayBuffer> globals, Address mem_addr, | 1338 void LoadDataSegments(Address mem_addr, size_t mem_size) { |
| 1283 size_t mem_size) { | |
| 1284 Handle<SeqOneByteString> module_bytes = compiled_module_->module_bytes(); | 1339 Handle<SeqOneByteString> module_bytes = compiled_module_->module_bytes(); |
| 1285 for (auto segment : module_->data_segments) { | 1340 for (auto segment : module_->data_segments) { |
| 1286 uint32_t dest_offset = EvalUint32InitExpr(globals, segment.dest_addr); | 1341 uint32_t dest_offset = EvalUint32InitExpr(segment.dest_addr); |
| 1287 uint32_t source_size = segment.source_size; | 1342 uint32_t source_size = segment.source_size; |
| 1288 if (dest_offset >= mem_size || source_size >= mem_size || | 1343 if (dest_offset >= mem_size || source_size >= mem_size || |
| 1289 dest_offset >= (mem_size - source_size)) { | 1344 dest_offset >= (mem_size - source_size)) { |
| 1290 thrower_->RangeError("data segment does not fit into memory"); | 1345 thrower_->RangeError("data segment does not fit into memory"); |
| 1291 } | 1346 } |
| 1292 byte* dest = mem_addr + dest_offset; | 1347 byte* dest = mem_addr + dest_offset; |
| 1293 const byte* src = reinterpret_cast<const byte*>( | 1348 const byte* src = reinterpret_cast<const byte*>( |
| 1294 module_bytes->GetCharsAddress() + segment.source_offset); | 1349 module_bytes->GetCharsAddress() + segment.source_offset); |
| 1295 memcpy(dest, src, source_size); | 1350 memcpy(dest, src, source_size); |
| 1296 } | 1351 } |
| 1297 } | 1352 } |
| 1298 | 1353 |
| 1299 Handle<Code> CompileImportWrapper(int index, const WasmImport& import, | 1354 Handle<Code> CompileImportWrapper(int index, const WasmImport& import, |
| 1300 Handle<JSReceiver> target, | 1355 Handle<JSReceiver> target, |
| 1301 Handle<String> module_name, | 1356 Handle<String> module_name, |
| 1302 MaybeHandle<String> import_name) { | 1357 MaybeHandle<String> import_name) { |
| 1303 FunctionSig* sig = module_->functions[import.index].sig; | 1358 FunctionSig* sig = module_->functions[import.index].sig; |
| 1304 Handle<Code> code; | 1359 Handle<Code> code; |
| 1305 bool is_match = false; | 1360 WasmFunction* other_func = |
| 1306 Handle<Code> export_wrapper_code; | 1361 GetWasmFunctionForImportWrapper(isolate_, target); |
| 1307 if (target->IsJSFunction()) { | 1362 if (other_func && sig->Equals(other_func->sig)) { |
| 1308 Handle<JSFunction> func = Handle<JSFunction>::cast(target); | 1363 // Signature matched. Unwrap the JS->WASM wrapper and return the raw |
| 1309 export_wrapper_code = handle(func->code()); | |
| 1310 if (export_wrapper_code->kind() == Code::JS_TO_WASM_FUNCTION) { | |
| 1311 // Compare signature of other exported wasm function. | |
| 1312 Handle<JSObject> other_instance( | |
| 1313 JSObject::cast(func->GetInternalField(kInternalModuleInstance)), | |
| 1314 isolate_); | |
| 1315 int func_index = | |
| 1316 Smi::cast(func->GetInternalField(kInternalFunctionIndex))->value(); | |
| 1317 FunctionSig* other_sig = | |
| 1318 GetCppModule(other_instance)->functions[func_index].sig; | |
| 1319 is_match = sig->Equals(other_sig); | |
| 1320 } | |
| 1321 } | |
| 1322 if (is_match) { | |
| 1323 // Signature matched. Unwrap the JS->WASM wrapper and return the naked | |
| 1324 // WASM function code. | 1364 // WASM function code. |
| 1325 int wasm_count = 0; | 1365 return UnwrapImportWrapper(target); |
| 1326 int const mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET); | |
| 1327 for (RelocIterator it(*export_wrapper_code, mask); !it.done(); | |
| 1328 it.next()) { | |
| 1329 RelocInfo* rinfo = it.rinfo(); | |
| 1330 Address target_address = rinfo->target_address(); | |
| 1331 Code* target = Code::GetCodeFromTargetAddress(target_address); | |
| 1332 if (target->kind() == Code::WASM_FUNCTION) { | |
| 1333 ++wasm_count; | |
| 1334 code = handle(target); | |
| 1335 } | |
| 1336 } | |
| 1337 DCHECK(wasm_count == 1); | |
| 1338 return code; | |
| 1339 } else { | 1366 } else { |
| 1340 // Signature mismatch. Compile a new wrapper for the new signature. | 1367 // Signature mismatch. Compile a new wrapper for the new signature. |
| 1341 return compiler::CompileWasmToJSWrapper(isolate_, target, sig, index, | 1368 return compiler::CompileWasmToJSWrapper(isolate_, target, sig, index, |
| 1342 module_name, import_name); | 1369 module_name, import_name); |
| 1343 } | 1370 } |
| 1344 } | 1371 } |
| 1345 | 1372 |
| 1346 void WriteGlobalValue(WasmGlobal& global, MaybeHandle<JSArrayBuffer> globals, | 1373 void WriteGlobalValue(WasmGlobal& global, Handle<Object> value) { |
| 1347 Handle<Object> value) { | |
| 1348 double num = 0; | 1374 double num = 0; |
| 1349 if (value->IsSmi()) { | 1375 if (value->IsSmi()) { |
| 1350 num = Smi::cast(*value)->value(); | 1376 num = Smi::cast(*value)->value(); |
| 1351 } else if (value->IsHeapNumber()) { | 1377 } else if (value->IsHeapNumber()) { |
| 1352 num = HeapNumber::cast(*value)->value(); | 1378 num = HeapNumber::cast(*value)->value(); |
| 1353 } else { | 1379 } else { |
| 1354 UNREACHABLE(); | 1380 UNREACHABLE(); |
| 1355 } | 1381 } |
| 1356 TRACE("init [globals+%u] = %lf, type = %s\n", global.offset, num, | 1382 TRACE("init [globals+%u] = %lf, type = %s\n", global.offset, num, |
| 1357 WasmOpcodes::TypeName(global.type)); | 1383 WasmOpcodes::TypeName(global.type)); |
| 1358 switch (global.type) { | 1384 switch (global.type) { |
| 1359 case kAstI32: | 1385 case kAstI32: |
| 1360 *GetRawGlobalPtr<int32_t>(global, globals) = static_cast<int32_t>(num); | 1386 *GetRawGlobalPtr<int32_t>(global) = static_cast<int32_t>(num); |
| 1361 break; | 1387 break; |
| 1362 case kAstI64: | 1388 case kAstI64: |
| 1363 // TODO(titzer): initialization of imported i64 globals. | 1389 // TODO(titzer): initialization of imported i64 globals. |
| 1364 UNREACHABLE(); | 1390 UNREACHABLE(); |
| 1365 break; | 1391 break; |
| 1366 case kAstF32: | 1392 case kAstF32: |
| 1367 *GetRawGlobalPtr<float>(global, globals) = static_cast<float>(num); | 1393 *GetRawGlobalPtr<float>(global) = static_cast<float>(num); |
| 1368 break; | 1394 break; |
| 1369 case kAstF64: | 1395 case kAstF64: |
| 1370 *GetRawGlobalPtr<double>(global, globals) = static_cast<double>(num); | 1396 *GetRawGlobalPtr<double>(global) = static_cast<double>(num); |
| 1371 break; | 1397 break; |
| 1372 default: | 1398 default: |
| 1373 UNREACHABLE(); | 1399 UNREACHABLE(); |
| 1374 } | 1400 } |
| 1375 } | 1401 } |
| 1376 | 1402 |
| 1377 // Process the imports, including functions, tables, globals, and memory, in | 1403 // Process the imports, including functions, tables, globals, and memory, in |
| 1378 // 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 |
| 1379 // functions. | 1405 // functions. |
| 1380 int ProcessImports(MaybeHandle<JSArrayBuffer> globals, | 1406 int ProcessImports(Handle<FixedArray> code_table, Handle<JSObject> instance) { |
| 1381 Handle<FixedArray> code_table, Handle<JSObject> instance) { | |
| 1382 int num_imported_functions = 0; | 1407 int num_imported_functions = 0; |
| 1408 int num_imported_tables = 0; | |
| 1383 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()); |
| 1384 ++index) { | 1410 ++index) { |
| 1385 WasmImport& import = module_->import_table[index]; | 1411 WasmImport& import = module_->import_table[index]; |
| 1386 Handle<String> module_name = | 1412 Handle<String> module_name = |
| 1387 ExtractStringFromModuleBytes(isolate_, compiled_module_, | 1413 ExtractStringFromModuleBytes(isolate_, compiled_module_, |
| 1388 import.module_name_offset, | 1414 import.module_name_offset, |
| 1389 import.module_name_length) | 1415 import.module_name_length) |
| 1390 .ToHandleChecked(); | 1416 .ToHandleChecked(); |
| 1391 Handle<String> function_name = Handle<String>::null(); | 1417 Handle<String> function_name = Handle<String>::null(); |
| 1392 if (import.field_name_length > 0) { | 1418 if (import.field_name_length > 0) { |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 1411 } | 1437 } |
| 1412 | 1438 |
| 1413 Handle<Code> import_wrapper = CompileImportWrapper( | 1439 Handle<Code> import_wrapper = CompileImportWrapper( |
| 1414 index, import, Handle<JSReceiver>::cast(function), module_name, | 1440 index, import, Handle<JSReceiver>::cast(function), module_name, |
| 1415 function_name); | 1441 function_name); |
| 1416 code_table->set(num_imported_functions, *import_wrapper); | 1442 code_table->set(num_imported_functions, *import_wrapper); |
| 1417 RecordStats(isolate_, *import_wrapper); | 1443 RecordStats(isolate_, *import_wrapper); |
| 1418 num_imported_functions++; | 1444 num_imported_functions++; |
| 1419 break; | 1445 break; |
| 1420 } | 1446 } |
| 1421 case kExternalTable: | 1447 case kExternalTable: { |
| 1422 // 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++; | |
| 1423 break; | 1496 break; |
| 1497 } | |
| 1424 case kExternalMemory: { | 1498 case kExternalMemory: { |
| 1425 Handle<Object> object = result.ToHandleChecked(); | 1499 Handle<Object> object = result.ToHandleChecked(); |
| 1426 if (!WasmJs::IsWasmMemoryObject(isolate_, object)) { | 1500 if (!WasmJs::IsWasmMemoryObject(isolate_, object)) { |
| 1427 ReportFFIError("memory import must be a WebAssembly.Memory object", | 1501 ReportFFIError("memory import must be a WebAssembly.Memory object", |
| 1428 index, module_name, function_name); | 1502 index, module_name, function_name); |
| 1429 return -1; | 1503 return -1; |
| 1430 } | 1504 } |
| 1431 instance->SetInternalField(kWasmMemObject, *object); | 1505 instance->SetInternalField(kWasmMemObject, *object); |
| 1432 memory_ = WasmJs::GetWasmMemoryArrayBuffer(isolate_, object); | 1506 memory_ = WasmJs::GetWasmMemoryArrayBuffer(isolate_, object); |
| 1433 break; | 1507 break; |
| 1434 } | 1508 } |
| 1435 case kExternalGlobal: { | 1509 case kExternalGlobal: { |
| 1436 // Global imports are converted to numbers and written into the | 1510 // Global imports are converted to numbers and written into the |
| 1437 // {globals} array buffer. | 1511 // {globals_} array buffer. |
| 1438 Handle<Object> object = result.ToHandleChecked(); | 1512 Handle<Object> object = result.ToHandleChecked(); |
| 1439 MaybeHandle<Object> number = Object::ToNumber(object); | 1513 MaybeHandle<Object> number = Object::ToNumber(object); |
| 1440 if (number.is_null()) { | 1514 if (number.is_null()) { |
| 1441 ReportFFIError("global import could not be converted to number", | 1515 ReportFFIError("global import could not be converted to number", |
| 1442 index, module_name, function_name); | 1516 index, module_name, function_name); |
| 1443 return -1; | 1517 return -1; |
| 1444 } | 1518 } |
| 1445 Handle<Object> val = number.ToHandleChecked(); | 1519 Handle<Object> val = number.ToHandleChecked(); |
| 1446 WriteGlobalValue(module_->globals[import.index], globals, val); | 1520 WriteGlobalValue(module_->globals[import.index], val); |
| 1447 break; | 1521 break; |
| 1448 } | 1522 } |
| 1449 default: | 1523 default: |
| 1450 UNREACHABLE(); | 1524 UNREACHABLE(); |
| 1451 break; | 1525 break; |
| 1452 } | 1526 } |
| 1453 } | 1527 } |
| 1454 return num_imported_functions; | 1528 return num_imported_functions; |
| 1455 } | 1529 } |
| 1456 | 1530 |
| 1457 template <typename T> | 1531 template <typename T> |
| 1458 T* GetRawGlobalPtr(WasmGlobal& global, MaybeHandle<JSArrayBuffer> globals) { | 1532 T* GetRawGlobalPtr(WasmGlobal& global) { |
| 1459 return reinterpret_cast<T*>(raw_buffer_ptr(globals, global.offset)); | 1533 return reinterpret_cast<T*>(raw_buffer_ptr(globals_, global.offset)); |
| 1460 } | 1534 } |
| 1461 | 1535 |
| 1462 // Process initialization of globals. | 1536 // Process initialization of globals. |
| 1463 void InitGlobals(MaybeHandle<JSArrayBuffer> globals) { | 1537 void InitGlobals() { |
| 1464 for (auto global : module_->globals) { | 1538 for (auto global : module_->globals) { |
| 1465 switch (global.init.kind) { | 1539 switch (global.init.kind) { |
| 1466 case WasmInitExpr::kI32Const: | 1540 case WasmInitExpr::kI32Const: |
| 1467 *GetRawGlobalPtr<int32_t>(global, globals) = | 1541 *GetRawGlobalPtr<int32_t>(global) = global.init.val.i32_const; |
| 1468 global.init.val.i32_const; | |
| 1469 break; | 1542 break; |
| 1470 case WasmInitExpr::kI64Const: | 1543 case WasmInitExpr::kI64Const: |
| 1471 *GetRawGlobalPtr<int64_t>(global, globals) = | 1544 *GetRawGlobalPtr<int64_t>(global) = global.init.val.i64_const; |
| 1472 global.init.val.i64_const; | |
| 1473 break; | 1545 break; |
| 1474 case WasmInitExpr::kF32Const: | 1546 case WasmInitExpr::kF32Const: |
| 1475 *GetRawGlobalPtr<float>(global, globals) = global.init.val.f32_const; | 1547 *GetRawGlobalPtr<float>(global) = global.init.val.f32_const; |
| 1476 break; | 1548 break; |
| 1477 case WasmInitExpr::kF64Const: | 1549 case WasmInitExpr::kF64Const: |
| 1478 *GetRawGlobalPtr<double>(global, globals) = global.init.val.f64_const; | 1550 *GetRawGlobalPtr<double>(global) = global.init.val.f64_const; |
| 1479 break; | 1551 break; |
| 1480 case WasmInitExpr::kGlobalIndex: { | 1552 case WasmInitExpr::kGlobalIndex: { |
| 1481 // Initialize with another global. | 1553 // Initialize with another global. |
| 1482 uint32_t new_offset = global.offset; | 1554 uint32_t new_offset = global.offset; |
| 1483 uint32_t old_offset = | 1555 uint32_t old_offset = |
| 1484 module_->globals[global.init.val.global_index].offset; | 1556 module_->globals[global.init.val.global_index].offset; |
| 1485 TRACE("init [globals+%u] = [globals+%d]\n", global.offset, | 1557 TRACE("init [globals+%u] = [globals+%d]\n", global.offset, |
| 1486 old_offset); | 1558 old_offset); |
| 1487 size_t size = (global.type == kAstI64 || global.type == kAstF64) | 1559 size_t size = (global.type == kAstI64 || global.type == kAstF64) |
| 1488 ? sizeof(double) | 1560 ? sizeof(double) |
| 1489 : sizeof(int32_t); | 1561 : sizeof(int32_t); |
| 1490 memcpy(raw_buffer_ptr(globals, new_offset), | 1562 memcpy(raw_buffer_ptr(globals_, new_offset), |
| 1491 raw_buffer_ptr(globals, old_offset), size); | 1563 raw_buffer_ptr(globals_, old_offset), size); |
| 1492 break; | 1564 break; |
| 1493 } | 1565 } |
| 1494 case WasmInitExpr::kNone: | 1566 case WasmInitExpr::kNone: |
| 1495 // Happens with imported globals. | 1567 // Happens with imported globals. |
| 1496 break; | 1568 break; |
| 1497 default: | 1569 default: |
| 1498 UNREACHABLE(); | 1570 UNREACHABLE(); |
| 1499 break; | 1571 break; |
| 1500 } | 1572 } |
| 1501 } | 1573 } |
| 1502 } | 1574 } |
| 1503 | 1575 |
| 1504 // Allocate memory for a module instance as a new JSArrayBuffer. | 1576 // Allocate memory for a module instance as a new JSArrayBuffer. |
| 1505 Handle<JSArrayBuffer> AllocateMemory(uint32_t min_mem_pages) { | 1577 Handle<JSArrayBuffer> AllocateMemory(uint32_t min_mem_pages) { |
| 1506 if (min_mem_pages > WasmModule::kMaxMemPages) { | 1578 if (min_mem_pages > WasmModule::kV8MaxPages) { |
| 1507 thrower_->RangeError("Out of memory: wasm memory too large"); | 1579 thrower_->RangeError("Out of memory: wasm memory too large"); |
| 1508 return Handle<JSArrayBuffer>::null(); | 1580 return Handle<JSArrayBuffer>::null(); |
| 1509 } | 1581 } |
| 1510 Handle<JSArrayBuffer> mem_buffer = | 1582 Handle<JSArrayBuffer> mem_buffer = |
| 1511 NewArrayBuffer(isolate_, min_mem_pages * WasmModule::kPageSize); | 1583 NewArrayBuffer(isolate_, min_mem_pages * WasmModule::kPageSize); |
| 1512 | 1584 |
| 1513 if (mem_buffer.is_null()) { | 1585 if (mem_buffer.is_null()) { |
| 1514 thrower_->RangeError("Out of memory: wasm memory"); | 1586 thrower_->RangeError("Out of memory: wasm memory"); |
| 1515 } | 1587 } |
| 1516 return mem_buffer; | 1588 return mem_buffer; |
| 1517 } | 1589 } |
| 1518 | 1590 |
| 1519 // Process the exports, creating wrappers for functions, tables, memories, | 1591 // Process the exports, creating wrappers for functions, tables, memories, |
| 1520 // and globals. | 1592 // and globals. |
| 1521 void ProcessExports(MaybeHandle<JSArrayBuffer> globals, | 1593 void ProcessExports(Handle<FixedArray> code_table, |
| 1522 std::vector<InitializedTable>& inited_tables, | |
| 1523 Handle<FixedArray> code_table, | |
| 1524 Handle<JSObject> instance) { | 1594 Handle<JSObject> instance) { |
| 1525 if (module_->export_table.size() == 0) return; | 1595 bool needs_wrappers = module_->num_exported_functions > 0; |
| 1526 | 1596 for (auto table_instance : table_instances_) { |
| 1527 // Allocate a table to cache the exported JSFunctions if needed. | 1597 if (!table_instance.js_wrappers.is_null()) { |
| 1528 bool has_exported_functions = module_->num_exported_functions > 0; | 1598 needs_wrappers = true; |
| 1529 if (!has_exported_functions) { | 1599 break; |
| 1530 for (auto table : module_->function_tables) { | |
| 1531 if (table.exported) { | |
| 1532 has_exported_functions = true; | |
| 1533 break; | |
| 1534 } | |
| 1535 } | 1600 } |
| 1536 } | 1601 } |
| 1537 Handle<FixedArray> exported_functions = | 1602 for (auto table : module_->function_tables) { |
| 1538 has_exported_functions | 1603 if (table.exported) { |
| 1539 ? isolate_->factory()->NewFixedArray( | 1604 needs_wrappers = true; |
| 1540 static_cast<int>(module_->functions.size())) | 1605 break; |
| 1541 : 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 } | |
| 1542 | 1613 |
| 1543 Handle<JSObject> exports_object = instance; | 1614 Handle<JSObject> exports_object = instance; |
| 1544 if (module_->origin == kWasmOrigin) { | 1615 if (module_->export_table.size() > 0 && module_->origin == kWasmOrigin) { |
| 1545 // Create the "exports" object. | 1616 // Create the "exports" object. |
| 1546 Handle<JSFunction> object_function = Handle<JSFunction>( | 1617 Handle<JSFunction> object_function = Handle<JSFunction>( |
| 1547 isolate_->native_context()->object_function(), isolate_); | 1618 isolate_->native_context()->object_function(), isolate_); |
| 1548 exports_object = | 1619 exports_object = |
| 1549 isolate_->factory()->NewJSObject(object_function, TENURED); | 1620 isolate_->factory()->NewJSObject(object_function, TENURED); |
| 1550 Handle<String> exports_name = | 1621 Handle<String> exports_name = |
| 1551 isolate_->factory()->InternalizeUtf8String("exports"); | 1622 isolate_->factory()->InternalizeUtf8String("exports"); |
| 1552 JSObject::AddProperty(instance, exports_name, exports_object, READ_ONLY); | 1623 JSObject::AddProperty(instance, exports_name, exports_object, READ_ONLY); |
| 1553 } | 1624 } |
| 1554 | 1625 |
| 1555 PropertyDescriptor desc; | 1626 PropertyDescriptor desc; |
| 1556 desc.set_writable(false); | 1627 desc.set_writable(false); |
| 1557 | 1628 |
| 1629 // Process each export in the export table. | |
| 1558 int func_index = 0; | 1630 int func_index = 0; |
| 1559 for (auto exp : module_->export_table) { | 1631 for (auto exp : module_->export_table) { |
| 1560 Handle<String> name = | 1632 Handle<String> name = |
| 1561 ExtractStringFromModuleBytes(isolate_, compiled_module_, | 1633 ExtractStringFromModuleBytes(isolate_, compiled_module_, |
| 1562 exp.name_offset, exp.name_length) | 1634 exp.name_offset, exp.name_length) |
| 1563 .ToHandleChecked(); | 1635 .ToHandleChecked(); |
| 1564 switch (exp.kind) { | 1636 switch (exp.kind) { |
| 1565 case kExternalFunction: { | 1637 case kExternalFunction: { |
| 1566 // Wrap and export the code as a JSFunction. | 1638 // Wrap and export the code as a JSFunction. |
| 1567 WasmFunction& function = module_->functions[exp.index]; | 1639 WasmFunction& function = module_->functions[exp.index]; |
| 1568 int export_index = | 1640 int export_index = |
| 1569 static_cast<int>(module_->functions.size() + func_index); | 1641 static_cast<int>(module_->functions.size() + func_index); |
| 1570 Handle<Object> value(exported_functions->get(exp.index), isolate_); | 1642 Handle<JSFunction> js_function = js_wrappers_[exp.index]; |
| 1571 Handle<JSFunction> js_function; | 1643 if (js_function.is_null()) { |
| 1572 if (value->IsJSFunction()) { | |
| 1573 // There already is a JSFunction for this WASM function. | |
| 1574 js_function = Handle<JSFunction>::cast(value); | |
| 1575 } else { | |
| 1576 // Wrap the exported code as a JSFunction. | 1644 // Wrap the exported code as a JSFunction. |
| 1577 Handle<Code> export_code = | 1645 Handle<Code> export_code = |
| 1578 code_table->GetValueChecked<Code>(isolate_, export_index); | 1646 code_table->GetValueChecked<Code>(isolate_, export_index); |
| 1579 js_function = | 1647 js_function = |
| 1580 WrapExportCodeAsJSFunction(isolate_, export_code, name, | 1648 WrapExportCodeAsJSFunction(isolate_, export_code, name, |
| 1581 function.sig, func_index, instance); | 1649 function.sig, func_index, instance); |
| 1582 exported_functions->set(exp.index, *js_function); | 1650 js_wrappers_[exp.index] = js_function; |
| 1583 } | 1651 } |
| 1584 desc.set_value(js_function); | 1652 desc.set_value(js_function); |
| 1585 func_index++; | 1653 func_index++; |
| 1586 break; | 1654 break; |
| 1587 } | 1655 } |
| 1588 case kExternalTable: { | 1656 case kExternalTable: { |
| 1589 InitializedTable& init_table = inited_tables[exp.index]; | 1657 // Export a table as a WebAssembly.Table object. |
| 1658 TableInstance& table_instance = table_instances_[exp.index]; | |
| 1590 WasmIndirectFunctionTable& table = | 1659 WasmIndirectFunctionTable& table = |
| 1591 module_->function_tables[exp.index]; | 1660 module_->function_tables[exp.index]; |
| 1592 if (init_table.table_object.is_null()) { | 1661 if (table_instance.table_object.is_null()) { |
| 1593 init_table.table_object = WasmJs::CreateWasmTableObject( | 1662 table_instance.table_object = WasmJs::CreateWasmTableObject( |
| 1594 isolate_, table.size, table.max_size != 0, table.max_size, | 1663 isolate_, table.min_size, table.has_max, table.max_size, |
| 1595 &init_table.js_functions); | 1664 &table_instance.js_wrappers); |
| 1596 } | 1665 } |
| 1597 desc.set_value(init_table.table_object); | 1666 desc.set_value(table_instance.table_object); |
| 1598 break; | 1667 break; |
| 1599 } | 1668 } |
| 1600 case kExternalMemory: { | 1669 case kExternalMemory: { |
| 1601 // Export the memory as a WebAssembly.Memory object. | 1670 // Export the memory as a WebAssembly.Memory object. |
| 1602 Handle<Object> memory_object( | 1671 Handle<Object> memory_object( |
| 1603 instance->GetInternalField(kWasmMemObject), isolate_); | 1672 instance->GetInternalField(kWasmMemObject), isolate_); |
| 1604 if (memory_object->IsUndefined(isolate_)) { | 1673 if (memory_object->IsUndefined(isolate_)) { |
| 1605 // If there was no imported WebAssembly.Memory object, create one. | 1674 // If there was no imported WebAssembly.Memory object, create one. |
| 1606 Handle<JSArrayBuffer> buffer( | 1675 Handle<JSArrayBuffer> buffer( |
| 1607 JSArrayBuffer::cast( | 1676 JSArrayBuffer::cast( |
| 1608 instance->GetInternalField(kWasmMemArrayBuffer)), | 1677 instance->GetInternalField(kWasmMemArrayBuffer)), |
| 1609 isolate_); | 1678 isolate_); |
| 1610 memory_object = | 1679 memory_object = |
| 1611 WasmJs::CreateWasmMemoryObject(isolate_, buffer, false, 0); | 1680 WasmJs::CreateWasmMemoryObject(isolate_, buffer, false, 0); |
| 1612 instance->SetInternalField(kWasmMemObject, *memory_object); | 1681 instance->SetInternalField(kWasmMemObject, *memory_object); |
| 1613 } | 1682 } |
| 1614 | 1683 |
| 1615 desc.set_value(memory_object); | 1684 desc.set_value(memory_object); |
| 1616 break; | 1685 break; |
| 1617 } | 1686 } |
| 1618 case kExternalGlobal: { | 1687 case kExternalGlobal: { |
| 1619 // Export the value of the global variable as a number. | 1688 // Export the value of the global variable as a number. |
| 1620 WasmGlobal& global = module_->globals[exp.index]; | 1689 WasmGlobal& global = module_->globals[exp.index]; |
| 1621 double num = 0; | 1690 double num = 0; |
| 1622 switch (global.type) { | 1691 switch (global.type) { |
| 1623 case kAstI32: | 1692 case kAstI32: |
| 1624 num = *GetRawGlobalPtr<int32_t>(global, globals); | 1693 num = *GetRawGlobalPtr<int32_t>(global); |
| 1625 break; | 1694 break; |
| 1626 case kAstF32: | 1695 case kAstF32: |
| 1627 num = *GetRawGlobalPtr<float>(global, globals); | 1696 num = *GetRawGlobalPtr<float>(global); |
| 1628 break; | 1697 break; |
| 1629 case kAstF64: | 1698 case kAstF64: |
| 1630 num = *GetRawGlobalPtr<double>(global, globals); | 1699 num = *GetRawGlobalPtr<double>(global); |
| 1631 break; | 1700 break; |
| 1632 default: | 1701 default: |
| 1633 UNREACHABLE(); | 1702 UNREACHABLE(); |
| 1634 } | 1703 } |
| 1635 desc.set_value(isolate_->factory()->NewNumber(num)); | 1704 desc.set_value(isolate_->factory()->NewNumber(num)); |
| 1636 break; | 1705 break; |
| 1637 } | 1706 } |
| 1638 default: | 1707 default: |
| 1639 UNREACHABLE(); | 1708 UNREACHABLE(); |
| 1640 break; | 1709 break; |
| 1641 } | 1710 } |
| 1642 | 1711 |
| 1643 v8::Maybe<bool> status = JSReceiver::DefineOwnProperty( | 1712 v8::Maybe<bool> status = JSReceiver::DefineOwnProperty( |
| 1644 isolate_, exports_object, name, &desc, Object::THROW_ON_ERROR); | 1713 isolate_, exports_object, name, &desc, Object::THROW_ON_ERROR); |
| 1645 if (!status.IsJust()) { | 1714 if (!status.IsJust()) { |
| 1646 thrower_->TypeError("export of %.*s failed.", name->length(), | 1715 thrower_->TypeError("export of %.*s failed.", name->length(), |
| 1647 name->ToCString().get()); | 1716 name->ToCString().get()); |
| 1648 return; | 1717 return; |
| 1649 } | 1718 } |
| 1650 } | 1719 } |
| 1720 } | |
| 1651 | 1721 |
| 1652 // Fill out the contents of WebAssembly.Table instances. | 1722 void InitializeTables(Handle<FixedArray> code_table, |
| 1653 if (!exported_functions.is_null()) { | 1723 Handle<JSObject> instance) { |
| 1654 // TODO(titzer): We compile JS->WASM wrappers for any function that is a | 1724 Handle<FixedArray> old_function_tables = |
| 1655 // member of an exported indirect table that is not itself exported. This | 1725 compiled_module_->function_tables(); |
| 1656 // should be done at compile time and cached instead. | 1726 int function_table_count = |
| 1657 WasmInstance temp_instance(module_); | 1727 static_cast<int>(module_->function_tables.size()); |
| 1658 temp_instance.context = isolate_->native_context(); | 1728 Handle<FixedArray> new_function_tables = |
| 1659 temp_instance.mem_size = 0; | 1729 isolate_->factory()->NewFixedArray(function_table_count); |
| 1660 temp_instance.mem_start = nullptr; | 1730 for (int index = 0; index < function_table_count; ++index) { |
| 1661 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); | |
| 1662 | 1734 |
| 1663 ModuleEnv module_env; | 1735 if (table_instance.dispatch_table.is_null()) { |
| 1664 module_env.module = module_; | 1736 // Create a new dispatch table if necessary. |
| 1665 module_env.instance = &temp_instance; | 1737 table_instance.dispatch_table = |
| 1666 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 } | |
| 1667 | 1745 |
| 1668 // Fill the exported tables with JSFunctions. | 1746 new_function_tables->set(static_cast<int>(index), |
| 1669 for (auto inited_table : inited_tables) { | 1747 *table_instance.dispatch_table); |
| 1670 if (inited_table.js_functions.is_null()) continue; | 1748 |
| 1671 for (int i = 0; i < static_cast<int>(inited_table.new_entries.size()); | 1749 Handle<FixedArray> all_dispatch_tables; |
| 1672 i++) { | 1750 if (!table_instance.table_object.is_null()) { |
| 1673 if (inited_table.new_entries[i] == nullptr) continue; | 1751 // Get the existing dispatch table(s) with the WebAssembly.Table object. |
| 1674 int func_index = | 1752 all_dispatch_tables = WasmJs::AddWasmTableDispatchTable( |
| 1675 static_cast<int>(inited_table.new_entries[i]->func_index); | 1753 isolate_, table_instance.table_object, Handle<JSObject>::null(), |
| 1676 if (!exported_functions->get(func_index)->IsJSFunction()) { | 1754 index, Handle<FixedArray>::null()); |
| 1677 // 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()) { | |
| 1678 Handle<Code> wasm_code(Code::cast(code_table->get(func_index)), | 1779 Handle<Code> wasm_code(Code::cast(code_table->get(func_index)), |
| 1679 isolate_); | 1780 isolate_); |
| 1680 Handle<Code> wrapper_code = compiler::CompileJSToWasmWrapper( | 1781 if (js_wrappers_[func_index].is_null()) { |
| 1681 isolate_, &module_env, wasm_code, func_index); | 1782 // No JSFunction entry yet exists for this function. Create one. |
| 1682 Handle<JSFunction> js_function = WrapExportCodeAsJSFunction( | 1783 // TODO(titzer): We compile JS->WASM wrappers for functions are |
| 1683 isolate_, wrapper_code, isolate_->factory()->empty_string(), | 1784 // not exported but are in an exported table. This should be done |
| 1684 module_->functions[func_index].sig, func_index, instance); | 1785 // at module compile time and cached instead. |
| 1685 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); | |
| 1686 } | 1809 } |
| 1687 inited_table.js_functions->set(i, | |
| 1688 exported_functions->get(func_index)); | |
| 1689 } | 1810 } |
| 1690 } | 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 } | |
| 1691 } | 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); | |
| 1692 } | 1834 } |
| 1693 }; | 1835 }; |
| 1694 | 1836 |
| 1695 // Instantiates a WASM module, creating a WebAssembly.Instance from a | 1837 // Instantiates a WASM module, creating a WebAssembly.Instance from a |
| 1696 // WebAssembly.Module. | 1838 // WebAssembly.Module. |
| 1697 MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate, | 1839 MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate, |
| 1698 ErrorThrower* thrower, | 1840 ErrorThrower* thrower, |
| 1699 Handle<JSObject> wasm_module, | 1841 Handle<JSObject> wasm_module, |
| 1700 Handle<JSReceiver> ffi, | 1842 Handle<JSReceiver> ffi, |
| 1701 Handle<JSArrayBuffer> memory) { | 1843 Handle<JSArrayBuffer> memory) { |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1952 GetInstanceMemory(isolate, instance); | 2094 GetInstanceMemory(isolate, instance); |
| 1953 Handle<JSArrayBuffer> buffer; | 2095 Handle<JSArrayBuffer> buffer; |
| 1954 if (!maybe_mem_buffer.ToHandle(&buffer)) { | 2096 if (!maybe_mem_buffer.ToHandle(&buffer)) { |
| 1955 return 0; | 2097 return 0; |
| 1956 } else { | 2098 } else { |
| 1957 return buffer->byte_length()->Number() / WasmModule::kPageSize; | 2099 return buffer->byte_length()->Number() / WasmModule::kPageSize; |
| 1958 } | 2100 } |
| 1959 } | 2101 } |
| 1960 | 2102 |
| 1961 uint32_t GetMaxInstanceMemorySize(Isolate* isolate, Handle<JSObject> instance) { | 2103 uint32_t GetMaxInstanceMemorySize(Isolate* isolate, Handle<JSObject> instance) { |
| 1962 uint32_t max_pages = WasmModule::kMaxMemPages; | 2104 uint32_t max_pages = WasmModule::kV8MaxPages; |
| 1963 Handle<Object> memory_object(instance->GetInternalField(kWasmMemObject), | 2105 Handle<Object> memory_object(instance->GetInternalField(kWasmMemObject), |
| 1964 isolate); | 2106 isolate); |
| 1965 if (memory_object->IsUndefined(isolate)) return max_pages; | 2107 if (memory_object->IsUndefined(isolate)) return max_pages; |
| 1966 return WasmJs::GetWasmMemoryMaximumSize(isolate, memory_object); | 2108 return WasmJs::GetWasmMemoryMaximumSize(isolate, memory_object); |
| 1967 } | 2109 } |
| 1968 | 2110 |
| 1969 int32_t wasm::GrowInstanceMemory(Isolate* isolate, Handle<JSObject> instance, | 2111 int32_t wasm::GrowInstanceMemory(Isolate* isolate, Handle<JSObject> instance, |
| 1970 uint32_t pages) { | 2112 uint32_t pages) { |
| 1971 if (!IsWasmInstance(*instance)) return -1; | 2113 if (!IsWasmInstance(*instance)) return -1; |
| 1972 if (pages == 0) return GetInstanceMemorySize(isolate, instance); | 2114 if (pages == 0) return GetInstanceMemorySize(isolate, instance); |
| 1973 uint32_t max_pages = GetMaxInstanceMemorySize(isolate, instance); | 2115 uint32_t max_pages = GetMaxInstanceMemorySize(isolate, instance); |
| 1974 if (WasmModule::kMaxMemPages < max_pages) return -1; | 2116 if (WasmModule::kV8MaxPages < max_pages) return -1; |
| 1975 | 2117 |
| 1976 Address old_mem_start = nullptr; | 2118 Address old_mem_start = nullptr; |
| 1977 uint32_t old_size = 0, new_size = 0; | 2119 uint32_t old_size = 0, new_size = 0; |
| 1978 | 2120 |
| 1979 MaybeHandle<JSArrayBuffer> maybe_mem_buffer = | 2121 MaybeHandle<JSArrayBuffer> maybe_mem_buffer = |
| 1980 GetInstanceMemory(isolate, instance); | 2122 GetInstanceMemory(isolate, instance); |
| 1981 Handle<JSArrayBuffer> old_buffer; | 2123 Handle<JSArrayBuffer> old_buffer; |
| 1982 if (!maybe_mem_buffer.ToHandle(&old_buffer) || | 2124 if (!maybe_mem_buffer.ToHandle(&old_buffer) || |
| 1983 old_buffer->backing_store() == nullptr) { | 2125 old_buffer->backing_store() == nullptr) { |
| 1984 // 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... | |
| 2090 CHECK_NOT_NULL(result.val); | 2232 CHECK_NOT_NULL(result.val); |
| 2091 module = const_cast<WasmModule*>(result.val); | 2233 module = const_cast<WasmModule*>(result.val); |
| 2092 } | 2234 } |
| 2093 | 2235 |
| 2094 Handle<WasmModuleWrapper> module_wrapper = | 2236 Handle<WasmModuleWrapper> module_wrapper = |
| 2095 WasmModuleWrapper::New(isolate, module); | 2237 WasmModuleWrapper::New(isolate, module); |
| 2096 | 2238 |
| 2097 compiled_module->set_module_wrapper(module_wrapper); | 2239 compiled_module->set_module_wrapper(module_wrapper); |
| 2098 DCHECK(WasmCompiledModule::IsWasmCompiledModule(*compiled_module)); | 2240 DCHECK(WasmCompiledModule::IsWasmCompiledModule(*compiled_module)); |
| 2099 } | 2241 } |
| OLD | NEW |