Skip to content

Commit d6f6688

Browse files
authored
Merge pull request #890 from Unity-Technologies/unity-master-fix-stack-alignment-structs-android
[arm] fix stack alignment for structs (mono#7691) case 915869 - Fix pinvoke structure alignment on Android case 960482 (maybe) - Fix Marshal.PtrToStructure alignment on Android
2 parents 2bcc421 + 0e083f5 commit d6f6688

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

mono/mini/mini-arm.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,7 @@ get_call_info (MonoMemPool *mp, MonoMethodSignature *sig)
15241524
ainfo->storage = RegTypeStructByVal;
15251525
ainfo->struct_size = size;
15261526
ainfo->align = align;
1527-
/* FIXME: align stack_size if needed */
1527+
15281528
if (eabi_supported) {
15291529
if (align >= 8 && (gr & 1))
15301530
gr ++;
@@ -1542,9 +1542,8 @@ get_call_info (MonoMemPool *mp, MonoMethodSignature *sig)
15421542
gr += n_in_regs;
15431543
nwords -= n_in_regs;
15441544
}
1545-
if (sig->call_convention == MONO_CALL_VARARG)
1546-
/* This matches the alignment in mono_ArgIterator_IntGetNextArg () */
1547-
stack_size = ALIGN_TO (stack_size, align);
1545+
stack_size = ALIGN_TO (stack_size, align);
1546+
15481547
ainfo->offset = stack_size;
15491548
/*g_print ("offset for arg %d at %d\n", n, stack_size);*/
15501549
stack_size += nwords * sizeof (gpointer);

mono/tests/libtest.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,25 @@ mono_return_nested_float (void)
280280
return f;
281281
}
282282

283+
struct Scalar4 {
284+
double val[4];
285+
};
286+
287+
struct Rect {
288+
int x;
289+
int y;
290+
int width;
291+
int height;
292+
};
293+
294+
LIBTEST_API char * STDCALL
295+
mono_return_struct_4_double (void *ptr, struct Rect rect, struct Scalar4 sc4, int a, int b, int c)
296+
{
297+
char *buffer = (char *) malloc (1024 * sizeof (char));
298+
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);
299+
return buffer;
300+
}
301+
283302
LIBTEST_API int STDCALL
284303
mono_test_many_int_arguments (int a, int b, int c, int d, int e,
285304
int f, int g, int h, int i, int j);

mono/tests/pinvoke11.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,39 @@ public struct NestedFloat {
6060
public float f4;
6161
}
6262

63+
[Serializable]
64+
[StructLayout(LayoutKind.Sequential)]
65+
public struct Rectangle
66+
{
67+
public int X;
68+
public int Y;
69+
public int Width;
70+
public int Height;
71+
72+
public Rectangle(int x, int y, int width, int height)
73+
{
74+
X = x;
75+
Y = y;
76+
Width = width;
77+
Height = height;
78+
}
79+
}
80+
81+
[Serializable]
82+
public struct Scalar4 {
83+
public double Val0;
84+
public double Val1;
85+
public double Val2;
86+
public double Val3;
87+
88+
public Scalar4 (double v0, double v1, double v2, double v3) {
89+
Val0 = v0;
90+
Val1 = v1;
91+
Val2 = v2;
92+
Val3 = v3;
93+
}
94+
}
95+
6396
public class Test
6497
{
6598
[DllImport ("libtest")]
@@ -89,6 +122,10 @@ public class Test
89122
[DllImport ("libtest", EntryPoint="mono_return_nested_float")]
90123
public static extern NestedFloat mono_return_nested_float ();
91124

125+
[DllImport("libtest", EntryPoint="mono_return_struct_4_double")]
126+
[return: MarshalAs(UnmanagedType.LPStr)]
127+
public static extern string mono_return_struct_4_double (IntPtr ptr, Rectangle rect, Scalar4 sc4, int a, int b, int c);
128+
92129
static int Main()
93130
{
94131
if (mono_return_int (5) != 5)
@@ -159,6 +196,14 @@ static int Main()
159196
if (f.fi.f1 != 1.0)
160197
return 12;
161198

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

0 commit comments

Comments
 (0)