Skip to content

[arm] fix stack alignment for structs (#7691) #890

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

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 3 additions & 4 deletions mono/mini/mini-arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,7 @@ get_call_info (MonoMemPool *mp, MonoMethodSignature *sig)
ainfo->storage = RegTypeStructByVal;
ainfo->struct_size = size;
ainfo->align = align;
/* FIXME: align stack_size if needed */

if (eabi_supported) {
if (align >= 8 && (gr & 1))
gr ++;
Expand All @@ -1542,9 +1542,8 @@ get_call_info (MonoMemPool *mp, MonoMethodSignature *sig)
gr += n_in_regs;
nwords -= n_in_regs;
}
if (sig->call_convention == MONO_CALL_VARARG)
/* This matches the alignment in mono_ArgIterator_IntGetNextArg () */
stack_size = ALIGN_TO (stack_size, align);
stack_size = ALIGN_TO (stack_size, align);

ainfo->offset = stack_size;
/*g_print ("offset for arg %d at %d\n", n, stack_size);*/
stack_size += nwords * sizeof (gpointer);
Expand Down
19 changes: 19 additions & 0 deletions mono/tests/libtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,25 @@ mono_return_nested_float (void)
return f;
}

struct Scalar4 {
double val[4];
};

struct Rect {
int x;
int y;
int width;
int height;
};

LIBTEST_API char * STDCALL
mono_return_struct_4_double (void *ptr, struct Rect rect, struct Scalar4 sc4, int a, int b, int c)
{
char *buffer = (char *) malloc (1024 * sizeof (char));
sprintf (buffer, "sc4 = {%.1f, %.1f, %.1f, %.1f }, a=%x, b=%x, c=%x\n", (float) sc4.val [0], (float) sc4.val [1], (float) sc4.val [2], (float) sc4.val [3], a, b, c);
return buffer;
}

LIBTEST_API int STDCALL
mono_test_many_int_arguments (int a, int b, int c, int d, int e,
int f, int g, int h, int i, int j);
Expand Down
45 changes: 45 additions & 0 deletions mono/tests/pinvoke11.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,39 @@ public struct NestedFloat {
public float f4;
}

[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Rectangle
{
public int X;
public int Y;
public int Width;
public int Height;

public Rectangle(int x, int y, int width, int height)
{
X = x;
Y = y;
Width = width;
Height = height;
}
}

[Serializable]
public struct Scalar4 {
public double Val0;
public double Val1;
public double Val2;
public double Val3;

public Scalar4 (double v0, double v1, double v2, double v3) {
Val0 = v0;
Val1 = v1;
Val2 = v2;
Val3 = v3;
}
}

public class Test
{
[DllImport ("libtest")]
Expand Down Expand Up @@ -89,6 +122,10 @@ public class Test
[DllImport ("libtest", EntryPoint="mono_return_nested_float")]
public static extern NestedFloat mono_return_nested_float ();

[DllImport("libtest", EntryPoint="mono_return_struct_4_double")]
[return: MarshalAs(UnmanagedType.LPStr)]
public static extern string mono_return_struct_4_double (IntPtr ptr, Rectangle rect, Scalar4 sc4, int a, int b, int c);

static int Main()
{
if (mono_return_int (5) != 5)
Expand Down Expand Up @@ -159,6 +196,14 @@ static int Main()
if (f.fi.f1 != 1.0)
return 12;

Rectangle rect = new Rectangle (10, 10, 100, 20);
Scalar4 sc4 = new Scalar4 (32, 64, 128, 256);
var sc4_ret = mono_return_struct_4_double (IntPtr.Zero, rect, sc4, 0x1337, 0x1234, 0x9876);
if (sc4_ret != "sc4 = {32.0, 64.0, 128.0, 256.0 }, a=1337, b=1234, c=9876\n") {
Console.WriteLine ("sc4_ret = " + sc4_ret);
return 13;
}

return 0;
}
}
Expand Down