diff --git a/src/mono/browser/runtime/runtime.c b/src/mono/browser/runtime/runtime.c index 2cf5b7605a5f7f..e31c8dfd7fef5b 100644 --- a/src/mono/browser/runtime/runtime.c +++ b/src/mono/browser/runtime/runtime.c @@ -209,14 +209,26 @@ get_native_to_interp (MonoMethod *method, void *extra_arg) const char *name = mono_assembly_name_get_name (aname); const char *class_name = mono_class_get_name (klass); const char *method_name = mono_method_get_name (method); - char key [128]; + char buf [128]; + char *key = &buf[0]; int len; + name = name ? name : ""; + + len = snprintf (key, sizeof(buf), "%s_%s_%s", name, class_name, method_name); + + if (len >= sizeof (buf)) { + // The key is too long, try again with a larger buffer + key = g_new (char, len + 1); + snprintf (key, len + 1, "%s_%s_%s", name, class_name, method_name); + } - assert (strlen (name) < 100); - snprintf (key, sizeof(key), "%s_%s_%s", name, class_name, method_name); char *fixedName = mono_fixup_symbol_name ("", key, ""); addr = wasm_dl_get_native_to_interp (fixedName, extra_arg); free (fixedName); + + if (key != buf) + free (key); + MONO_EXIT_GC_UNSAFE; return addr; } diff --git a/src/mono/wasm/Wasm.Build.Tests/PInvokeTableGeneratorTests.cs b/src/mono/wasm/Wasm.Build.Tests/PInvokeTableGeneratorTests.cs index eb0959fcdb6425..7753f9ac848a52 100644 --- a/src/mono/wasm/Wasm.Build.Tests/PInvokeTableGeneratorTests.cs +++ b/src/mono/wasm/Wasm.Build.Tests/PInvokeTableGeneratorTests.cs @@ -952,6 +952,7 @@ public void UCOWithSpecialCharacters(BuildArgs buildArgs, RunHost host, string i var runOutput = RunAndTestWasmApp(buildArgs, buildDir: _projectDir, expectedExitCode: 42, host: host, id: id); Assert.Contains("ManagedFunc returned 42", runOutput); + Assert.Contains("caught PlatformNotSupportedException", runOutput); } } } diff --git a/src/mono/wasm/testassets/Wasm.Buid.Tests.Programs/UnmanagedCallback.cs b/src/mono/wasm/testassets/Wasm.Buid.Tests.Programs/UnmanagedCallback.cs index 2157909c3d5fc6..4479118266da75 100644 --- a/src/mono/wasm/testassets/Wasm.Buid.Tests.Programs/UnmanagedCallback.cs +++ b/src/mono/wasm/testassets/Wasm.Buid.Tests.Programs/UnmanagedCallback.cs @@ -9,6 +9,13 @@ public unsafe static int Main(string[] args) Console.WriteLine($"main: {args.Length}"); Interop.UnmanagedFunc(); + + try { + nint fptr = Marshal.GetFunctionPointerForDelegate(new Action(() => Console.WriteLine("Managed method callee"))); + ((delegate* unmanaged)fptr)(); + } catch (PlatformNotSupportedException) { + Console.WriteLine("caught PlatformNotSupportedException"); + } return 42; } }