-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error out when struct size is bigger than int.MaxValue (#104393)
* Error out when field size is too big * Error out during type loading and add a test * Move test to a different file * Make things loadable in the managed type system * Error out when size overflow the value that int could hold * Update the threshold to FIELD_OFFSET_LAST_REAL_OFFSET * Change the threashold to int.MaxValue * Fix mono struct size overflow issue * Add signed/unsigned type conversion * Use gint64 instead of long, due to windows * Update src/mono/mono/metadata/class-init.c Co-authored-by: Aleksey Kliger (λgeek) <akliger@gmail.com> * Fix test * Add CoreCLR test for Explicit struct * Address review feedback * Fixes Explicit struct size for Mono * Disable explicit struct tests on 32bit platforms, due to out of memory issue. * Disable CoreCLR test for NativeAOT and crossgen * Fix test * Add RequiresProcessIsolation for CoreCLR test --------- Co-authored-by: Michal Strehovský <MichalStrehovsky@users.noreply.github.com> Co-authored-by: Aleksey Kliger (λgeek) <akliger@gmail.com>
- Loading branch information
1 parent
d315414
commit 423faed
Showing
11 changed files
with
232 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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; | ||
[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 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 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 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 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