-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Error out when struct size is bigger than int.MaxValue #104393
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
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f7f124b
Error out when field size is too big
fanyang-mono 8f7388e
Error out during type loading and add a test
fanyang-mono 528b737
Move test to a different file
fanyang-mono 6c91570
Make things loadable in the managed type system
MichalStrehovsky d3f062a
Error out when size overflow the value that int could hold
fanyang-mono c843b90
Update the threshold to FIELD_OFFSET_LAST_REAL_OFFSET
fanyang-mono 0ca301c
Change the threashold to int.MaxValue
fanyang-mono afd5c8f
Fix mono struct size overflow issue
fanyang-mono b091a2a
Add signed/unsigned type conversion
fanyang-mono 4a57e6c
Use gint64 instead of long, due to windows
fanyang-mono 186acf8
Update src/mono/mono/metadata/class-init.c
fanyang-mono 33145a2
Fix test
fanyang-mono 6e0c43b
Add CoreCLR test for Explicit struct
fanyang-mono b557816
Address review feedback
fanyang-mono 339bc63
Fixes Explicit struct size for Mono
fanyang-mono a0adb6d
Disable explicit struct tests on 32bit platforms, due to out of memor…
fanyang-mono 39cc6f1
Disable CoreCLR test for NativeAOT and crossgen
fanyang-mono 08daa62
Fix test
fanyang-mono d7160c7
Merge remote-tracking branch 'origin/main' into fix_overflow
fanyang-mono 5fad351
Add RequiresProcessIsolation for CoreCLR test
fanyang-mono File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/tests/Loader/classloader/SequentialLayout/ManagedSequential/LargeStructSize_CoreCLR.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using Xunit; | ||
|
||
[SkipOnMono("This test suite tests CoreCLR and Crossgen2/NativeAOT-specific layout rules.")] | ||
public unsafe class LargeStructSize | ||
{ | ||
struct X | ||
{ | ||
byte x; | ||
BigArray a; | ||
} | ||
|
||
[StructLayout(LayoutKind.Explicit)] | ||
struct X_explicit | ||
{ | ||
[FieldOffset(0)] | ||
byte x; | ||
fanyang-mono marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[FieldOffset(1)] | ||
BigArray a; | ||
} | ||
|
||
[StructLayout(LayoutKind.Explicit)] | ||
struct X_non_blittable | ||
{ | ||
[FieldOffset(0)] | ||
bool x; | ||
[FieldOffset(1)] | ||
BigArray a; | ||
} | ||
|
||
struct Y | ||
{ | ||
BigArray a; | ||
byte y; | ||
} | ||
|
||
[StructLayout(LayoutKind.Explicit)] | ||
struct Y_explict | ||
{ | ||
[FieldOffset(0)] | ||
BigArray b; | ||
[FieldOffset(int.MaxValue)] | ||
byte y; | ||
} | ||
|
||
[StructLayout(LayoutKind.Sequential, Size = int.MaxValue)] | ||
struct BigArray | ||
{ | ||
} | ||
|
||
[Fact] | ||
public static void TestLargeStructSize() | ||
{ | ||
Assert.Equal(int.MaxValue, sizeof(BigArray)); | ||
Assert.Throws<TypeLoadException>(() => sizeof(X)); | ||
Assert.Throws<TypeLoadException>(() => sizeof(Y)); | ||
if (Environment.Is64BitProcess) | ||
{ | ||
// Explicit struct of big size triggers out of memory error instead of type load exception | ||
Assert.Throws<TypeLoadException>(() => sizeof(X_explicit)); | ||
Assert.Throws<TypeLoadException>(() => sizeof(X_non_blittable)); | ||
Assert.Throws<TypeLoadException>(() => sizeof(Y_explict)); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ests/Loader/classloader/SequentialLayout/ManagedSequential/LargeStructSize_CoreCLR.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<NativeAotIncompatible>true</NativeAotIncompatible> | ||
<CrossGenTest>false</CrossGenTest> | ||
<RequiresProcessIsolation>true</RequiresProcessIsolation> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="$(MSBuildProjectName).cs" /> | ||
</ItemGroup> | ||
</Project> |
93 changes: 93 additions & 0 deletions
93
src/tests/Loader/classloader/SequentialLayout/ManagedSequential/LargeStructSize_Mono.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using Xunit; | ||
|
||
public unsafe class LargeStructSize | ||
{ | ||
struct X_64 | ||
{ | ||
byte x; | ||
BigArray_64_1 a; | ||
} | ||
|
||
[StructLayout(LayoutKind.Explicit)] | ||
struct X_explicit_64 | ||
{ | ||
[FieldOffset(0)] | ||
bool x; | ||
[FieldOffset(1)] | ||
BigArray_64_1 a; | ||
} | ||
|
||
struct Y_64 | ||
{ | ||
BigArray_64_1 a; | ||
byte y; | ||
} | ||
|
||
struct X_32 | ||
{ | ||
byte x; | ||
BigArray_32_1 a; | ||
} | ||
|
||
[StructLayout(LayoutKind.Explicit)] | ||
struct X_explicit_32 | ||
{ | ||
[FieldOffset(0)] | ||
bool x; | ||
[FieldOffset(1)] | ||
BigArray_32_1 a; | ||
} | ||
|
||
struct Y_32 | ||
{ | ||
BigArray_32_1 a; | ||
byte y; | ||
} | ||
|
||
[StructLayout(LayoutKind.Sequential, Size = int.MaxValue - 16)] | ||
struct BigArray_64_1 | ||
{ | ||
} | ||
|
||
[StructLayout(LayoutKind.Sequential, Size = int.MaxValue - 16 - 1)] | ||
struct BigArray_64_2 | ||
{ | ||
} | ||
|
||
[StructLayout(LayoutKind.Sequential, Size = int.MaxValue - 8)] | ||
struct BigArray_32_1 | ||
{ | ||
} | ||
|
||
[StructLayout(LayoutKind.Sequential, Size = int.MaxValue - 8 - 1)] | ||
struct BigArray_32_2 | ||
{ | ||
} | ||
|
||
[Fact] | ||
public static void TestLargeStructSize() | ||
{ | ||
if (Environment.Is64BitProcess) | ||
{ | ||
Assert.Equal(int.MaxValue - (IntPtr.Size * 2), sizeof(BigArray_64_1)); | ||
Assert.Throws<TypeLoadException>(() => sizeof(BigArray_64_2)); | ||
Assert.Throws<TypeLoadException>(() => sizeof(X_64)); | ||
Assert.Throws<TypeLoadException>(() => sizeof(X_explicit_64)); | ||
Assert.Throws<TypeLoadException>(() => sizeof(Y_64)); | ||
} | ||
else | ||
{ | ||
Assert.Equal(int.MaxValue - (IntPtr.Size * 2), sizeof(BigArray_32_1)); | ||
Assert.Throws<TypeLoadException>(() => sizeof(BigArray_32_2)); | ||
Assert.Throws<TypeLoadException>(() => sizeof(X_32)); | ||
Assert.Throws<TypeLoadException>(() => sizeof(X_explicit_32)); | ||
Assert.Throws<TypeLoadException>(() => sizeof(Y_32)); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/tests/Loader/classloader/SequentialLayout/ManagedSequential/LargeStructSize_Mono.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<RequiresProcessIsolation>true</RequiresProcessIsolation> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<NativeAotIncompatible>true</NativeAotIncompatible> | ||
<CrossGenTest>false</CrossGenTest> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="$(MSBuildProjectName).cs" /> | ||
</ItemGroup> | ||
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.