diff --git a/src/wasm/module-compiler.cc b/src/wasm/module-compiler.cc index f61e6b1cb6..9d804cf547 100644 --- a/src/wasm/module-compiler.cc +++ b/src/wasm/module-compiler.cc @@ -871,7 +871,9 @@ bool compile_lazy(const WasmModule* module) { } void FlushICache(const wasm::NativeModule* native_module) { - for (uint32_t i = 0, e = native_module->function_count(); i < e; ++i) { + for (uint32_t i = native_module->num_imported_functions(), + e = native_module->function_count(); + i < e; ++i) { const wasm::WasmCode* code = native_module->code(i); if (code == nullptr) continue; Assembler::FlushICache(code->instructions().start(), @@ -905,7 +907,9 @@ void RecordStats(const wasm::WasmCode* code, Counters* counters) { } void RecordStats(const wasm::NativeModule* native_module, Counters* counters) { - for (uint32_t i = 0, e = native_module->function_count(); i < e; ++i) { + for (uint32_t i = native_module->num_imported_functions(), + e = native_module->function_count(); + i < e; ++i) { const wasm::WasmCode* code = native_module->code(i); if (code != nullptr) RecordStats(code, counters); } @@ -2125,9 +2129,6 @@ int InstanceBuilder::ProcessImports(Handle instance) { ImportedFunctionEntry entry(instance, func_index); Address imported_target = imported_function->GetWasmCallTarget(); entry.set_wasm_to_wasm(*imported_instance, imported_target); - // TODO(clemensh): Remove this. NativeModule must be instance - // independent. - native_module->set_code(func_index, imported_function->GetWasmCode()); } else { // The imported function is a callable. Handle js_receiver(JSReceiver::cast(*value), isolate_); @@ -2651,20 +2652,20 @@ void InstanceBuilder::LoadTableSegments(Handle instance) { // Update the local dispatch table first. uint32_t sig_id = module_->signature_ids[function->sig_index]; - Address call_target = - native_module->GetCallTargetForFunction(func_index); - + WasmInstanceObject* target_instance = *instance; + Address call_target; const bool is_import = func_index < module_->num_imported_functions; if (is_import) { - // Imported functions have the target instance put into the IFT. - WasmInstanceObject* target_instance = - ImportedFunctionEntry(instance, func_index).instance(); - IndirectFunctionTableEntry(instance, table_index) - .set(sig_id, target_instance, call_target); + // For imported calls, take target instance and address from the + // import table. + ImportedFunctionEntry entry(instance, func_index); + target_instance = entry.instance(); + call_target = entry.target(); } else { - IndirectFunctionTableEntry(instance, table_index) - .set(sig_id, *instance, call_target); + call_target = native_module->GetCallTargetForFunction(func_index); } + IndirectFunctionTableEntry(instance, table_index) + .set(sig_id, target_instance, call_target); if (!table_instance.table_object.is_null()) { // Update the table object's other dispatch tables. diff --git a/src/wasm/wasm-code-manager.h b/src/wasm/wasm-code-manager.h index bb1320a73b..273f1ed425 100644 --- a/src/wasm/wasm-code-manager.h +++ b/src/wasm/wasm-code-manager.h @@ -252,12 +252,16 @@ class V8_EXPORT_PRIVATE NativeModule final { WasmCode* code(uint32_t index) const { DCHECK_LT(index, function_count()); + DCHECK_LE(num_imported_functions(), index); return code_table_[index]; } - void set_code(uint32_t index, WasmCode* wasm_code) { + // TODO(clemensh): Remove this method once we have the jump table + // (crbug.com/v8/7758). + void SetCodeForTesting(uint32_t index, WasmCode* code) { DCHECK_LT(index, function_count()); - code_table_[index] = wasm_code; + DCHECK_LE(num_imported_functions(), index); + code_table_[index] = code; } bool has_code(uint32_t index) const { diff --git a/src/wasm/wasm-debug.cc b/src/wasm/wasm-debug.cc index 8b66e9fa57..ea8a14d2fe 100644 --- a/src/wasm/wasm-debug.cc +++ b/src/wasm/wasm-debug.cc @@ -553,20 +553,16 @@ wasm::InterpreterHandle* GetInterpreterHandleOrNull(WasmDebugInfo* debug_info) { return Managed::cast(handle_obj)->raw(); } -int GetNumFunctions(WasmInstanceObject* instance) { - size_t num_functions = - instance->module_object()->shared()->module()->functions.size(); - DCHECK_GE(kMaxInt, num_functions); - return static_cast(num_functions); -} - Handle GetOrCreateInterpretedFunctions( Isolate* isolate, Handle debug_info) { Handle obj(debug_info->interpreted_functions(), isolate); if (!obj->IsUndefined(isolate)) return Handle::cast(obj); - Handle new_arr = isolate->factory()->NewFixedArray( - GetNumFunctions(debug_info->wasm_instance())); + int num_functions = debug_info->wasm_instance() + ->compiled_module() + ->GetNativeModule() + ->function_count(); + Handle new_arr = isolate->factory()->NewFixedArray(num_functions); debug_info->set_interpreted_functions(*new_arr); return new_arr; } @@ -603,9 +599,12 @@ void RedirectCallsitesInInstance(Isolate* isolate, WasmInstanceObject* instance, CodeRelocationMap* map) { DisallowHeapAllocation no_gc; // Redirect all calls in wasm functions. - for (uint32_t i = 0, e = GetNumFunctions(instance); i < e; ++i) { - wasm::WasmCode* code = - instance->compiled_module()->GetNativeModule()->code(i); + wasm::NativeModule* native_module = + instance->compiled_module()->GetNativeModule(); + for (uint32_t i = native_module->num_imported_functions(), + e = native_module->function_count(); + i < e; ++i) { + wasm::WasmCode* code = native_module->code(i); RedirectCallsitesInCode(isolate, code, map); } // TODO(6668): Find instances that imported our code and also patch those. diff --git a/test/cctest/wasm/test-run-wasm.cc b/test/cctest/wasm/test-run-wasm.cc index 121e478224..b460c3e297 100644 --- a/test/cctest/wasm/test-run-wasm.cc +++ b/test/cctest/wasm/test-run-wasm.cc @@ -3463,8 +3463,9 @@ TEST(Liftoff_prologue) { CHECK_EQ(10, r.Call(1, 2, 3, 4)); // Update the native_module to contain the "optimized" code ({sub_locals}). - native_module->set_code(add_compiler.function_index(), - native_module->code(sub_compiler.function_index())); + native_module->SetCodeForTesting( + add_compiler.function_index(), + native_module->code(sub_compiler.function_index())); // Second run should execute {add_locals}, which should detect that // the code was updated, and run {sub_locals}.