-
Notifications
You must be signed in to change notification settings - Fork 5.3k
[Mono] Fix support for nested structs with explicit layout #61467
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
simonrozsival
merged 28 commits into
dotnet:main
from
simonrozsival:simonrozsival/61385-nested-struct-with-explicit-layout
Dec 2, 2021
Merged
Changes from 26 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
929ab21
Add test case for nested struct with explicit layout
simonrozsival 4574ac3
Add license
simonrozsival 7d49b82
Modify test
simonrozsival e18281d
Temporarily enable the test in the CI pipeline
simonrozsival 1e40416
Allow running Mac Catalyst builds in App Sandbox
simonrozsival 8c3c9f9
Allow enabling app sandbox for the other Mac Catalyst sample
simonrozsival 586f4e4
Revert "Allow enabling app sandbox for the other Mac Catalyst sample"
simonrozsival 49af5fd
Revert "Allow running Mac Catalyst builds in App Sandbox"
simonrozsival fd663ef
Add more test cases
simonrozsival 993bf36
WIP: First implementation of the recursive check
simonrozsival 5814633
Merge branch 'main' of github.com:simonrozsival/runtime into simonroz…
simonrozsival 7d072ff
Improve code
simonrozsival 0cd67e2
Fix test case
simonrozsival 017258f
Add more test cases
simonrozsival cd7cb1d
Unify test cases
simonrozsival 1b1dfed
Bugfixes
simonrozsival 342d6cd
Remove test which behaves differently depending on the target platform
simonrozsival bb00b01
Remove sequential layout test which behaves differently on Linux and …
simonrozsival afa00a7
Reorganize test cases
simonrozsival da553d7
Remove call to mono_class_setup_fields
simonrozsival 699fa7d
Fix embedding detection
simonrozsival 8803138
Fix layout validation for generic structs
simonrozsival a53285b
Remove unintentional change
simonrozsival c5e92db
Revert temporary change to the runtime.yml pipeline
simonrozsival 607ab9a
Merge branch 'main' of https://github.com/dotnet/runtime into simonro…
simonrozsival 12d8f52
Code clean-up
simonrozsival 3204414
Revert unrelated change
simonrozsival 6715423
Use getters instead of directly accessing MonoClass fields
simonrozsival 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
74 changes: 74 additions & 0 deletions
74
src/tests/Loader/classloader/explicitlayout/NestedStructs/case01.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,74 @@ | ||
| // 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.InteropServices; | ||
|
|
||
| [StructLayout(LayoutKind.Explicit, Size = 24)] | ||
| public struct ComplexStruct | ||
| { | ||
| [FieldOffset(0)] | ||
| public object? Object; | ||
|
|
||
| [FieldOffset(0)] | ||
| public InnerStruct Inner; | ||
|
|
||
| [FieldOffset(8)] | ||
| public double Double; | ||
|
|
||
| [FieldOffset(8)] | ||
| public ulong High; | ||
|
|
||
| [FieldOffset(16)] | ||
| public ulong Low; | ||
| } | ||
|
|
||
| [StructLayout(LayoutKind.Explicit, Size = 16)] | ||
| public struct InnerStruct | ||
| { | ||
| [FieldOffset(0)] | ||
| public object? Object; | ||
|
|
||
| [FieldOffset(8)] | ||
| public int High; | ||
|
|
||
| [FieldOffset(12)] | ||
| public int Low; | ||
| } | ||
|
|
||
| public class Test_NestedStructsWithExplicitLayout_Case01 { | ||
| private ComplexStruct currentCount = default; | ||
|
|
||
| private void IncrementCount() | ||
| { | ||
| var x = new ComplexStruct(); | ||
| x.Inner.High = currentCount.Inner.High + 1; | ||
| currentCount = x; | ||
| } | ||
|
|
||
| public static int Main () | ||
| { | ||
| try | ||
| { | ||
| var instance = new Test_NestedStructsWithExplicitLayout_Case01(); | ||
| instance.IncrementCount(); | ||
| var result = 99 + instance.currentCount.Inner.High; | ||
|
|
||
| if (result == 100) | ||
| { | ||
| Console.WriteLine("PASS: union of Explict + Explicit works correctly"); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| catch (TypeLoadException e) | ||
| { | ||
| Console.WriteLine("FAIL: type was not loaded"); | ||
| return 101; | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| return 102; | ||
| } | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/tests/Loader/classloader/explicitlayout/NestedStructs/case01.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,9 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="case01.cs" /> | ||
| </ItemGroup> | ||
| </Project> |
89 changes: 89 additions & 0 deletions
89
src/tests/Loader/classloader/explicitlayout/NestedStructs/case02.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,89 @@ | ||
| // 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.InteropServices; | ||
|
|
||
| [StructLayout(LayoutKind.Explicit, Size = 16)] | ||
| public struct FirstLevel | ||
| { | ||
| [FieldOffset(0)] | ||
| public object? Object; | ||
|
|
||
| [FieldOffset(0)] | ||
| public SecondLevel SecondLevel; | ||
|
|
||
| [FieldOffset(8)] | ||
| public int High; | ||
|
|
||
| [FieldOffset(12)] | ||
| public int Low; | ||
| } | ||
|
|
||
| [StructLayout(LayoutKind.Explicit, Size = 16)] | ||
| public struct SecondLevel | ||
| { | ||
| [FieldOffset(0)] | ||
| public object? Object; | ||
|
|
||
| [FieldOffset(0)] | ||
| public ThirdLevel ThirdLevel; | ||
|
|
||
| [FieldOffset(8)] | ||
| public int High; | ||
|
|
||
| [FieldOffset(12)] | ||
| public int Low; | ||
| } | ||
|
|
||
| [StructLayout(LayoutKind.Explicit, Size = 16)] | ||
| public struct ThirdLevel | ||
| { | ||
| [FieldOffset(0)] | ||
| public object? Object; | ||
|
|
||
| [FieldOffset(8)] | ||
| public int High; | ||
|
|
||
| [FieldOffset(12)] | ||
| public int Low; | ||
| } | ||
|
|
||
| public class Test_NestedStructsWithExplicitLayout_Case02 { | ||
| private int Run(int value) | ||
| { | ||
| var x = new FirstLevel(); | ||
| x.Low = value; | ||
| return x.SecondLevel.ThirdLevel.Low; | ||
| } | ||
|
|
||
| public static int Main () | ||
| { | ||
| try | ||
| { | ||
| var expectedResult = 13; | ||
|
|
||
| var testInstance = new Test_NestedStructsWithExplicitLayout_Case02(); | ||
| var result = testInstance.Run(expectedResult); | ||
|
|
||
| if (result == expectedResult) | ||
| { | ||
| Console.WriteLine("PASS: types were loaded correctly"); | ||
| return 100; | ||
| } | ||
|
|
||
| Console.WriteLine("FAIL: invalid value"); | ||
| return 103; | ||
| } | ||
| catch (TypeLoadException e) | ||
| { | ||
| Console.WriteLine("FAIL: type was not loaded correctly"); | ||
| return 101; | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| Console.WriteLine("FAIL: unknown error"); | ||
| return 102; | ||
| } | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/tests/Loader/classloader/explicitlayout/NestedStructs/case02.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,9 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="case02.cs" /> | ||
| </ItemGroup> | ||
| </Project> |
62 changes: 62 additions & 0 deletions
62
src/tests/Loader/classloader/explicitlayout/NestedStructs/case03.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,62 @@ | ||
| // 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.InteropServices; | ||
|
|
||
| [StructLayout(LayoutKind.Explicit)] | ||
| public struct FirstLevel | ||
| { | ||
| [FieldOffset(0)] | ||
| public object? ConflictingObjectField; | ||
|
|
||
| [FieldOffset(0)] | ||
| public SecondLevel SecondLevel; | ||
| } | ||
|
|
||
| [StructLayout(LayoutKind.Explicit)] | ||
| public struct SecondLevel | ||
| { | ||
| [FieldOffset(0)] | ||
| public ThirdLevel ThirdLevel; | ||
|
|
||
| [FieldOffset(8)] | ||
| public long Value; | ||
| } | ||
|
|
||
| [StructLayout(LayoutKind.Explicit)] | ||
| public struct ThirdLevel | ||
| { | ||
| [FieldOffset(0)] | ||
| public long ConflictingValueTypeField; | ||
| } | ||
|
|
||
| public class Test_NestedStructsWithExplicitLayout_Case06 { | ||
| private void Run() | ||
| { | ||
| var x = new FirstLevel(); | ||
| x.ConflictingObjectField = new object(); | ||
| } | ||
|
|
||
| public static int Main () | ||
| { | ||
| try | ||
| { | ||
| var test = new Test_NestedStructsWithExplicitLayout_Case06(); | ||
| test.Run(); | ||
| } | ||
| catch (TypeLoadException e) | ||
| { | ||
| Console.WriteLine("PASS: object and non-object field overlap was detected"); | ||
| return 100; | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| Console.WriteLine("FAIL: unexpected exception type"); | ||
| return 102; | ||
| } | ||
|
|
||
| Console.WriteLine("FAIL: object and non-object field overlap was not detected"); | ||
| return 101; | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/tests/Loader/classloader/explicitlayout/NestedStructs/case03.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,9 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="case03.cs" /> | ||
| </ItemGroup> | ||
| </Project> |
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.