Skip to content

Commit c7aa26b

Browse files
committed
Handle exceptional cases when looking up unmanaged delegates
1 parent 4adc0b4 commit c7aa26b

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/mono/browser/runtime/runtime.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,26 @@ get_native_to_interp (MonoMethod *method, void *extra_arg)
209209
const char *name = mono_assembly_name_get_name (aname);
210210
const char *class_name = mono_class_get_name (klass);
211211
const char *method_name = mono_method_get_name (method);
212-
char key [128];
212+
char buf [128];
213+
char *key = &buf[0];
213214
int len;
215+
name = name ? name : "";
216+
217+
len = snprintf (key, sizeof(buf), "%s_%s_%s", name, class_name, method_name);
218+
219+
if (len >= sizeof (buf)) {
220+
// The key is too long, try again with a larger buffer
221+
key = g_new (char, len + 1);
222+
snprintf (key, len + 1, "%s_%s_%s", name, class_name, method_name);
223+
}
214224

215-
assert (strlen (name) < 100);
216-
snprintf (key, sizeof(key), "%s_%s_%s", name, class_name, method_name);
217225
char *fixedName = mono_fixup_symbol_name ("", key, "");
218226
addr = wasm_dl_get_native_to_interp (fixedName, extra_arg);
219227
free (fixedName);
228+
229+
if (len >= sizeof (buf))
230+
free (key);
231+
220232
MONO_EXIT_GC_UNSAFE;
221233
return addr;
222234
}

src/mono/wasm/Wasm.Build.Tests/PInvokeTableGeneratorTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,7 @@ public void UCOWithSpecialCharacters(BuildArgs buildArgs, RunHost host, string i
952952

953953
var runOutput = RunAndTestWasmApp(buildArgs, buildDir: _projectDir, expectedExitCode: 42, host: host, id: id);
954954
Assert.Contains("ManagedFunc returned 42", runOutput);
955+
Assert.Contains("caught PlatformNotSupportedException", runOutput);
955956
}
956957
}
957958
}

src/mono/wasm/testassets/Wasm.Buid.Tests.Programs/UnmanagedCallback.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ public unsafe static int Main(string[] args)
99

1010
Console.WriteLine($"main: {args.Length}");
1111
Interop.UnmanagedFunc();
12+
13+
try {
14+
nint fptr = Marshal.GetFunctionPointerForDelegate(new Action(() => Console.WriteLine("Managed method callee")));
15+
((delegate* unmanaged<void>)fptr)();
16+
} catch (PlatformNotSupportedException) {
17+
Console.WriteLine("caught PlatformNotSupportedException");
18+
}
1219
return 42;
1320
}
1421
}

0 commit comments

Comments
 (0)