Skip to content

Commit cb7817f

Browse files
authored
[mono][aot] Emit marhsalling exception when dealing with MONO_MARSHAL_CONV_OBJECT_IUNKNOWN (#105156)
This change prevents the aot compiler from erroring out when dealing with COM types that were not marked with MarshalAs attributes. In most scenarios that we support, we want to allow pinvokes to aot compile as in cases like anything COM interop, will end up erroring out if you try to use it. Fixes #104463
1 parent 47da68b commit cb7817f

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

src/mono/mono/metadata/marshal-shared.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,11 @@ mono_marshal_shared_emit_object_to_ptr_conv (MonoMethodBuilder *mb, MonoType *ty
12251225
mono_mb_emit_byte (mb, CEE_STIND_I);
12261226
break;
12271227
}
1228+
case MONO_MARSHAL_CONV_OBJECT_IUNKNOWN: {
1229+
char *msg = g_strdup_printf ("Marshaling not supported for COM type MONO_MARSHAL_CONV_OBJECT_IUNKNOWN.");
1230+
mono_marshal_shared_mb_emit_exception_marshal_directive (mb, msg);
1231+
break;
1232+
}
12281233

12291234
default: {
12301235
g_error ("marshalling conversion %d not implemented", conv);

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,44 @@ void GenerateSourceFiles(string outputPath, int baseArg)
719719
return (buildArgs, output);
720720
}
721721

722+
private void EnsureComInteropCompiles(BuildArgs buildArgs, RunHost host, string id)
723+
{
724+
string programText = @"
725+
using System;
726+
using System.Runtime.CompilerServices;
727+
using System.Runtime.InteropServices;
728+
using System.Runtime.InteropServices.ComTypes;
729+
730+
public class Test
731+
{
732+
public static int Main(string[] args)
733+
{
734+
var s = new STGMEDIUM();
735+
ReleaseStgMedium(ref s);
736+
return 42;
737+
}
738+
739+
[DllImport(""ole32.dll"")]
740+
internal static extern void ReleaseStgMedium(ref STGMEDIUM medium);
741+
}
742+
743+
";
744+
745+
buildArgs = ExpandBuildArgs(buildArgs);
746+
747+
(string libraryDir, string output) = BuildProject(buildArgs,
748+
id: id,
749+
new BuildProjectOptions(
750+
InitProject: () =>
751+
{
752+
File.WriteAllText(Path.Combine(_projectDir!, "Program.cs"), programText);
753+
},
754+
Publish: buildArgs.AOT,
755+
DotnetWasmFromRuntimePack: true));
756+
757+
Assert.Contains("Generated app bundle at " + libraryDir, output);
758+
}
759+
722760
private void EnsureWasmAbiRulesAreFollowed(BuildArgs buildArgs, RunHost host, string id)
723761
{
724762
string programText = @"
@@ -885,6 +923,11 @@ public void EnsureWasmAbiRulesAreFollowedInAOT(BuildArgs buildArgs, RunHost host
885923
public void EnsureWasmAbiRulesAreFollowedInInterpreter(BuildArgs buildArgs, RunHost host, string id) =>
886924
EnsureWasmAbiRulesAreFollowed(buildArgs, host, id);
887925

926+
[Theory]
927+
[BuildAndRun(host: RunHost.Chrome, aot: true)]
928+
public void EnsureComInteropCompilesInAOT(BuildArgs buildArgs, RunHost host, string id) =>
929+
EnsureComInteropCompiles(buildArgs, host, id);
930+
888931
[Theory]
889932
[BuildAndRun(host: RunHost.Chrome, aot: false)]
890933
public void UCOWithSpecialCharacters(BuildArgs buildArgs, RunHost host, string id)

src/mono/wasm/Wasm.Build.Tests/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ being generated.
1414

1515
- Running:
1616

17-
Linux/macOS: `$ make -C src/mono/wasm run-build-tests`
17+
Linux/macOS: `$ make -C src/mono/(browser|wasi) run-build-tests`
1818
Windows: `.\dotnet.cmd build .\src\mono\wasm\Wasm.Build.Tests\Wasm.Build.Tests.csproj -c Release -t:Test -p:TargetOS=browser -p:TargetArchitecture=wasm`
1919

2020
- Specific tests can be run via `XUnitClassName`, and `XUnitMethodName`

0 commit comments

Comments
 (0)