-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
[x86/Linux] JIT.Methodical.structs.systemvbringup.structinregs test failure #7621
Comments
It seems that there is mismatch between managed struct layout and native struct layout. The native method shows the following result when adding
|
@jkotas @BruceForstall Could you let me know which code (JIT, EE) is related with such layout issues? |
The following patch seems to resolve S5 case, but introduces other test failures. diff --git a/src/vm/fieldmarshaler.h b/src/vm/fieldmarshaler.h
index 9ec7e87..b193b46 100644
--- a/src/vm/fieldmarshaler.h
+++ b/src/vm/fieldmarshaler.h
@@ -85,7 +85,11 @@ enum NStructFieldType
//=======================================================================
// Magic number for default struct packing size.
//=======================================================================
+#if defined(_TARGET_X86_) && defined(FEATURE_PAL)
+#define DEFAULT_PACKING_SIZE 4
+#else // _TARGET_X86 && FEATURE_APL
#define DEFAULT_PACKING_SIZE 8
+#endif // !_TARGET_X86_ || !FEATURE_PAL
//======================================================================= |
Yes, you are looking in the right place. methodtablebuilder.cpp is also involved in this. |
I observed the following test failure while evaluating the above patch:
The related test uses a hard-coded size for check:
Is there any standard related with struct layout? I'm not sure whether this failure just comes from test issue, or not. |
@parjong It seems odd that we would need to change It seems like the issue is in the structinregs.cpp test case. It seems that clang on Linux/x86 compiles this:
without 4 bytes of padding between the Can you check? Does the Linux/x86 compiler have the equivalent of |
@BruceForstall This article says that struct is 4-byte aligned even if it includes double fields for GCC on Linux (unlike VC++ on Windows). |
As I know, there is clang attribute for this issue |
Seems like we should use that in the structinregs.cpp test. |
I checked As I understand, this approach has the same effect with adding This means that any x86/Linux native code that P/Invoke targets should use this kind of attributes, or the corresponding managed code should use struct layout attributes. I would like to ask about the current .NET guideline on cross-platform development. Does that recommend this approach? If so, we could resolve this via simply adding |
We should do what Mono does on Linux x86. If Mono uses the platform native padding for Linux x86, we should do the same for .NET Core. |
Mono seems to use native padding for x86/Linux. Here are test samples and the result:
using System;
using System.Runtime.InteropServices;
namespace structinreg
{
class Program
{
public struct S5
{
public int x;
public double y;
};
public delegate void MyCallback5(S5 s);
[DllImport("native")]
public static extern void InvokeCallback5(MyCallback5 callback, S5 s);
public static void Main()
{
S5 s5;
s5.x = 1;
s5.y = 2;
InvokeCallback5((par) =>
{
Console.WriteLine("S5: {0}, {1}", par.x, par.y);
if (par.x != 1 || par.y != 2)
{
throw new System.Exception();
}
}, s5);
}
};
}
~
#include <stdio.h>
struct S5
{
int x;
double y;
};
typedef void (*PFNACTION5)(S5 s);
extern "C" void InvokeCallback5(PFNACTION5 callback, S5 s)
{
printf("Native S5: %d, %f\n", s.x, s.y);
callback(s);
}
|
@jkotas It seems like changing how we layout structs, per platform, is going to be very tricky to get right, with lots of pervasive changes required, e.g., to tests, maybe to frameworks assemblies, etc. Especially since it appears we haven't done this before. Do you agree? |
We have done it before - the layout rules are different between x86 and arm on Windows as side-effect of I agree that there may be some tests that need to be updated. However, the effect on framework assemblies or code out there should be exact opposite: it should just work on Linux x86 CoreCLR if it worked on other platforms. |
@BruceForstall @jkotas The following tests failed when we set the default struct alignment as 4:
|
@parjong I don't know about SpanBench, but it's easy to see looking at the others that they all depend on existing struct layout rules, so they would need to be altered (dual versioned, or |
@BruceForstall As you mentioned, all the tests except SpanBench seems to depend on struct layout rules. It seems that |
An exception is thrown while running
InvokeCallback5
test:The text was updated successfully, but these errors were encountered: