Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mono][wasm] Fix function signature mismatch in m2n invoke #101106

Merged
merged 20 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions src/mono/browser/runtime/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,33 @@ init_icall_table (void)
#endif
}

// Keep synced with FixupSymbolName from src/tasks/Common/Utils.cs
static void fixupSymbolName(char *key, char *fixedName) {
mkhamoyan marked this conversation as resolved.
Show resolved Hide resolved
int sb_index = 0;
int len = strlen (key);

for (int i = 0; i < len; ++i) {
unsigned char b = key[i];
if ((b >= '0' && b <= '9') ||
(b >= 'a' && b <= 'z') ||
(b >= 'A' && b <= 'Z') ||
(b == '_')) {
fixedName[sb_index++] = b;
}
else if (b == '.' || b == '-' || b == '+' || b == '<' || b == '>') {
fixedName[sb_index++] = '_';
}
else {
// Append the hexadecimal representation of b between underscores
sprintf(&fixedName[sb_index], "_%X_", b);
sb_index += 4; // Move the index after the appended hexadecimal characters
}
}

// Null-terminate the fixedName string
fixedName[sb_index] = '\0';
}

/*
* get_native_to_interp:
*
Expand All @@ -209,17 +236,13 @@ get_native_to_interp (MonoMethod *method, void *extra_arg)
const char *class_name = mono_class_get_name (klass);
const char *method_name = mono_method_get_name (method);
char key [128];
char fixedName[256];
mkhamoyan marked this conversation as resolved.
Show resolved Hide resolved
int len;

assert (strlen (name) < 100);
snprintf (key, sizeof(key), "%s_%s_%s", name, class_name, method_name);
len = strlen (key);
for (int i = 0; i < len; ++i) {
if (key [i] == '.')
key [i] = '_';
}

addr = wasm_dl_get_native_to_interp (key, extra_arg);
fixupSymbolName(key, fixedName);
addr = wasm_dl_get_native_to_interp (fixedName, extra_arg);
MONO_EXIT_GC_UNSAFE;
return addr;
}
Expand Down
4 changes: 3 additions & 1 deletion src/mono/wasm/Wasm.Build.Tests/Common/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ public static void AssertEqual(object expected, object actual, string label)
$"[{label}]\n");
}

private static readonly char[] s_charsToReplace = new[] { '.', '-', '+' };
private static readonly char[] s_charsToReplace = new[] { '.', '-', '+', '<', '>' };
// Keep synced with FixupSymbolName from src/tasks/Common/Utils.cs
// and src/mono/browser/runtime/runtime.c
public static string FixupSymbolName(string name)
{
UTF8Encoding utf8 = new();
Expand Down
26 changes: 26 additions & 0 deletions src/mono/wasm/Wasm.Build.Tests/PInvokeTableGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -884,5 +884,31 @@ public void EnsureWasmAbiRulesAreFollowedInAOT(BuildArgs buildArgs, RunHost host
[BuildAndRun(host: RunHost.Chrome, aot: false)]
public void EnsureWasmAbiRulesAreFollowedInInterpreter(BuildArgs buildArgs, RunHost host, string id) =>
EnsureWasmAbiRulesAreFollowed(buildArgs, host, id);

[Theory]
[BuildAndRun(host: RunHost.Chrome, aot: false)]
public void UCOWithSpecialCharacters(BuildArgs buildArgs, RunHost host, string id)
{
var extraProperties = "<AllowUnsafeBlocks>true</AllowUnsafeBlocks>";
var extraItems = @"<NativeFileReference Include=""local.c"" />";

buildArgs = ExpandBuildArgs(buildArgs,
extraItems: extraItems,
extraProperties: extraProperties);

(string libraryDir, string output) = BuildProject(buildArgs,
id: id,
new BuildProjectOptions(
InitProject: () =>
{
File.Copy(Path.Combine(BuildEnvironment.TestAssetsPath, "Wasm.Buid.Tests.Programs", "UnmanagedCallback.cs"), Path.Combine(_projectDir!, "Program.cs"));
File.Copy(Path.Combine(BuildEnvironment.TestAssetsPath, "native-libs", "local.c"), Path.Combine(_projectDir!, "local.c"));
},
Publish: true,
DotnetWasmFromRuntimePack: false));

var runOutput = RunAndTestWasmApp(buildArgs, buildDir: _projectDir, expectedExitCode: 42, host: host, id: id);
Assert.Contains("ManagedFunc returned 42", runOutput);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Runtime.InteropServices;

public unsafe partial class Test
{
public unsafe static int Main(string[] args)
{
((IntPtr)(delegate* unmanaged<int,int>)&Interop.Managed8\u4F60Func).ToString();

Console.WriteLine($"main: {args.Length}");
Interop.UnmanagedFunc();
return 42;
}
}

file partial class Interop
{
[UnmanagedCallersOnly(EntryPoint = "ManagedFunc")]
public static int Managed8\u4F60Func(int number)
{
// called from UnmanagedFunc
Console.WriteLine($"Managed8\u4F60Func({number}) -> 42");
return 42;
}

[DllImport("local", EntryPoint = "UnmanagedFunc")]
public static extern void UnmanagedFunc(); // calls ManagedFunc
}
10 changes: 10 additions & 0 deletions src/mono/wasm/testassets/native-libs/local.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <stdio.h>
int ManagedFunc(int number);

void UnmanagedFunc()
{
int ret = 0;
printf("UnmanagedFunc calling ManagedFunc\n");
ret = ManagedFunc(123);
printf("ManagedFunc returned %d\n", ret);
}
21 changes: 1 addition & 20 deletions src/tasks/AotCompilerTask/MonoAOTCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1237,26 +1237,7 @@ private string FixupSymbolName(string name)
if (_symbolNameFixups.TryGetValue(name, out string? fixedName))
return fixedName;

UTF8Encoding utf8 = new();
byte[] bytes = utf8.GetBytes(name);
StringBuilder sb = new();

foreach (byte b in bytes)
{
if ((b >= (byte)'0' && b <= (byte)'9') ||
(b >= (byte)'a' && b <= (byte)'z') ||
(b >= (byte)'A' && b <= (byte)'Z') ||
(b == (byte)'_'))
{
sb.Append((char)b);
}
else
{
sb.Append('_');
}
}

fixedName = sb.ToString();
fixedName = Utils.FixupSymbolName(name);
_symbolNameFixups[name] = fixedName;
return fixedName;
}
Expand Down
32 changes: 32 additions & 0 deletions src/tasks/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Reflection.Metadata;
using System.Security.Cryptography;
using System.Text;
using System.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

Expand All @@ -33,6 +34,8 @@ public enum HashEncodingType

private static readonly object s_SyncObj = new object();

private static readonly char[] s_charsToReplace = new[] { '.', '-', '+', '<', '>' };

public static string GetEmbeddedResource(string file)
{
using Stream stream = typeof(Utils).Assembly
Expand Down Expand Up @@ -411,4 +414,33 @@ private static bool IsManagedAssembly(PEReader peReader)
return false;
}
}

// Keep synced with fixupSymbolName from src/mono/browser/runtime/runtime.c
public static string FixupSymbolName(string name)
{
UTF8Encoding utf8 = new();
byte[] bytes = utf8.GetBytes(name);
StringBuilder sb = new();

foreach (byte b in bytes)
{
if ((b >= (byte)'0' && b <= (byte)'9') ||
(b >= (byte)'a' && b <= (byte)'z') ||
(b >= (byte)'A' && b <= (byte)'Z') ||
(b == (byte)'_'))
{
sb.Append((char)b);
}
else if (s_charsToReplace.Contains((char)b))
{
sb.Append('_');
}
else
{
sb.Append($"_{b:X}_");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure of the exact constraints for this name mapping algorithm but could we just replace all chars outside of a-z/0-9 with underscore? that would allow us to not allocate in the native side

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed when mapping two assemblies to the same name.
For example having an app project that references a library project named in a way that maps to the same name, but is different before fixup.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah so e.g. HelloÖProject.DoSomething() and HelloÜProject.DoSomething() would both map to Hello_Project_DoSomething, makes sense.

Copy link
Member

@akoeplinger akoeplinger Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure whether it'd be strictly better but we could also collect the special chars and append them at the end or return them separately. that'd have the benefit of not needing to allocate on the native side when the string doesn't have special chars

@lambdageek for thoughts

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how adding the characters in a suffix would make a difference.

On the native side I guess it would be nice to have a function that precomputes the required space for the entire mangled name and just does a single allocation. (ie: get rid of the g_strdup_printf with the prefix and the name together with the mangling loop - and just make a helper that that does a single allocation for the final mangled name)

}
}

return sb.ToString();
}
}
27 changes: 1 addition & 26 deletions src/tasks/WasmAppBuilder/ManagedToNativeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ public class ManagedToNativeGenerator : Task
[Output]
public string[]? FileWrites { get; private set; }

private static readonly char[] s_charsToReplace = new[] { '.', '-', '+', '<', '>' };

public override bool Execute()
{
if (Assemblies!.Length == 0)
Expand Down Expand Up @@ -108,30 +106,7 @@ string FixupSymbolName(string name)
if (_symbolNameFixups.TryGetValue(name, out string? fixedName))
return fixedName;

UTF8Encoding utf8 = new();
byte[] bytes = utf8.GetBytes(name);
StringBuilder sb = new();

foreach (byte b in bytes)
{
if ((b >= (byte)'0' && b <= (byte)'9') ||
(b >= (byte)'a' && b <= (byte)'z') ||
(b >= (byte)'A' && b <= (byte)'Z') ||
(b == (byte)'_'))
{
sb.Append((char)b);
}
else if (s_charsToReplace.Contains((char)b))
{
sb.Append('_');
}
else
{
sb.Append($"_{b:X}_");
}
}

fixedName = sb.ToString();
fixedName = Utils.FixupSymbolName(name);
_symbolNameFixups[name] = fixedName;
return fixedName;
}
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ private string DelegateKey(PInvokeCallback export)
// it needs to match the key generated in get_native_to_interp
var method = export.Method;
string module_symbol = method.DeclaringType!.Module!.Assembly!.GetName()!.Name!;
return $"\"{module_symbol}_{method.DeclaringType.Name}_{method.Name}\"".Replace('.', '_');
return $"\"{_fixupSymbolName($"{module_symbol}_{method.DeclaringType.Name}_{method.Name}")}\"";
}

#pragma warning disable SYSLIB1045 // framework doesn't support GeneratedRegexAttribute
Expand Down
Loading