Skip to content

Commit 423fd78

Browse files
[release/8.0-staging] [Mono AOT] Fix error when returning zero sized struct (#107198)
* Fix error when returning zero sized struct * Add test * Fix x64 errors --------- Co-authored-by: Jeremi Kurdek <[email protected]>
1 parent e2e089c commit 423fd78

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

src/mono/mono/mini/mini-llvm.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,7 @@ sig_to_llvm_sig_full (EmitContext *ctx, MonoMethodSignature *sig, LLVMCallInfo *
15571557
ret_type = LLVMStructType (members, 1, FALSE);
15581558
} else if (cinfo->ret.pair_storage [0] == LLVMArgNone && cinfo->ret.pair_storage [1] == LLVMArgNone) {
15591559
/* Empty struct */
1560-
ret_type = LLVMVoidType ();
1560+
ret_type = LLVMStructType (NULL, 0, FALSE);
15611561
} else if (cinfo->ret.pair_storage [0] == LLVMArgInIReg && cinfo->ret.pair_storage [1] == LLVMArgInIReg) {
15621562
LLVMTypeRef members [2];
15631563

@@ -1574,7 +1574,11 @@ sig_to_llvm_sig_full (EmitContext *ctx, MonoMethodSignature *sig, LLVMCallInfo *
15741574
case LLVMArgVtypeAsScalar: {
15751575
int size = mono_class_value_size (mono_class_from_mono_type_internal (rtype), NULL);
15761576
/* LLVM models this by returning an int */
1577-
if (size < TARGET_SIZEOF_VOID_P) {
1577+
if (size == 0) {
1578+
/* Empty struct with LayoutKind attribute and without specified size */
1579+
g_assert(cinfo->ret.nslots == 0);
1580+
ret_type = LLVMIntType (8);
1581+
} else if (size < TARGET_SIZEOF_VOID_P) {
15781582
g_assert (cinfo->ret.nslots == 1);
15791583
ret_type = LLVMIntType (size * 8);
15801584
} else {
@@ -4806,6 +4810,9 @@ process_call (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref,
48064810
/* Empty struct */
48074811
break;
48084812

4813+
if (LLVMTypeOf (lcall) == LLVMStructType (NULL, 0, FALSE))
4814+
break;
4815+
48094816
if (!addresses [ins->dreg])
48104817
addresses [ins->dreg] = build_alloca_address (ctx, sig->ret);
48114818

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Runtime.InteropServices;
5+
using Xunit;
6+
7+
[StructLayout(LayoutKind.Sequential)]
8+
public struct S1
9+
{
10+
}
11+
12+
[StructLayout(LayoutKind.Auto)]
13+
public struct S2
14+
{
15+
}
16+
17+
public class Runtime_103628
18+
{
19+
public static S1 Get_S1() => new S1();
20+
21+
public static S2 Get_S2() => new S2();
22+
23+
[Fact]
24+
public static void TestEntryPoint()
25+
{
26+
S1 s1 = Get_S1();
27+
S2 s2 = Get_S2();
28+
}
29+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<Optimize>True</Optimize>
4+
</PropertyGroup>
5+
<ItemGroup>
6+
<Compile Include="$(MSBuildProjectName).cs" />
7+
</ItemGroup>
8+
</Project>

0 commit comments

Comments
 (0)