From ce02b539080638cb85f00c6696638428c0ffad49 Mon Sep 17 00:00:00 2001 From: Meri Khamoyan <96171496+mkhamoyan@users.noreply.github.com> Date: Wed, 26 Jun 2024 17:03:20 +0200 Subject: [PATCH] [wasi][mono] Fix conflicting naming for wasm import/export (#103881) Add namespace for entrypoint --- src/mono/browser/runtime/runtime.c | 4 ++-- src/mono/browser/runtime/runtime.h | 2 +- .../WasmAppBuilder/PInvokeTableGenerator.cs | 17 ++++++++++++----- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/mono/browser/runtime/runtime.c b/src/mono/browser/runtime/runtime.c index 436c3a2958632..2cf5b7605a5f7 100644 --- a/src/mono/browser/runtime/runtime.c +++ b/src/mono/browser/runtime/runtime.c @@ -370,13 +370,13 @@ mono_wasm_assembly_find_method (MonoClass *klass, const char *name, int argument * This wrapper ensures that the interpreter initializes the pointers. */ void -mono_wasm_marshal_get_managed_wrapper (const char* assemblyName, const char* typeName, const char* methodName, int num_params) +mono_wasm_marshal_get_managed_wrapper (const char* assemblyName, const char* namespaceName, const char* typeName, const char* methodName, int num_params) { MonoError error; mono_error_init (&error); MonoAssembly* assembly = mono_wasm_assembly_load (assemblyName); assert (assembly); - MonoClass* class = mono_wasm_assembly_find_class (assembly, "", typeName); + MonoClass* class = mono_wasm_assembly_find_class (assembly, namespaceName, typeName); assert (class); MonoMethod* method = mono_wasm_assembly_find_method (class, methodName, num_params); assert (method); diff --git a/src/mono/browser/runtime/runtime.h b/src/mono/browser/runtime/runtime.h index 380c1fcd15b5f..f630762ded217 100644 --- a/src/mono/browser/runtime/runtime.h +++ b/src/mono/browser/runtime/runtime.h @@ -19,7 +19,7 @@ MonoDomain *mono_wasm_load_runtime_common (int debug_level, MonoLogCallback log_ MonoAssembly *mono_wasm_assembly_load (const char *name); MonoClass *mono_wasm_assembly_find_class (MonoAssembly *assembly, const char *namespace, const char *name); MonoMethod *mono_wasm_assembly_find_method (MonoClass *klass, const char *name, int arguments); -void mono_wasm_marshal_get_managed_wrapper (const char* assemblyName, const char* typeName, const char* methodName, int num_params); +void mono_wasm_marshal_get_managed_wrapper (const char* assemblyName, const char* namespaceName, const char* typeName, const char* methodName, int num_params); int initialize_runtime (); #endif diff --git a/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs b/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs index 2515c70ba65ea..19e4181b2217e 100644 --- a/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs +++ b/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs @@ -181,7 +181,8 @@ private string CEntryPoint(PInvoke pinvoke) if (pinvoke.WasmLinkage) { // We mangle the name to avoid collisions with symbols in other modules - return _fixupSymbolName($"{pinvoke.Module}#{pinvoke.EntryPoint}"); + string namespaceName = pinvoke.Method.DeclaringType?.Namespace ?? string.Empty; + return _fixupSymbolName($"{namespaceName}#{pinvoke.Module}#{pinvoke.EntryPoint}"); } return _fixupSymbolName(pinvoke.EntryPoint); } @@ -295,8 +296,13 @@ private string CEntryPoint(PInvokeCallback export) } var method = export.Method; - // EntryPoint wasn't specified generate a name for the entry point - return _fixupSymbolName($"wasm_native_to_interp_{method.DeclaringType!.Module!.Assembly!.GetName()!.Name!}_{method.DeclaringType.Name}_{method.Name}"); + string namespaceName = method.DeclaringType?.Namespace ?? string.Empty; + string assemblyName = method.DeclaringType?.Module?.Assembly?.GetName()?.Name ?? string.Empty; + string declaringTypeName = method.DeclaringType?.Name ?? string.Empty; + + string entryPoint = $"wasm_native_to_interp_{namespaceName}_{assemblyName}_{declaringTypeName}_{method.Name}"; + + return _fixupSymbolName(entryPoint); } private string DelegateKey(PInvokeCallback export) @@ -390,10 +396,11 @@ private void EmitNativeToInterp(StreamWriter w, List callbacks) sb.Append($" if (!(WasmInterpEntrySig_{cb_index})wasm_native_to_interp_ftndescs [{cb_index}].func) {{\n"); var assemblyFullName = cb.Method.DeclaringType == null ? "" : cb.Method.DeclaringType.Assembly.FullName; var assemblyName = assemblyFullName != null && assemblyFullName.Split(',').Length > 0 ? assemblyFullName.Split(',')[0].Trim() : ""; - var typeName = cb.Method.DeclaringType == null || cb.Method.DeclaringType.FullName == null ? "" : cb.Method.DeclaringType.FullName; + var namespaceName = cb.Method.DeclaringType == null ? "" : cb.Method.DeclaringType.Namespace; + var typeName = cb.Method.DeclaringType == null || cb.Method.DeclaringType.Name == null ? "" : cb.Method.DeclaringType.Name; var methodName = cb.Method.Name; int numParams = method.GetParameters().Length; - sb.Append($" mono_wasm_marshal_get_managed_wrapper (\"{assemblyName}\", \"{typeName}\", \"{methodName}\", {numParams});\n"); + sb.Append($" mono_wasm_marshal_get_managed_wrapper (\"{assemblyName}\",\"{namespaceName}\", \"{typeName}\", \"{methodName}\", {numParams});\n"); sb.Append($" }}\n"); sb.Append($" ((WasmInterpEntrySig_{cb_index})wasm_native_to_interp_ftndescs [{cb_index}].func) (");