-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not promote struct locals with holes (#62645)
* Make sure the combined field size matches the struct size * Fix the condition * Update test and include containHoles condition
- Loading branch information
1 parent
f18f658
commit bffa7cf
Showing
3 changed files
with
90 additions
and
2 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
70 changes: 70 additions & 0 deletions
70
src/tests/JIT/Regression/JitBlue/Runtime_62597/Runtime_62597.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. | ||
// | ||
// Note: In below test case, we were not honoring the fact that the explicit struct size | ||
// of struct is 32 bytes while the only 2 fields it has is just 2 bytes. In such case, | ||
// we would pass partial struct value. | ||
using System; | ||
using System.Reflection.Emit; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
|
||
[StructLayout(LayoutKind.Explicit, Size = 32)] | ||
public readonly unsafe struct SmallString | ||
{ | ||
[FieldOffset(0)] private readonly byte _length; | ||
[FieldOffset(1)] private readonly byte _firstByte; | ||
|
||
public SmallString(string value) | ||
{ | ||
fixed (char* srcPtr = value) | ||
fixed (byte* destPtr = &_firstByte) | ||
{ | ||
Encoding.ASCII.GetBytes(srcPtr, value.Length, destPtr, value.Length); | ||
} | ||
|
||
_length = (byte)value.Length; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
public byte Dump() | ||
{ | ||
fixed (byte* ptr = &_firstByte) | ||
{ | ||
byte* next = ptr + 1; | ||
return *next; | ||
} | ||
} | ||
} | ||
|
||
public static class Program | ||
{ | ||
static int result = 0; | ||
public static int Main() | ||
{ | ||
var value = new SmallString("foobar"); | ||
|
||
TheTest(value); | ||
|
||
return result; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
public static void TheTest(SmallString foo) | ||
{ | ||
Execute(foo); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
public static object Execute(SmallString foo) | ||
{ | ||
byte value = foo.Dump(); | ||
// 111 corresponds to the ASCII code of 2nd characted of string "foobar" i.e. ASCII value of 'o'. | ||
if (value == 111) | ||
{ | ||
result = 100; | ||
} | ||
return new StringBuilder(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/tests/JIT/Regression/JitBlue/Runtime_62597/Runtime_62597.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,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<AllowUnsafeBlocks>True</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<DebugType>None</DebugType> | ||
<Optimize>True</Optimize> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="$(MSBuildProjectName).cs" /> | ||
</ItemGroup> | ||
</Project> |