From e11cee846f3c197d46a09189cb74cfb01c3571db Mon Sep 17 00:00:00 2001 From: Mircea Trofin Date: Tue, 5 Sep 2017 08:50:14 -0700 Subject: [PATCH] [wasm] Rename the APIs that unwrap exports from other instances. The renames capture what the item being unwrapped is - it's always a js-to-wasm wrapper, which is more closely captured by "export" rather than "import". Bug: Change-Id: Iffc3d8cb9037afc2d32885301fd13fc12b8277ce Reviewed-on: https://chromium-review.googlesource.com/648005 Reviewed-by: Clemens Hammacher Commit-Queue: Mircea Trofin Cr-Commit-Position: refs/heads/master@{#47827} --- src/wasm/module-compiler.cc | 20 ++++++++++---------- src/wasm/wasm-module.cc | 19 +++++++++++-------- src/wasm/wasm-module.h | 8 +++++--- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/wasm/module-compiler.cc b/src/wasm/module-compiler.cc index 1058bf94aa..f2ec4c141c 100644 --- a/src/wasm/module-compiler.cc +++ b/src/wasm/module-compiler.cc @@ -491,11 +491,11 @@ bool in_bounds(uint32_t offset, uint32_t size, uint32_t upper) { using WasmInstanceMap = IdentityMap, FreeStoreAllocationPolicy>; -Handle UnwrapOrCompileImportWrapper( +Handle UnwrapExportOrCompileImportWrapper( Isolate* isolate, int index, FunctionSig* sig, Handle target, Handle module_name, MaybeHandle import_name, ModuleOrigin origin, WasmInstanceMap* imported_instances) { - WasmFunction* other_func = GetWasmFunctionForImportWrapper(isolate, target); + WasmFunction* other_func = GetWasmFunctionForExport(isolate, target); if (other_func) { if (!sig->Equals(other_func->sig)) return Handle::null(); // Signature matched. Unwrap the import wrapper and return the raw wasm @@ -504,7 +504,7 @@ Handle UnwrapOrCompileImportWrapper( Handle imported_instance( Handle::cast(target)->instance(), isolate); imported_instances->Set(imported_instance, imported_instance); - return UnwrapImportWrapper(target); + return UnwrapExportWrapper(Handle::cast(target)); } // No wasm function or being debugged. Compile a new wrapper for the new // signature. @@ -1335,17 +1335,17 @@ int InstanceBuilder::ProcessImports(Handle code_table, return -1; } - Handle import_wrapper = UnwrapOrCompileImportWrapper( + Handle import_code = UnwrapExportOrCompileImportWrapper( isolate_, index, module_->functions[import.index].sig, Handle::cast(value), module_name, import_name, module_->origin(), &imported_wasm_instances); - if (import_wrapper.is_null()) { + if (import_code.is_null()) { ReportLinkError("imported function does not match the expected type", index, module_name, import_name); return -1; } - code_table->set(num_imported_functions, *import_wrapper); - RecordStats(*import_wrapper, counters()); + code_table->set(num_imported_functions, *import_code); + RecordStats(*import_code, counters()); num_imported_functions++; break; } @@ -1403,8 +1403,7 @@ int InstanceBuilder::ProcessImports(Handle code_table, for (int i = 0; i < table_size; ++i) { Handle val(table_instance.js_wrappers->get(i), isolate_); if (!val->IsJSFunction()) continue; - WasmFunction* function = - GetWasmFunctionForImportWrapper(isolate_, val); + WasmFunction* function = GetWasmFunctionForExport(isolate_, val); if (function == nullptr) { thrower_->LinkError("table import %d[%d] is not a wasm function", index, i); @@ -1412,7 +1411,8 @@ int InstanceBuilder::ProcessImports(Handle code_table, } int sig_index = table.map.FindOrInsert(function->sig); table_instance.signature_table->set(i, Smi::FromInt(sig_index)); - table_instance.function_table->set(i, *UnwrapImportWrapper(val)); + table_instance.function_table->set( + i, *UnwrapExportWrapper(Handle::cast(val))); } num_imported_tables++; diff --git a/src/wasm/wasm-module.cc b/src/wasm/wasm-module.cc index c43226a767..572b326c47 100644 --- a/src/wasm/wasm-module.cc +++ b/src/wasm/wasm-module.cc @@ -367,8 +367,8 @@ WasmInstanceObject* GetOwningWasmInstance(Code* code) { WasmModule::WasmModule(std::unique_ptr owned) : signature_zone(std::move(owned)) {} -WasmFunction* GetWasmFunctionForImportWrapper(Isolate* isolate, - Handle target) { +WasmFunction* GetWasmFunctionForExport(Isolate* isolate, + Handle target) { if (target->IsJSFunction()) { Handle func = Handle::cast(target); if (func->code()->kind() == Code::JS_TO_WASM_FUNCTION) { @@ -381,9 +381,9 @@ WasmFunction* GetWasmFunctionForImportWrapper(Isolate* isolate, return nullptr; } -Handle UnwrapImportWrapper(Handle import_wrapper) { - Handle func = Handle::cast(import_wrapper); - Handle export_wrapper_code = handle(func->code()); +Handle UnwrapExportWrapper(Handle export_wrapper) { + Handle export_wrapper_code = handle(export_wrapper->code()); + DCHECK_EQ(export_wrapper_code->kind(), Code::JS_TO_WASM_FUNCTION); int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET); for (RelocIterator it(*export_wrapper_code, mask);; it.next()) { DCHECK(!it.done()); @@ -447,11 +447,14 @@ void TableSet(ErrorThrower* thrower, Isolate* isolate, WasmFunction* wasm_function = nullptr; Handle code = Handle::null(); - Handle value = handle(isolate->heap()->null_value()); + Handle value = isolate->factory()->null_value(); if (!function.is_null()) { - wasm_function = GetWasmFunctionForImportWrapper(isolate, function); - code = UnwrapImportWrapper(function); + wasm_function = GetWasmFunctionForExport(isolate, function); + // The verification that {function} is an export was done + // by the caller. + DCHECK_NOT_NULL(wasm_function); + code = UnwrapExportWrapper(function); value = Handle::cast(function); } diff --git a/src/wasm/wasm-module.h b/src/wasm/wasm-module.h index 63edd5f865..23de3b1336 100644 --- a/src/wasm/wasm-module.h +++ b/src/wasm/wasm-module.h @@ -339,12 +339,14 @@ void DetachWebAssemblyMemoryBuffer(Isolate* isolate, Handle buffer, bool free_memory); +// If the target is an export wrapper, return the {WasmFunction*} corresponding +// to the wrapped wasm function; in all other cases, return nullptr. // The returned pointer is owned by the wasm instance target belongs to. The // result is alive as long as the instance exists. -WasmFunction* GetWasmFunctionForImportWrapper(Isolate* isolate, - Handle target); +WasmFunction* GetWasmFunctionForExport(Isolate* isolate, Handle target); -Handle UnwrapImportWrapper(Handle import_wrapper); +// {export_wrapper} is known to be an export. +Handle UnwrapExportWrapper(Handle export_wrapper); void TableSet(ErrorThrower* thrower, Isolate* isolate, Handle table, int64_t index,