-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Fix JIT using too wide indirections when returning small structs #68160
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
8 commits
Select commit
Hold shift + click to select a range
f82f21b
Fix JIT using too wide indirections when returning small structs
jakobbotsch fe6444c
Add test
jakobbotsch a019a3c
Run jit-format
jakobbotsch d8b5184
Do not leak in case of merged tests
jakobbotsch a6aa282
Small test nit
jakobbotsch b7160e2
Another test nit
jakobbotsch 5ca923b
Final test nit
jakobbotsch af1a907
Make test compile
jakobbotsch 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
97 changes: 97 additions & 0 deletions
97
src/tests/JIT/Regression/JitBlue/Runtime_58874/Runtime_58874.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,97 @@ | ||
| // 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; | ||
|
|
||
| public unsafe class Runtime_58874 | ||
| { | ||
| private static int Main(string[] args) | ||
| { | ||
| using EndOfPage endOfPage = EndOfPage.Create(); | ||
| if (endOfPage != null) | ||
| { | ||
| Foo(endOfPage.Pointer); | ||
| } | ||
| return 100; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private static Test Foo(Test* t) | ||
| { | ||
| // This read was too wide. | ||
| return *t; | ||
| } | ||
|
|
||
| private class EndOfPage : IDisposable | ||
| { | ||
| private void* _addr; | ||
| private EndOfPage() | ||
| { | ||
| } | ||
|
|
||
| public Test* Pointer => (Test*)((byte*)_addr + 0x1000 - sizeof(Test)); | ||
| public void Dispose() | ||
| { | ||
| if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
| { | ||
| const int MEM_RELEASE = 0x8000; | ||
| VirtualFree(_addr, 0, MEM_RELEASE); | ||
| } | ||
| else | ||
| { | ||
| NativeMemory.Free(_addr); | ||
| } | ||
| } | ||
|
|
||
| public static EndOfPage Create() | ||
| { | ||
| void* mem; | ||
| if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
| { | ||
| const int MEM_COMMIT = 0x1000; | ||
| const int MEM_RESERVE = 0x2000; | ||
| const int PAGE_READWRITE = 0x04; | ||
|
|
||
| // Reserve 2 pages | ||
| void* pages = VirtualAlloc(null, 0x2000, MEM_RESERVE, PAGE_READWRITE); | ||
| if (pages == null) | ||
| { | ||
| return null; | ||
| } | ||
| // Commit first page | ||
| mem = VirtualAlloc(pages, 0x1000, MEM_COMMIT, PAGE_READWRITE); | ||
| if (mem != pages) | ||
| { | ||
| return null; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| try | ||
| { | ||
| mem = NativeMemory.Alloc(0x1000); | ||
| } | ||
| catch (OutOfMemoryException) | ||
| { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| return new EndOfPage { _addr = mem }; | ||
| } | ||
|
|
||
| [DllImport("kernel32")] | ||
| private static extern void* VirtualAlloc(void* lpAddress, nuint dwSize, uint flAllocationType, uint flProtect); | ||
|
|
||
| [DllImport("kernel32")] | ||
| [return: MarshalAs(UnmanagedType.Bool)] | ||
| private static extern bool VirtualFree(void* lpAddress, nuint dwSize, uint dwFreeType); | ||
| } | ||
| } | ||
|
|
||
| struct Test | ||
| { | ||
| public byte A, B; | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/tests/JIT/Regression/JitBlue/Runtime_58874/Runtime_58874.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,10 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <Optimize>True</Optimize> | ||
| <AllowUnsafeBlocks>True</AllowUnsafeBlocks> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="$(MSBuildProjectName).cs" /> | ||
| </ItemGroup> | ||
| </Project> |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that comment still correct with your changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be still relevant, the following
genActualTypeshould upscale all small-types to intUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, there are two nodes here, the return and the operand of the return. The return is still wide but the bug was that we were retyping the operand to a wider type, which is wrong.