Skip to content

Commit

Permalink
Handle exceptional cases when looking up unmanaged delegates
Browse files Browse the repository at this point in the history
  • Loading branch information
lewing committed Aug 28, 2024
1 parent 4adc0b4 commit c7aa26b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/mono/browser/runtime/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 (len >= sizeof (buf))
free (key);

MONO_EXIT_GC_UNSAFE;
return addr;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>)fptr)();
} catch (PlatformNotSupportedException) {
Console.WriteLine("caught PlatformNotSupportedException");
}
return 42;
}
}
Expand Down

0 comments on commit c7aa26b

Please sign in to comment.