From 91d381fd3dbfe03806c4e7cda679bb148da493e1 Mon Sep 17 00:00:00 2001 From: Ankit Jain Date: Thu, 15 Jun 2023 17:13:36 +0000 Subject: [PATCH 1/4] [wasm] Fix inclusion of `marshal-ilgen` Issue: `marshal-ilgen` component never included Reason: The item inclusion was being done *outside* any targets, which would get evaluated *before* any targets are run. But the item that it depends on to decide whether to include `marshal-ilgen` is in target `_ScanAssembliesDecideLightweightMarshaler`. This broke microbenchmarks with: ``` Error: [MONO] * Assertion at /__w/1/s/src/mono/mono/component/marshal-ilgen-stub.c:28, condition `!m_type_is_byref(t)' not met at ft (/home/helixbot/work/96640802/w/B9C40A15/e/performance/artifacts/bin/for-running/MicroBenchmarks/fa21f9df-08d0-43a4-b28d-0078821ea492/bin/net8.0/browser-wasm/AppBundle/dotnet.runtime.js:3:12422) at mt (/home/helixbot/work/96640802/w/B9C40A15/e/performance/artifacts/bin/for-running/MicroBenchmarks/fa21f9df-08d0-43a4-b28d-0078821ea492/bin/net8.0/browser-wasm/AppBundle/dotnet.runtime.js:3:12676) at wasm_trace_logger (wasm://wasm/00a5f7d6:wasm-function[7879]:0x1e41d0) at eglib_log_adapter (wasm://wasm/00a5f7d6:wasm-function[581]:0x37fbb) at monoeg_g_logv_nofree (wasm://wasm/00a5f7d6:wasm-function[504]:0x35f57) at monoeg_assertion_message (wasm://wasm/00a5f7d6:wasm-function[508]:0x36078) at mono_assertion_message (wasm://wasm/00a5f7d6:wasm-function[510]:0x360bb) at stub_emit_marshal_ilgen (wasm://wasm/00a5f7d6:wasm-function[1300]:0x66db1) at mono_emit_marshal (wasm://wasm/00a5f7d6:wasm-function[1820]:0x8990b) at emit_native_wrapper_ilgen (wasm://wasm/00a5f7d6:wasm-function[2651]:0xb86de) ``` --- src/mono/wasm/build/WasmApp.Native.targets | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/mono/wasm/build/WasmApp.Native.targets b/src/mono/wasm/build/WasmApp.Native.targets index 0c8a017754e7db..568f23f6568b06 100644 --- a/src/mono/wasm/build/WasmApp.Native.targets +++ b/src/mono/wasm/build/WasmApp.Native.targets @@ -35,9 +35,6 @@ <_MonoComponent Include="hot_reload;debugger" /> - - <_MonoComponent Include="marshal-ilgen" /> - @@ -684,12 +681,16 @@ - + <_AssembliesToScan Include="@(_WasmAssembliesInternal)" /> - + + + + <_MonoComponent Include="marshal-ilgen" /> + From a5cdacc69c82ca134c51e17de05c030cf2077f5d Mon Sep 17 00:00:00 2001 From: Ankit Jain Date: Thu, 15 Jun 2023 22:16:22 +0000 Subject: [PATCH 2/4] [wasm] WBT: Use sdk 8.0.100-preview.6.23314.19 .. to workaround https://github.com/dotnet/runtime/issues/87647 . --- eng/Versions.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/Versions.props b/eng/Versions.props index d3abb3f2fa062b..a1b6304e770bb3 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -204,7 +204,7 @@ 2.45.0 2.45.0 - + 8.0.100-preview.6.23314.19 1.1.2-beta1.23205.1 7.0.0-preview-20221010.1 From 8aa8457ca686d824f88aa4386a315f8223d2eabd Mon Sep 17 00:00:00 2001 From: Ankit Jain Date: Thu, 15 Jun 2023 23:58:15 +0000 Subject: [PATCH 3/4] [wasm] Add test for marshal-ilgen - wbt --- .../wasm/Wasm.Build.Tests/NativeBuildTests.cs | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/mono/wasm/Wasm.Build.Tests/NativeBuildTests.cs b/src/mono/wasm/Wasm.Build.Tests/NativeBuildTests.cs index b388d2cd99854f..efe75b94352dcc 100644 --- a/src/mono/wasm/Wasm.Build.Tests/NativeBuildTests.cs +++ b/src/mono/wasm/Wasm.Build.Tests/NativeBuildTests.cs @@ -180,5 +180,41 @@ public void BuildWithUndefinedNativeSymbol(bool allowUndefined) Assert.Contains("Use '-p:WasmAllowUndefinedSymbols=true' to allow undefined symbols", result.Output); } } + + [Theory] + [InlineData("Debug")] + [InlineData("Release")] + public void ProjectWithDllImportsRequiringMarshalIlGen_ArrayTypeParameter(string config) + { + string id = $"dllimport_incompatible_{Path.GetRandomFileName()}"; + string projectPath = CreateWasmTemplateProject(id, template: "wasmconsole"); + + string nativeSourceFilename = "incompatible_type.c"; + string nativeCode = "void call_needing_marhsal_ilgen(void *x) {}"; + File.WriteAllText(path: Path.Combine(_projectDir!, nativeSourceFilename), nativeCode); + + AddItemsPropertiesToProject( + projectPath, + extraItems: "" + ); + + File.Copy(Path.Combine(BuildEnvironment.TestAssetsPath, "marshal_ilgen_test.cs"), + Path.Combine(_projectDir!, "Program.cs"), + overwrite: true); + + CommandResult result = new DotNetCommand(s_buildEnv, _testOutput) + .WithWorkingDirectory(_projectDir!) + .WithEnvironmentVariable("NUGET_PACKAGES", _nugetPackagesDir) + .ExecuteWithCapturedOutput("build", $"-c {config} -bl"); + + Assert.True(result.ExitCode == 0, "Expected build to succeed"); + + CommandResult res = new RunCommand(s_buildEnv, _testOutput) + .WithWorkingDirectory(_projectDir!) + .ExecuteWithCapturedOutput($"run --no-silent --no-build -c {config}") + .EnsureSuccessful(); + Assert.Contains("Hello, Console!", res.Output); + Assert.Contains("Hello, World! Greetings from node version", res.Output); + } } } From 60b579118f169330001c659f3a8b28244c2c6e7d Mon Sep 17 00:00:00 2001 From: Ankit Jain Date: Fri, 16 Jun 2023 00:12:13 +0000 Subject: [PATCH 4/4] add missing file --- .../wasm/testassets/marshal_ilgen_test.cs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/mono/wasm/testassets/marshal_ilgen_test.cs diff --git a/src/mono/wasm/testassets/marshal_ilgen_test.cs b/src/mono/wasm/testassets/marshal_ilgen_test.cs new file mode 100644 index 00000000000000..ef4572b62f4bd1 --- /dev/null +++ b/src/mono/wasm/testassets/marshal_ilgen_test.cs @@ -0,0 +1,27 @@ +using System; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.JavaScript; + +Console.WriteLine("Hello, Console!"); + +return 0; + +public partial class MyClass +{ + [JSExport] + internal static string Greeting() + { + int[] x = new int[0]; + call_needing_marhsal_ilgen(x); + + var text = $"Hello, World! Greetings from node version: {GetNodeVersion()}"; + return text; + } + + [JSImport("node.process.version", "main.mjs")] + internal static partial string GetNodeVersion(); + + [DllImport("incompatible_type")] + static extern void call_needing_marhsal_ilgen(int[] numbers); +} +