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

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

Issue 2456193006: Revert of [wasm] Support for restricted table imports. (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/wasm/wasm-module.h ('k') | test/cctest/wasm/wasm-run-utils.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 the V8 project authors. All rights reserved. 1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <memory> 5 #include <memory>
6 6
7 #include "src/base/atomic-utils.h" 7 #include "src/base/atomic-utils.h"
8 #include "src/code-stubs.h" 8 #include "src/code-stubs.h"
9 9
10 #include "src/macro-assembler.h" 10 #include "src/macro-assembler.h"
(...skipping 19 matching lines...) Expand all
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
68 namespace { 40 namespace {
69 41
70 static const int kPlaceholderMarker = 1000000000; 42 static const int kPlaceholderMarker = 1000000000;
71 43
72 enum JSFunctionExportInternalField { 44 enum JSFunctionExportInternalField {
73 kInternalModuleInstance, 45 kInternalModuleInstance,
74 kInternalFunctionIndex 46 kInternalFunctionIndex
75 }; 47 };
76 48
77 // Internal constants for the layout of the module object. 49 // Internal constants for the layout of the module object.
(...skipping 30 matching lines...) Expand all
108 Handle<Object> new_ref) { 80 Handle<Object> new_ref) {
109 for (RelocIterator it(*code, 1 << RelocInfo::EMBEDDED_OBJECT); !it.done(); 81 for (RelocIterator it(*code, 1 << RelocInfo::EMBEDDED_OBJECT); !it.done();
110 it.next()) { 82 it.next()) {
111 if (it.rinfo()->target_object() == *old_ref) { 83 if (it.rinfo()->target_object() == *old_ref) {
112 it.rinfo()->set_target_object(*new_ref); 84 it.rinfo()->set_target_object(*new_ref);
113 } 85 }
114 } 86 }
115 } 87 }
116 88
117 Handle<JSArrayBuffer> NewArrayBuffer(Isolate* isolate, size_t size) { 89 Handle<JSArrayBuffer> NewArrayBuffer(Isolate* isolate, size_t size) {
118 if (size > (WasmModule::kV8MaxPages * WasmModule::kPageSize)) { 90 if (size > (WasmModule::kMaxMemPages * WasmModule::kPageSize)) {
119 // TODO(titzer): lift restriction on maximum memory allocated here. 91 // TODO(titzer): lift restriction on maximum memory allocated here.
120 return Handle<JSArrayBuffer>::null(); 92 return Handle<JSArrayBuffer>::null();
121 } 93 }
122 void* memory = isolate->array_buffer_allocator()->Allocate(size); 94 void* memory = isolate->array_buffer_allocator()->Allocate(size);
123 if (memory == nullptr) { 95 if (memory == nullptr) {
124 return Handle<JSArrayBuffer>::null(); 96 return Handle<JSArrayBuffer>::null();
125 } 97 }
126 98
127 #if DEBUG 99 #if DEBUG
128 // Double check the API allocator actually zero-initialized the memory. 100 // Double check the API allocator actually zero-initialized the memory.
(...skipping 735 matching lines...) Expand 10 before | Expand all | Expand 10 after
864 Handle<String> module_bytes_string = 836 Handle<String> module_bytes_string =
865 factory->NewStringFromOneByte(module_bytes_vec, TENURED) 837 factory->NewStringFromOneByte(module_bytes_vec, TENURED)
866 .ToHandleChecked(); 838 .ToHandleChecked();
867 DCHECK(module_bytes_string->IsSeqOneByteString()); 839 DCHECK(module_bytes_string->IsSeqOneByteString());
868 ret->set_module_bytes(Handle<SeqOneByteString>::cast(module_bytes_string)); 840 ret->set_module_bytes(Handle<SeqOneByteString>::cast(module_bytes_string));
869 } 841 }
870 842
871 return ret; 843 return ret;
872 } 844 }
873 845
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
969 // A helper class to simplify instantiating a module from a compiled module. 846 // A helper class to simplify instantiating a module from a compiled module.
970 // It closes over the {Isolate}, the {ErrorThrower}, the {WasmCompiledModule}, 847 // It closes over the {Isolate}, the {ErrorThrower}, the {WasmCompiledModule},
971 // etc. 848 // etc.
972 class WasmInstanceBuilder { 849 class WasmInstanceBuilder {
973 public: 850 public:
974 WasmInstanceBuilder(Isolate* isolate, ErrorThrower* thrower, 851 WasmInstanceBuilder(Isolate* isolate, ErrorThrower* thrower,
975 Handle<JSObject> module_object, Handle<JSReceiver> ffi, 852 Handle<JSObject> module_object, Handle<JSReceiver> ffi,
976 Handle<JSArrayBuffer> memory) 853 Handle<JSArrayBuffer> memory)
977 : isolate_(isolate), 854 : isolate_(isolate),
978 thrower_(thrower), 855 thrower_(thrower),
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
1067 JS_OBJECT_TYPE, 944 JS_OBJECT_TYPE,
1068 JSObject::kHeaderSize + kWasmInstanceInternalFieldCount * kPointerSize); 945 JSObject::kHeaderSize + kWasmInstanceInternalFieldCount * kPointerSize);
1069 Handle<JSObject> instance = factory->NewJSObjectFromMap(map, TENURED); 946 Handle<JSObject> instance = factory->NewJSObjectFromMap(map, TENURED);
1070 instance->SetInternalField(kWasmMemObject, 947 instance->SetInternalField(kWasmMemObject,
1071 isolate_->heap()->undefined_value()); 948 isolate_->heap()->undefined_value());
1072 949
1073 //-------------------------------------------------------------------------- 950 //--------------------------------------------------------------------------
1074 // Set up the globals for the new instance. 951 // Set up the globals for the new instance.
1075 //-------------------------------------------------------------------------- 952 //--------------------------------------------------------------------------
1076 MaybeHandle<JSArrayBuffer> old_globals; 953 MaybeHandle<JSArrayBuffer> old_globals;
954 MaybeHandle<JSArrayBuffer> globals;
1077 uint32_t globals_size = module_->globals_size; 955 uint32_t globals_size = module_->globals_size;
1078 if (globals_size > 0) { 956 if (globals_size > 0) {
1079 Handle<JSArrayBuffer> global_buffer = 957 Handle<JSArrayBuffer> global_buffer =
1080 NewArrayBuffer(isolate_, globals_size); 958 NewArrayBuffer(isolate_, globals_size);
1081 globals_ = global_buffer; 959 globals = global_buffer;
1082 if (globals_.is_null()) { 960 if (globals.is_null()) {
1083 thrower_->RangeError("Out of memory: wasm globals"); 961 thrower_->RangeError("Out of memory: wasm globals");
1084 return nothing; 962 return nothing;
1085 } 963 }
1086 Address old_address = owner.is_null() 964 Address old_address = owner.is_null()
1087 ? nullptr 965 ? nullptr
1088 : GetGlobalStartAddressFromCodeTemplate( 966 : GetGlobalStartAddressFromCodeTemplate(
1089 isolate_->heap()->undefined_value(), 967 isolate_->heap()->undefined_value(),
1090 JSObject::cast(*owner.ToHandleChecked())); 968 JSObject::cast(*owner.ToHandleChecked()));
1091 RelocateGlobals(code_table, old_address, 969 RelocateGlobals(code_table, old_address,
1092 static_cast<Address>(global_buffer->backing_store())); 970 static_cast<Address>(global_buffer->backing_store()));
1093 instance->SetInternalField(kWasmGlobalsArrayBuffer, *global_buffer); 971 instance->SetInternalField(kWasmGlobalsArrayBuffer, *global_buffer);
1094 } 972 }
1095 973
1096 //-------------------------------------------------------------------------- 974 //--------------------------------------------------------------------------
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 //--------------------------------------------------------------------------
1109 // Process the imports for the module. 975 // Process the imports for the module.
1110 //-------------------------------------------------------------------------- 976 //--------------------------------------------------------------------------
1111 int num_imported_functions = ProcessImports(code_table, instance); 977 int num_imported_functions = ProcessImports(globals, code_table, instance);
1112 if (num_imported_functions < 0) return nothing; 978 if (num_imported_functions < 0) return nothing;
1113 979
1114 //-------------------------------------------------------------------------- 980 //--------------------------------------------------------------------------
1115 // Process the initialization for the module's globals. 981 // Process the initialization for the module's globals.
1116 //-------------------------------------------------------------------------- 982 //--------------------------------------------------------------------------
1117 InitGlobals(); 983 InitGlobals(globals);
1118 984
1119 //-------------------------------------------------------------------------- 985 //--------------------------------------------------------------------------
1120 // Set up the memory for the new instance. 986 // Set up the memory for the new instance.
1121 //-------------------------------------------------------------------------- 987 //--------------------------------------------------------------------------
1122 MaybeHandle<JSArrayBuffer> old_memory; 988 MaybeHandle<JSArrayBuffer> old_memory;
1123 989
1124 uint32_t min_mem_pages = module_->min_mem_pages; 990 uint32_t min_mem_pages = module_->min_mem_pages;
1125 isolate_->counters()->wasm_min_mem_pages_count()->AddSample(min_mem_pages); 991 isolate_->counters()->wasm_min_mem_pages_count()->AddSample(min_mem_pages);
1126 // TODO(wasm): re-enable counter for max_mem_pages when we use that field. 992 // TODO(wasm): re-enable counter for max_mem_pages when we use that field.
1127 993
1128 if (!memory_.is_null()) { 994 if (!memory_.is_null()) {
1129 // Set externally passed ArrayBuffer non neuterable. 995 // Set externally passed ArrayBuffer non neuterable.
1130 memory_->set_is_neuterable(false); 996 memory_->set_is_neuterable(false);
1131 } else if (min_mem_pages > 0) { 997 } else if (min_mem_pages > 0) {
1132 memory_ = AllocateMemory(min_mem_pages); 998 memory_ = AllocateMemory(min_mem_pages);
1133 if (memory_.is_null()) return nothing; // failed to allocate memory 999 if (memory_.is_null()) return nothing; // failed to allocate memory
1134 } 1000 }
1135 1001
1136 if (!memory_.is_null()) { 1002 if (!memory_.is_null()) {
1137 instance->SetInternalField(kWasmMemArrayBuffer, *memory_); 1003 instance->SetInternalField(kWasmMemArrayBuffer, *memory_);
1138 Address mem_start = static_cast<Address>(memory_->backing_store()); 1004 Address mem_start = static_cast<Address>(memory_->backing_store());
1139 uint32_t mem_size = 1005 uint32_t mem_size =
1140 static_cast<uint32_t>(memory_->byte_length()->Number()); 1006 static_cast<uint32_t>(memory_->byte_length()->Number());
1141 LoadDataSegments(mem_start, mem_size); 1007 LoadDataSegments(globals, mem_start, mem_size);
1142 1008
1143 uint32_t old_mem_size = compiled_module_->mem_size(); 1009 uint32_t old_mem_size = compiled_module_->mem_size();
1144 Address old_mem_start = 1010 Address old_mem_start =
1145 compiled_module_->has_memory() 1011 compiled_module_->has_memory()
1146 ? static_cast<Address>( 1012 ? static_cast<Address>(
1147 compiled_module_->memory()->backing_store()) 1013 compiled_module_->memory()->backing_store())
1148 : nullptr; 1014 : nullptr;
1149 RelocateMemoryReferencesInCode(code_table, old_mem_start, mem_start, 1015 RelocateMemoryReferencesInCode(code_table, old_mem_start, mem_start,
1150 old_mem_size, mem_size); 1016 old_mem_size, mem_size);
1151 compiled_module_->set_memory(memory_); 1017 compiled_module_->set_memory(memory_);
(...skipping 10 matching lines...) Expand all
1162 if (code->kind() == Code::WASM_FUNCTION) { 1028 if (code->kind() == Code::WASM_FUNCTION) {
1163 Handle<FixedArray> deopt_data = factory->NewFixedArray(2, TENURED); 1029 Handle<FixedArray> deopt_data = factory->NewFixedArray(2, TENURED);
1164 deopt_data->set(0, *weak_link); 1030 deopt_data->set(0, *weak_link);
1165 deopt_data->set(1, Smi::FromInt(static_cast<int>(i))); 1031 deopt_data->set(1, Smi::FromInt(static_cast<int>(i)));
1166 deopt_data->set_length(2); 1032 deopt_data->set_length(2);
1167 code->set_deoptimization_data(*deopt_data); 1033 code->set_deoptimization_data(*deopt_data);
1168 } 1034 }
1169 } 1035 }
1170 1036
1171 //-------------------------------------------------------------------------- 1037 //--------------------------------------------------------------------------
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 //--------------------------------------------------------------------------
1172 // Set up the exports object for the new instance. 1104 // Set up the exports object for the new instance.
1173 //-------------------------------------------------------------------------- 1105 //--------------------------------------------------------------------------
1174 ProcessExports(code_table, instance); 1106 ProcessExports(globals, inited_tables, 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);
1180 1107
1181 if (num_imported_functions > 0 || !owner.is_null()) { 1108 if (num_imported_functions > 0 || !owner.is_null()) {
1182 // If the code was cloned, or new imports were compiled, patch. 1109 // If the code was cloned, or new imports were compiled, patch.
1183 PatchDirectCalls(old_code_table, code_table, num_imported_functions); 1110 PatchDirectCalls(old_code_table, code_table, num_imported_functions);
1184 } 1111 }
1185 1112
1186 FlushICache(isolate_, code_table); 1113 FlushICache(isolate_, code_table);
1187 1114
1188 //-------------------------------------------------------------------------- 1115 //--------------------------------------------------------------------------
1189 // Set up and link the new instance. 1116 // Set up and link the new instance.
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1258 } 1185 }
1259 1186
1260 DCHECK(!isolate_->has_pending_exception()); 1187 DCHECK(!isolate_->has_pending_exception());
1261 TRACE("Finishing instance %d\n", compiled_module_->instance_id()); 1188 TRACE("Finishing instance %d\n", compiled_module_->instance_id());
1262 TRACE_CHAIN(WasmCompiledModule::cast(module_object_->GetInternalField(0))); 1189 TRACE_CHAIN(WasmCompiledModule::cast(module_object_->GetInternalField(0)));
1263 return instance; 1190 return instance;
1264 } 1191 }
1265 1192
1266 private: 1193 private:
1267 // Represents the initialized state of a table. 1194 // Represents the initialized state of a table.
1268 struct TableInstance { 1195 struct InitializedTable {
1269 Handle<JSObject> table_object; // WebAssembly.Table instance 1196 Handle<JSObject> table_object; // WebAssembly.Table instance
1270 Handle<FixedArray> js_wrappers; // JSFunctions exported 1197 Handle<FixedArray> js_functions; // JSFunctions exported
1271 Handle<FixedArray> dispatch_table; // internal (code, sig) pairs 1198 Handle<FixedArray> dispatch_table; // internal (code, sig) pairs
1199 std::vector<WasmFunction*> new_entries; // overwriting entries
1272 }; 1200 };
1273 1201
1274 Isolate* isolate_; 1202 Isolate* isolate_;
1275 WasmModule* module_; 1203 WasmModule* module_;
1276 ErrorThrower* thrower_; 1204 ErrorThrower* thrower_;
1277 Handle<JSObject> module_object_; 1205 Handle<JSObject> module_object_;
1278 Handle<JSReceiver> ffi_; 1206 Handle<JSReceiver> ffi_;
1279 Handle<JSArrayBuffer> memory_; 1207 Handle<JSArrayBuffer> memory_;
1280 Handle<JSArrayBuffer> globals_;
1281 Handle<WasmCompiledModule> compiled_module_; 1208 Handle<WasmCompiledModule> compiled_module_;
1282 std::vector<TableInstance> table_instances_;
1283 std::vector<Handle<JSFunction>> js_wrappers_;
1284 1209
1285 // Helper routine to print out errors with imports (FFI). 1210 // Helper routine to print out errors with imports (FFI).
1286 MaybeHandle<JSFunction> ReportFFIError(const char* error, uint32_t index, 1211 MaybeHandle<JSFunction> ReportFFIError(const char* error, uint32_t index,
1287 Handle<String> module_name, 1212 Handle<String> module_name,
1288 MaybeHandle<String> function_name) { 1213 MaybeHandle<String> function_name) {
1289 Handle<String> function_name_handle; 1214 Handle<String> function_name_handle;
1290 if (function_name.ToHandle(&function_name_handle)) { 1215 if (function_name.ToHandle(&function_name_handle)) {
1291 thrower_->TypeError( 1216 thrower_->TypeError(
1292 "Import #%d module=\"%.*s\" function=\"%.*s\" error: %s", index, 1217 "Import #%d module=\"%.*s\" function=\"%.*s\" error: %s", index,
1293 module_name->length(), module_name->ToCString().get(), 1218 module_name->length(), module_name->ToCString().get(),
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1332 import_name); 1257 import_name);
1333 } 1258 }
1334 } else { 1259 } else {
1335 // No function specified. Use the "default export". 1260 // No function specified. Use the "default export".
1336 result = module; 1261 result = module;
1337 } 1262 }
1338 1263
1339 return result; 1264 return result;
1340 } 1265 }
1341 1266
1342 uint32_t EvalUint32InitExpr(WasmInitExpr& expr) { 1267 uint32_t EvalUint32InitExpr(MaybeHandle<JSArrayBuffer> globals,
1268 WasmInitExpr& expr) {
1343 switch (expr.kind) { 1269 switch (expr.kind) {
1344 case WasmInitExpr::kI32Const: 1270 case WasmInitExpr::kI32Const:
1345 return expr.val.i32_const; 1271 return expr.val.i32_const;
1346 case WasmInitExpr::kGlobalIndex: { 1272 case WasmInitExpr::kGlobalIndex: {
1347 uint32_t offset = module_->globals[expr.val.global_index].offset; 1273 uint32_t offset = module_->globals[expr.val.global_index].offset;
1348 return *reinterpret_cast<uint32_t*>(raw_buffer_ptr(globals_, offset)); 1274 return *reinterpret_cast<uint32_t*>(raw_buffer_ptr(globals, offset));
1349 } 1275 }
1350 default: 1276 default:
1351 UNREACHABLE(); 1277 UNREACHABLE();
1352 return 0; 1278 return 0;
1353 } 1279 }
1354 } 1280 }
1355 1281
1356 // Load data segments into the memory. 1282 // Load data segments into the memory.
1357 void LoadDataSegments(Address mem_addr, size_t mem_size) { 1283 void LoadDataSegments(MaybeHandle<JSArrayBuffer> globals, Address mem_addr,
1284 size_t mem_size) {
1358 Handle<SeqOneByteString> module_bytes = compiled_module_->module_bytes(); 1285 Handle<SeqOneByteString> module_bytes = compiled_module_->module_bytes();
1359 for (auto segment : module_->data_segments) { 1286 for (auto segment : module_->data_segments) {
1360 uint32_t dest_offset = EvalUint32InitExpr(segment.dest_addr); 1287 uint32_t dest_offset = EvalUint32InitExpr(globals, segment.dest_addr);
1361 uint32_t source_size = segment.source_size; 1288 uint32_t source_size = segment.source_size;
1362 if (dest_offset >= mem_size || source_size >= mem_size || 1289 if (dest_offset >= mem_size || source_size >= mem_size ||
1363 dest_offset >= (mem_size - source_size)) { 1290 dest_offset >= (mem_size - source_size)) {
1364 thrower_->RangeError("data segment does not fit into memory"); 1291 thrower_->RangeError("data segment does not fit into memory");
1365 } 1292 }
1366 byte* dest = mem_addr + dest_offset; 1293 byte* dest = mem_addr + dest_offset;
1367 const byte* src = reinterpret_cast<const byte*>( 1294 const byte* src = reinterpret_cast<const byte*>(
1368 module_bytes->GetCharsAddress() + segment.source_offset); 1295 module_bytes->GetCharsAddress() + segment.source_offset);
1369 memcpy(dest, src, source_size); 1296 memcpy(dest, src, source_size);
1370 } 1297 }
1371 } 1298 }
1372 1299
1373 void WriteGlobalValue(WasmGlobal& global, Handle<Object> value) { 1300 Handle<Code> CompileImportWrapper(int index, const WasmImport& import,
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) {
1374 double num = 0; 1349 double num = 0;
1375 if (value->IsSmi()) { 1350 if (value->IsSmi()) {
1376 num = Smi::cast(*value)->value(); 1351 num = Smi::cast(*value)->value();
1377 } else if (value->IsHeapNumber()) { 1352 } else if (value->IsHeapNumber()) {
1378 num = HeapNumber::cast(*value)->value(); 1353 num = HeapNumber::cast(*value)->value();
1379 } else { 1354 } else {
1380 UNREACHABLE(); 1355 UNREACHABLE();
1381 } 1356 }
1382 TRACE("init [globals+%u] = %lf, type = %s\n", global.offset, num, 1357 TRACE("init [globals+%u] = %lf, type = %s\n", global.offset, num,
1383 WasmOpcodes::TypeName(global.type)); 1358 WasmOpcodes::TypeName(global.type));
1384 switch (global.type) { 1359 switch (global.type) {
1385 case kAstI32: 1360 case kAstI32:
1386 *GetRawGlobalPtr<int32_t>(global) = static_cast<int32_t>(num); 1361 *GetRawGlobalPtr<int32_t>(global, globals) = static_cast<int32_t>(num);
1387 break; 1362 break;
1388 case kAstI64: 1363 case kAstI64:
1389 // TODO(titzer): initialization of imported i64 globals. 1364 // TODO(titzer): initialization of imported i64 globals.
1390 UNREACHABLE(); 1365 UNREACHABLE();
1391 break; 1366 break;
1392 case kAstF32: 1367 case kAstF32:
1393 *GetRawGlobalPtr<float>(global) = static_cast<float>(num); 1368 *GetRawGlobalPtr<float>(global, globals) = static_cast<float>(num);
1394 break; 1369 break;
1395 case kAstF64: 1370 case kAstF64:
1396 *GetRawGlobalPtr<double>(global) = static_cast<double>(num); 1371 *GetRawGlobalPtr<double>(global, globals) = static_cast<double>(num);
1397 break; 1372 break;
1398 default: 1373 default:
1399 UNREACHABLE(); 1374 UNREACHABLE();
1400 } 1375 }
1401 } 1376 }
1402 1377
1403 // Process the imports, including functions, tables, globals, and memory, in 1378 // Process the imports, including functions, tables, globals, and memory, in
1404 // order, loading them from the {ffi_} object. Returns the number of imported 1379 // order, loading them from the {ffi_} object. Returns the number of imported
1405 // functions. 1380 // functions.
1406 int ProcessImports(Handle<FixedArray> code_table, Handle<JSObject> instance) { 1381 int ProcessImports(MaybeHandle<JSArrayBuffer> globals,
1382 Handle<FixedArray> code_table, Handle<JSObject> instance) {
1407 int num_imported_functions = 0; 1383 int num_imported_functions = 0;
1408 int num_imported_tables = 0;
1409 for (int index = 0; index < static_cast<int>(module_->import_table.size()); 1384 for (int index = 0; index < static_cast<int>(module_->import_table.size());
1410 ++index) { 1385 ++index) {
1411 WasmImport& import = module_->import_table[index]; 1386 WasmImport& import = module_->import_table[index];
1412 Handle<String> module_name = 1387 Handle<String> module_name =
1413 ExtractStringFromModuleBytes(isolate_, compiled_module_, 1388 ExtractStringFromModuleBytes(isolate_, compiled_module_,
1414 import.module_name_offset, 1389 import.module_name_offset,
1415 import.module_name_length) 1390 import.module_name_length)
1416 .ToHandleChecked(); 1391 .ToHandleChecked();
1417 Handle<String> function_name = Handle<String>::null(); 1392 Handle<String> function_name = Handle<String>::null();
1418 if (import.field_name_length > 0) { 1393 if (import.field_name_length > 0) {
(...skipping 11 matching lines...) Expand all
1430 case kExternalFunction: { 1405 case kExternalFunction: {
1431 // Function imports must be callable. 1406 // Function imports must be callable.
1432 Handle<Object> function = result.ToHandleChecked(); 1407 Handle<Object> function = result.ToHandleChecked();
1433 if (!function->IsCallable()) { 1408 if (!function->IsCallable()) {
1434 ReportFFIError("function import requires a callable", index, 1409 ReportFFIError("function import requires a callable", index,
1435 module_name, function_name); 1410 module_name, function_name);
1436 return -1; 1411 return -1;
1437 } 1412 }
1438 1413
1439 Handle<Code> import_wrapper = CompileImportWrapper( 1414 Handle<Code> import_wrapper = CompileImportWrapper(
1440 isolate_, index, module_->functions[import.index].sig, 1415 index, import, Handle<JSReceiver>::cast(function), module_name,
1441 Handle<JSReceiver>::cast(function), module_name, function_name); 1416 function_name);
1442 code_table->set(num_imported_functions, *import_wrapper); 1417 code_table->set(num_imported_functions, *import_wrapper);
1443 RecordStats(isolate_, *import_wrapper); 1418 RecordStats(isolate_, *import_wrapper);
1444 num_imported_functions++; 1419 num_imported_functions++;
1445 break; 1420 break;
1446 } 1421 }
1447 case kExternalTable: { 1422 case kExternalTable:
1448 Handle<Object> value = result.ToHandleChecked(); 1423 // TODO(titzer): Table imports must be a WebAssembly.Table.
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++;
1496 break; 1424 break;
1497 }
1498 case kExternalMemory: { 1425 case kExternalMemory: {
1499 Handle<Object> object = result.ToHandleChecked(); 1426 Handle<Object> object = result.ToHandleChecked();
1500 if (!WasmJs::IsWasmMemoryObject(isolate_, object)) { 1427 if (!WasmJs::IsWasmMemoryObject(isolate_, object)) {
1501 ReportFFIError("memory import must be a WebAssembly.Memory object", 1428 ReportFFIError("memory import must be a WebAssembly.Memory object",
1502 index, module_name, function_name); 1429 index, module_name, function_name);
1503 return -1; 1430 return -1;
1504 } 1431 }
1505 instance->SetInternalField(kWasmMemObject, *object); 1432 instance->SetInternalField(kWasmMemObject, *object);
1506 memory_ = WasmJs::GetWasmMemoryArrayBuffer(isolate_, object); 1433 memory_ = WasmJs::GetWasmMemoryArrayBuffer(isolate_, object);
1507 break; 1434 break;
1508 } 1435 }
1509 case kExternalGlobal: { 1436 case kExternalGlobal: {
1510 // Global imports are converted to numbers and written into the 1437 // Global imports are converted to numbers and written into the
1511 // {globals_} array buffer. 1438 // {globals} array buffer.
1512 Handle<Object> object = result.ToHandleChecked(); 1439 Handle<Object> object = result.ToHandleChecked();
1513 MaybeHandle<Object> number = Object::ToNumber(object); 1440 MaybeHandle<Object> number = Object::ToNumber(object);
1514 if (number.is_null()) { 1441 if (number.is_null()) {
1515 ReportFFIError("global import could not be converted to number", 1442 ReportFFIError("global import could not be converted to number",
1516 index, module_name, function_name); 1443 index, module_name, function_name);
1517 return -1; 1444 return -1;
1518 } 1445 }
1519 Handle<Object> val = number.ToHandleChecked(); 1446 Handle<Object> val = number.ToHandleChecked();
1520 WriteGlobalValue(module_->globals[import.index], val); 1447 WriteGlobalValue(module_->globals[import.index], globals, val);
1521 break; 1448 break;
1522 } 1449 }
1523 default: 1450 default:
1524 UNREACHABLE(); 1451 UNREACHABLE();
1525 break; 1452 break;
1526 } 1453 }
1527 } 1454 }
1528 return num_imported_functions; 1455 return num_imported_functions;
1529 } 1456 }
1530 1457
1531 template <typename T> 1458 template <typename T>
1532 T* GetRawGlobalPtr(WasmGlobal& global) { 1459 T* GetRawGlobalPtr(WasmGlobal& global, MaybeHandle<JSArrayBuffer> globals) {
1533 return reinterpret_cast<T*>(raw_buffer_ptr(globals_, global.offset)); 1460 return reinterpret_cast<T*>(raw_buffer_ptr(globals, global.offset));
1534 } 1461 }
1535 1462
1536 // Process initialization of globals. 1463 // Process initialization of globals.
1537 void InitGlobals() { 1464 void InitGlobals(MaybeHandle<JSArrayBuffer> globals) {
1538 for (auto global : module_->globals) { 1465 for (auto global : module_->globals) {
1539 switch (global.init.kind) { 1466 switch (global.init.kind) {
1540 case WasmInitExpr::kI32Const: 1467 case WasmInitExpr::kI32Const:
1541 *GetRawGlobalPtr<int32_t>(global) = global.init.val.i32_const; 1468 *GetRawGlobalPtr<int32_t>(global, globals) =
1469 global.init.val.i32_const;
1542 break; 1470 break;
1543 case WasmInitExpr::kI64Const: 1471 case WasmInitExpr::kI64Const:
1544 *GetRawGlobalPtr<int64_t>(global) = global.init.val.i64_const; 1472 *GetRawGlobalPtr<int64_t>(global, globals) =
1473 global.init.val.i64_const;
1545 break; 1474 break;
1546 case WasmInitExpr::kF32Const: 1475 case WasmInitExpr::kF32Const:
1547 *GetRawGlobalPtr<float>(global) = global.init.val.f32_const; 1476 *GetRawGlobalPtr<float>(global, globals) = global.init.val.f32_const;
1548 break; 1477 break;
1549 case WasmInitExpr::kF64Const: 1478 case WasmInitExpr::kF64Const:
1550 *GetRawGlobalPtr<double>(global) = global.init.val.f64_const; 1479 *GetRawGlobalPtr<double>(global, globals) = global.init.val.f64_const;
1551 break; 1480 break;
1552 case WasmInitExpr::kGlobalIndex: { 1481 case WasmInitExpr::kGlobalIndex: {
1553 // Initialize with another global. 1482 // Initialize with another global.
1554 uint32_t new_offset = global.offset; 1483 uint32_t new_offset = global.offset;
1555 uint32_t old_offset = 1484 uint32_t old_offset =
1556 module_->globals[global.init.val.global_index].offset; 1485 module_->globals[global.init.val.global_index].offset;
1557 TRACE("init [globals+%u] = [globals+%d]\n", global.offset, 1486 TRACE("init [globals+%u] = [globals+%d]\n", global.offset,
1558 old_offset); 1487 old_offset);
1559 size_t size = (global.type == kAstI64 || global.type == kAstF64) 1488 size_t size = (global.type == kAstI64 || global.type == kAstF64)
1560 ? sizeof(double) 1489 ? sizeof(double)
1561 : sizeof(int32_t); 1490 : sizeof(int32_t);
1562 memcpy(raw_buffer_ptr(globals_, new_offset), 1491 memcpy(raw_buffer_ptr(globals, new_offset),
1563 raw_buffer_ptr(globals_, old_offset), size); 1492 raw_buffer_ptr(globals, old_offset), size);
1564 break; 1493 break;
1565 } 1494 }
1566 case WasmInitExpr::kNone: 1495 case WasmInitExpr::kNone:
1567 // Happens with imported globals. 1496 // Happens with imported globals.
1568 break; 1497 break;
1569 default: 1498 default:
1570 UNREACHABLE(); 1499 UNREACHABLE();
1571 break; 1500 break;
1572 } 1501 }
1573 } 1502 }
1574 } 1503 }
1575 1504
1576 // Allocate memory for a module instance as a new JSArrayBuffer. 1505 // Allocate memory for a module instance as a new JSArrayBuffer.
1577 Handle<JSArrayBuffer> AllocateMemory(uint32_t min_mem_pages) { 1506 Handle<JSArrayBuffer> AllocateMemory(uint32_t min_mem_pages) {
1578 if (min_mem_pages > WasmModule::kV8MaxPages) { 1507 if (min_mem_pages > WasmModule::kMaxMemPages) {
1579 thrower_->RangeError("Out of memory: wasm memory too large"); 1508 thrower_->RangeError("Out of memory: wasm memory too large");
1580 return Handle<JSArrayBuffer>::null(); 1509 return Handle<JSArrayBuffer>::null();
1581 } 1510 }
1582 Handle<JSArrayBuffer> mem_buffer = 1511 Handle<JSArrayBuffer> mem_buffer =
1583 NewArrayBuffer(isolate_, min_mem_pages * WasmModule::kPageSize); 1512 NewArrayBuffer(isolate_, min_mem_pages * WasmModule::kPageSize);
1584 1513
1585 if (mem_buffer.is_null()) { 1514 if (mem_buffer.is_null()) {
1586 thrower_->RangeError("Out of memory: wasm memory"); 1515 thrower_->RangeError("Out of memory: wasm memory");
1587 } 1516 }
1588 return mem_buffer; 1517 return mem_buffer;
1589 } 1518 }
1590 1519
1591 // Process the exports, creating wrappers for functions, tables, memories, 1520 // Process the exports, creating wrappers for functions, tables, memories,
1592 // and globals. 1521 // and globals.
1593 void ProcessExports(Handle<FixedArray> code_table, 1522 void ProcessExports(MaybeHandle<JSArrayBuffer> globals,
1523 std::vector<InitializedTable>& inited_tables,
1524 Handle<FixedArray> code_table,
1594 Handle<JSObject> instance) { 1525 Handle<JSObject> instance) {
1595 bool needs_wrappers = module_->num_exported_functions > 0; 1526 if (module_->export_table.size() == 0) return;
1596 for (auto table_instance : table_instances_) { 1527
1597 if (!table_instance.js_wrappers.is_null()) { 1528 // Allocate a table to cache the exported JSFunctions if needed.
1598 needs_wrappers = true; 1529 bool has_exported_functions = module_->num_exported_functions > 0;
1599 break; 1530 if (!has_exported_functions) {
1531 for (auto table : module_->function_tables) {
1532 if (table.exported) {
1533 has_exported_functions = true;
1534 break;
1535 }
1600 } 1536 }
1601 } 1537 }
1602 for (auto table : module_->function_tables) { 1538 Handle<FixedArray> exported_functions =
1603 if (table.exported) { 1539 has_exported_functions
1604 needs_wrappers = true; 1540 ? isolate_->factory()->NewFixedArray(
1605 break; 1541 static_cast<int>(module_->functions.size()))
1606 } 1542 : Handle<FixedArray>::null();
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 }
1613 1543
1614 Handle<JSObject> exports_object = instance; 1544 Handle<JSObject> exports_object = instance;
1615 if (module_->export_table.size() > 0 && module_->origin == kWasmOrigin) { 1545 if (module_->origin == kWasmOrigin) {
1616 // Create the "exports" object. 1546 // Create the "exports" object.
1617 Handle<JSFunction> object_function = Handle<JSFunction>( 1547 Handle<JSFunction> object_function = Handle<JSFunction>(
1618 isolate_->native_context()->object_function(), isolate_); 1548 isolate_->native_context()->object_function(), isolate_);
1619 exports_object = 1549 exports_object =
1620 isolate_->factory()->NewJSObject(object_function, TENURED); 1550 isolate_->factory()->NewJSObject(object_function, TENURED);
1621 Handle<String> exports_name = 1551 Handle<String> exports_name =
1622 isolate_->factory()->InternalizeUtf8String("exports"); 1552 isolate_->factory()->InternalizeUtf8String("exports");
1623 JSObject::AddProperty(instance, exports_name, exports_object, READ_ONLY); 1553 JSObject::AddProperty(instance, exports_name, exports_object, READ_ONLY);
1624 } 1554 }
1625 1555
1626 PropertyDescriptor desc; 1556 PropertyDescriptor desc;
1627 desc.set_writable(false); 1557 desc.set_writable(false);
1628 1558
1629 // Process each export in the export table.
1630 int func_index = 0; 1559 int func_index = 0;
1631 for (auto exp : module_->export_table) { 1560 for (auto exp : module_->export_table) {
1632 Handle<String> name = 1561 Handle<String> name =
1633 ExtractStringFromModuleBytes(isolate_, compiled_module_, 1562 ExtractStringFromModuleBytes(isolate_, compiled_module_,
1634 exp.name_offset, exp.name_length) 1563 exp.name_offset, exp.name_length)
1635 .ToHandleChecked(); 1564 .ToHandleChecked();
1636 switch (exp.kind) { 1565 switch (exp.kind) {
1637 case kExternalFunction: { 1566 case kExternalFunction: {
1638 // Wrap and export the code as a JSFunction. 1567 // Wrap and export the code as a JSFunction.
1639 WasmFunction& function = module_->functions[exp.index]; 1568 WasmFunction& function = module_->functions[exp.index];
1640 int export_index = 1569 int export_index =
1641 static_cast<int>(module_->functions.size() + func_index); 1570 static_cast<int>(module_->functions.size() + func_index);
1642 Handle<JSFunction> js_function = js_wrappers_[exp.index]; 1571 Handle<Object> value(exported_functions->get(exp.index), isolate_);
1643 if (js_function.is_null()) { 1572 Handle<JSFunction> js_function;
1573 if (value->IsJSFunction()) {
1574 // There already is a JSFunction for this WASM function.
1575 js_function = Handle<JSFunction>::cast(value);
1576 } else {
1644 // Wrap the exported code as a JSFunction. 1577 // Wrap the exported code as a JSFunction.
1645 Handle<Code> export_code = 1578 Handle<Code> export_code =
1646 code_table->GetValueChecked<Code>(isolate_, export_index); 1579 code_table->GetValueChecked<Code>(isolate_, export_index);
1647 js_function = 1580 js_function =
1648 WrapExportCodeAsJSFunction(isolate_, export_code, name, 1581 WrapExportCodeAsJSFunction(isolate_, export_code, name,
1649 function.sig, func_index, instance); 1582 function.sig, func_index, instance);
1650 js_wrappers_[exp.index] = js_function; 1583 exported_functions->set(exp.index, *js_function);
1651 } 1584 }
1652 desc.set_value(js_function); 1585 desc.set_value(js_function);
1653 func_index++; 1586 func_index++;
1654 break; 1587 break;
1655 } 1588 }
1656 case kExternalTable: { 1589 case kExternalTable: {
1657 // Export a table as a WebAssembly.Table object. 1590 InitializedTable& init_table = inited_tables[exp.index];
1658 TableInstance& table_instance = table_instances_[exp.index];
1659 WasmIndirectFunctionTable& table = 1591 WasmIndirectFunctionTable& table =
1660 module_->function_tables[exp.index]; 1592 module_->function_tables[exp.index];
1661 if (table_instance.table_object.is_null()) { 1593 if (init_table.table_object.is_null()) {
1662 table_instance.table_object = WasmJs::CreateWasmTableObject( 1594 init_table.table_object = WasmJs::CreateWasmTableObject(
1663 isolate_, table.min_size, table.has_max, table.max_size, 1595 isolate_, table.size, table.max_size != 0, table.max_size,
1664 &table_instance.js_wrappers); 1596 &init_table.js_functions);
1665 } 1597 }
1666 desc.set_value(table_instance.table_object); 1598 desc.set_value(init_table.table_object);
1667 break; 1599 break;
1668 } 1600 }
1669 case kExternalMemory: { 1601 case kExternalMemory: {
1670 // Export the memory as a WebAssembly.Memory object. 1602 // Export the memory as a WebAssembly.Memory object.
1671 Handle<Object> memory_object( 1603 Handle<Object> memory_object(
1672 instance->GetInternalField(kWasmMemObject), isolate_); 1604 instance->GetInternalField(kWasmMemObject), isolate_);
1673 if (memory_object->IsUndefined(isolate_)) { 1605 if (memory_object->IsUndefined(isolate_)) {
1674 // If there was no imported WebAssembly.Memory object, create one. 1606 // If there was no imported WebAssembly.Memory object, create one.
1675 Handle<JSArrayBuffer> buffer( 1607 Handle<JSArrayBuffer> buffer(
1676 JSArrayBuffer::cast( 1608 JSArrayBuffer::cast(
1677 instance->GetInternalField(kWasmMemArrayBuffer)), 1609 instance->GetInternalField(kWasmMemArrayBuffer)),
1678 isolate_); 1610 isolate_);
1679 memory_object = 1611 memory_object =
1680 WasmJs::CreateWasmMemoryObject(isolate_, buffer, false, 0); 1612 WasmJs::CreateWasmMemoryObject(isolate_, buffer, false, 0);
1681 instance->SetInternalField(kWasmMemObject, *memory_object); 1613 instance->SetInternalField(kWasmMemObject, *memory_object);
1682 } 1614 }
1683 1615
1684 desc.set_value(memory_object); 1616 desc.set_value(memory_object);
1685 break; 1617 break;
1686 } 1618 }
1687 case kExternalGlobal: { 1619 case kExternalGlobal: {
1688 // Export the value of the global variable as a number. 1620 // Export the value of the global variable as a number.
1689 WasmGlobal& global = module_->globals[exp.index]; 1621 WasmGlobal& global = module_->globals[exp.index];
1690 double num = 0; 1622 double num = 0;
1691 switch (global.type) { 1623 switch (global.type) {
1692 case kAstI32: 1624 case kAstI32:
1693 num = *GetRawGlobalPtr<int32_t>(global); 1625 num = *GetRawGlobalPtr<int32_t>(global, globals);
1694 break; 1626 break;
1695 case kAstF32: 1627 case kAstF32:
1696 num = *GetRawGlobalPtr<float>(global); 1628 num = *GetRawGlobalPtr<float>(global, globals);
1697 break; 1629 break;
1698 case kAstF64: 1630 case kAstF64:
1699 num = *GetRawGlobalPtr<double>(global); 1631 num = *GetRawGlobalPtr<double>(global, globals);
1700 break; 1632 break;
1701 default: 1633 default:
1702 UNREACHABLE(); 1634 UNREACHABLE();
1703 } 1635 }
1704 desc.set_value(isolate_->factory()->NewNumber(num)); 1636 desc.set_value(isolate_->factory()->NewNumber(num));
1705 break; 1637 break;
1706 } 1638 }
1707 default: 1639 default:
1708 UNREACHABLE(); 1640 UNREACHABLE();
1709 break; 1641 break;
1710 } 1642 }
1711 1643
1712 v8::Maybe<bool> status = JSReceiver::DefineOwnProperty( 1644 v8::Maybe<bool> status = JSReceiver::DefineOwnProperty(
1713 isolate_, exports_object, name, &desc, Object::THROW_ON_ERROR); 1645 isolate_, exports_object, name, &desc, Object::THROW_ON_ERROR);
1714 if (!status.IsJust()) { 1646 if (!status.IsJust()) {
1715 thrower_->TypeError("export of %.*s failed.", name->length(), 1647 thrower_->TypeError("export of %.*s failed.", name->length(),
1716 name->ToCString().get()); 1648 name->ToCString().get());
1717 return; 1649 return;
1718 } 1650 }
1719 } 1651 }
1720 }
1721 1652
1722 void InitializeTables(Handle<FixedArray> code_table, 1653 // Fill out the contents of WebAssembly.Table instances.
1723 Handle<JSObject> instance) { 1654 if (!exported_functions.is_null()) {
1724 Handle<FixedArray> old_function_tables = 1655 // TODO(titzer): We compile JS->WASM wrappers for any function that is a
1725 compiled_module_->function_tables(); 1656 // member of an exported indirect table that is not itself exported. This
1726 int function_table_count = 1657 // should be done at compile time and cached instead.
1727 static_cast<int>(module_->function_tables.size()); 1658 WasmInstance temp_instance(module_);
1728 Handle<FixedArray> new_function_tables = 1659 temp_instance.context = isolate_->native_context();
1729 isolate_->factory()->NewFixedArray(function_table_count); 1660 temp_instance.mem_size = 0;
1730 for (int index = 0; index < function_table_count; ++index) { 1661 temp_instance.mem_start = nullptr;
1731 WasmIndirectFunctionTable& table = module_->function_tables[index]; 1662 temp_instance.globals_start = nullptr;
1732 TableInstance& table_instance = table_instances_[index];
1733 int table_size = static_cast<int>(table.min_size);
1734 1663
1735 if (table_instance.dispatch_table.is_null()) { 1664 ModuleEnv module_env;
1736 // Create a new dispatch table if necessary. 1665 module_env.module = module_;
1737 table_instance.dispatch_table = 1666 module_env.instance = &temp_instance;
1738 isolate_->factory()->NewFixedArray(table_size * 2); 1667 module_env.origin = module_->origin;
1739 for (int i = 0; i < table_size; ++i) { 1668
1740 // Fill the table with invalid signature indexes so that 1669 // Fill the exported tables with JSFunctions.
1741 // uninitialized entries will always fail the signature check. 1670 for (auto inited_table : inited_tables) {
1742 table_instance.dispatch_table->set(i, Smi::FromInt(kInvalidSigIndex)); 1671 if (inited_table.js_functions.is_null()) continue;
1672 for (int i = 0; i < static_cast<int>(inited_table.new_entries.size());
1673 i++) {
1674 if (inited_table.new_entries[i] == nullptr) continue;
1675 int func_index =
1676 static_cast<int>(inited_table.new_entries[i]->func_index);
1677 if (!exported_functions->get(func_index)->IsJSFunction()) {
1678 // No JSFunction entry yet exists for this function. Create one.
1679 Handle<Code> wasm_code(Code::cast(code_table->get(func_index)),
1680 isolate_);
1681 Handle<Code> wrapper_code = compiler::CompileJSToWasmWrapper(
1682 isolate_, &module_env, wasm_code, func_index);
1683 Handle<JSFunction> js_function = WrapExportCodeAsJSFunction(
1684 isolate_, wrapper_code, isolate_->factory()->empty_string(),
1685 module_->functions[func_index].sig, func_index, instance);
1686 exported_functions->set(func_index, *js_function);
1687 }
1688 inited_table.js_functions->set(i,
1689 exported_functions->get(func_index));
1743 } 1690 }
1744 } 1691 }
1745
1746 new_function_tables->set(static_cast<int>(index),
1747 *table_instance.dispatch_table);
1748
1749 Handle<FixedArray> all_dispatch_tables;
1750 if (!table_instance.table_object.is_null()) {
1751 // Get the existing dispatch table(s) with the WebAssembly.Table object.
1752 all_dispatch_tables = WasmJs::AddWasmTableDispatchTable(
1753 isolate_, table_instance.table_object, Handle<JSObject>::null(),
1754 index, Handle<FixedArray>::null());
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()) {
1779 Handle<Code> wasm_code(Code::cast(code_table->get(func_index)),
1780 isolate_);
1781 if (js_wrappers_[func_index].is_null()) {
1782 // No JSFunction entry yet exists for this function. Create one.
1783 // TODO(titzer): We compile JS->WASM wrappers for functions are
1784 // not exported but are in an exported table. This should be done
1785 // at module compile time and cached instead.
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);
1809 }
1810 }
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 }
1822 } 1692 }
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);
1834 } 1693 }
1835 }; 1694 };
1836 1695
1837 // Instantiates a WASM module, creating a WebAssembly.Instance from a 1696 // Instantiates a WASM module, creating a WebAssembly.Instance from a
1838 // WebAssembly.Module. 1697 // WebAssembly.Module.
1839 MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate, 1698 MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate,
1840 ErrorThrower* thrower, 1699 ErrorThrower* thrower,
1841 Handle<JSObject> wasm_module, 1700 Handle<JSObject> wasm_module,
1842 Handle<JSReceiver> ffi, 1701 Handle<JSReceiver> ffi,
1843 Handle<JSArrayBuffer> memory) { 1702 Handle<JSArrayBuffer> memory) {
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after
2094 GetInstanceMemory(isolate, instance); 1953 GetInstanceMemory(isolate, instance);
2095 Handle<JSArrayBuffer> buffer; 1954 Handle<JSArrayBuffer> buffer;
2096 if (!maybe_mem_buffer.ToHandle(&buffer)) { 1955 if (!maybe_mem_buffer.ToHandle(&buffer)) {
2097 return 0; 1956 return 0;
2098 } else { 1957 } else {
2099 return buffer->byte_length()->Number() / WasmModule::kPageSize; 1958 return buffer->byte_length()->Number() / WasmModule::kPageSize;
2100 } 1959 }
2101 } 1960 }
2102 1961
2103 uint32_t GetMaxInstanceMemorySize(Isolate* isolate, Handle<JSObject> instance) { 1962 uint32_t GetMaxInstanceMemorySize(Isolate* isolate, Handle<JSObject> instance) {
2104 uint32_t max_pages = WasmModule::kV8MaxPages; 1963 uint32_t max_pages = WasmModule::kMaxMemPages;
2105 Handle<Object> memory_object(instance->GetInternalField(kWasmMemObject), 1964 Handle<Object> memory_object(instance->GetInternalField(kWasmMemObject),
2106 isolate); 1965 isolate);
2107 if (memory_object->IsUndefined(isolate)) return max_pages; 1966 if (memory_object->IsUndefined(isolate)) return max_pages;
2108 return WasmJs::GetWasmMemoryMaximumSize(isolate, memory_object); 1967 return WasmJs::GetWasmMemoryMaximumSize(isolate, memory_object);
2109 } 1968 }
2110 1969
2111 int32_t wasm::GrowInstanceMemory(Isolate* isolate, Handle<JSObject> instance, 1970 int32_t wasm::GrowInstanceMemory(Isolate* isolate, Handle<JSObject> instance,
2112 uint32_t pages) { 1971 uint32_t pages) {
2113 if (!IsWasmInstance(*instance)) return -1; 1972 if (!IsWasmInstance(*instance)) return -1;
2114 if (pages == 0) return GetInstanceMemorySize(isolate, instance); 1973 if (pages == 0) return GetInstanceMemorySize(isolate, instance);
2115 uint32_t max_pages = GetMaxInstanceMemorySize(isolate, instance); 1974 uint32_t max_pages = GetMaxInstanceMemorySize(isolate, instance);
2116 if (WasmModule::kV8MaxPages < max_pages) return -1; 1975 if (WasmModule::kMaxMemPages < max_pages) return -1;
2117 1976
2118 Address old_mem_start = nullptr; 1977 Address old_mem_start = nullptr;
2119 uint32_t old_size = 0, new_size = 0; 1978 uint32_t old_size = 0, new_size = 0;
2120 1979
2121 MaybeHandle<JSArrayBuffer> maybe_mem_buffer = 1980 MaybeHandle<JSArrayBuffer> maybe_mem_buffer =
2122 GetInstanceMemory(isolate, instance); 1981 GetInstanceMemory(isolate, instance);
2123 Handle<JSArrayBuffer> old_buffer; 1982 Handle<JSArrayBuffer> old_buffer;
2124 if (!maybe_mem_buffer.ToHandle(&old_buffer) || 1983 if (!maybe_mem_buffer.ToHandle(&old_buffer) ||
2125 old_buffer->backing_store() == nullptr) { 1984 old_buffer->backing_store() == nullptr) {
2126 // If module object does not have linear memory associated with it, 1985 // If module object does not have linear memory associated with it,
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
2232 CHECK_NOT_NULL(result.val); 2091 CHECK_NOT_NULL(result.val);
2233 module = const_cast<WasmModule*>(result.val); 2092 module = const_cast<WasmModule*>(result.val);
2234 } 2093 }
2235 2094
2236 Handle<WasmModuleWrapper> module_wrapper = 2095 Handle<WasmModuleWrapper> module_wrapper =
2237 WasmModuleWrapper::New(isolate, module); 2096 WasmModuleWrapper::New(isolate, module);
2238 2097
2239 compiled_module->set_module_wrapper(module_wrapper); 2098 compiled_module->set_module_wrapper(module_wrapper);
2240 DCHECK(WasmCompiledModule::IsWasmCompiledModule(*compiled_module)); 2099 DCHECK(WasmCompiledModule::IsWasmCompiledModule(*compiled_module));
2241 } 2100 }
OLDNEW
« no previous file with comments | « src/wasm/wasm-module.h ('k') | test/cctest/wasm/wasm-run-utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698