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

[NativeAOT-LLVM, Wasm] Cannot marshal struct containing an array #2234

Closed
RReverser opened this issue Mar 21, 2023 · 5 comments
Closed

[NativeAOT-LLVM, Wasm] Cannot marshal struct containing an array #2234

RReverser opened this issue Mar 21, 2023 · 5 comments
Labels
area-NativeAOT-LLVM LLVM generation for Native AOT compilation (including Web Assembly)

Comments

@RReverser
Copy link

Minimal repro:

[StructLayout(LayoutKind.Sequential)]
public struct MyStruct {
	[MarshalAs(UnmanagedType.LPArray)]
	public byte[] bytes;
}

[DllImport("*")]
public static extern void pass_custom_struct(MyStruct s);

...

pass_custom_struct(new MyStruct {
	bytes = new byte[] { 1, 2, 3 }
});

Results in:

EXEC : warning : Method `[S.P.CompilerGenerated]Internal.CompilerGenerated.<Module>.ManagedToNative__MyStruct(MyS
truct&,__NativeType__MyStruct&)` will always throw because: Failed to load type 'Byte[]' from assembly '?' [C:\Us 
ers\me\Documents\NativeAOTWasm\helloworld\helloworld.csproj]
@jkotas jkotas added the area-NativeAOT-LLVM LLVM generation for Native AOT compilation (including Web Assembly) label Mar 21, 2023
@RReverser
Copy link
Author

Following #2233, I thought this one might be fixed in 8.0.0-* previews as well, so rechecked, but it still fails, albeit with different error messages:

EXEC : error : Code generation failed for method '[minisample]Lib.pass_custom_struct(MyStruct)' [C:\User
s\me\Documents\minisample\minisample.csproj]
  ILCompiler.CodeGenerationFailedException: Code generation failed for method '[minisample]Lib.pass_cust
  om_struct(MyStruct)'
     at Internal.JitInterface.CorInfoImpl.CompileMethodInternal(IMethodNode, MethodIL) in /_/src/coreclr 
  /tools/Common/JitInterface/CorInfoImpl.cs:line 410
     at Internal.JitInterface.CorInfoImpl.CompileMethod(IMethodCodeNode, MethodIL ) in /_/src/coreclr/to 
  ols/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs:line 63
     at ILCompiler.LLVMCodegenCompilation.CompileSingleMethod(CorInfoImpl, LLVMMethodCodeNode) in /_/src 
  /coreclr/tools/aot/ILCompiler.LLVM/Compiler/LLVMCodegenCompilation.cs:line 145
     at ILCompiler.LLVMCodegenCompilation.CompileSingleThreaded(List`1) in /_/src/coreclr/tools/aot/ILCo 
  mpiler.LLVM/Compiler/LLVMCodegenCompilation.cs:line 129
     at ILCompiler.LLVMCodegenCompilation.ComputeDependencyNodeDependencies(List`1) in /_/src/coreclr/to 
  ols/aot/ILCompiler.LLVM/Compiler/LLVMCodegenCompilation.cs:line 106
     at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.ComputeMarkedNodes() in /_/src/corec 
  lr/tools/aot/ILCompiler.DependencyAnalysisFramework/DependencyAnalyzer.cs:line 315
     at ILCompiler.LLVMCodegenCompilation.CompileInternal(String, ObjectDumper) in /_/src/coreclr/tools/ 
  aot/ILCompiler.LLVM/Compiler/LLVMCodegenCompilation.cs:line 73
     at ILCompiler.Compilation.ILCompiler.ICompilation.Compile(String, ObjectDumper) in /_/src/coreclr/t 
  ools/aot/ILCompiler.Compiler/Compiler/Compilation.cs:line 529
     at ILCompiler.Program.Run(String[]) in /_/src/coreclr/tools/aot/ILCompiler/Program.cs:line 996      
     at ILCompiler.Program.Main(String[]) in /_/src/coreclr/tools/aot/ILCompiler/Program.cs:line 1188    
C:\Users\me\Documents\minisample\.packages\microsoft.dotnet.ilcompiler.llvm\8.0.0-alpha.1.23170.1\build\
Microsoft.NETCore.Native.targets(307,5): error MSB3073: The command ""C:\Users\me\Documents\minisample\. 
packages\runtime.win-x64.microsoft.dotnet.ilcompiler.llvm\8.0.0-alpha.1.23170.1\tools\\ilc" @"obj\Debug\
net7.0\browser-wasm\native\minisample.ilc.rsp"" exited with code 1. [C:\Users\me\Documents\minisample\mi 
nisample.csproj]

cc @SingleAccretion

@SingleAccretion
Copy link

This is supposed to have been fixed with #2220.

@RReverser
Copy link
Author

RReverser commented Mar 22, 2023

"merged 3 hours ago" love living on the edge 😅 Okay, I'll recheck when that's out.

Btw, just to be sure based on the title of the PR alone - Pass structs by reference in accordance with the WASM C ABI - you mean only for structs with >1 field, right? Because structs with 1 field like in my example still need to be passed as their inner value.

@SingleAccretion
Copy link

SingleAccretion commented Mar 22, 2023

Btw, just to be sure based on the title of the PR alone - Pass structs by reference in accordance with the WASM C ABI - you mean only for structs with >1 field, right? Because structs with 1 field like in my example still need to be passed as their inner value.

That is correct. I suspect the failure here is because the code used to (explicitly) block any struct arguments for PInvokes.

@RReverser
Copy link
Author

"merged 3 hours ago" love living on the edge 😅 Okay, I'll recheck when that's out.

Ah I see 8.0.0-alpha.1.23172.1 is already published; just tried and it doesn't crash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-NativeAOT-LLVM LLVM generation for Native AOT compilation (including Web Assembly)
Projects
None yet
Development

No branches or pull requests

3 participants