diff --git a/src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/GcInfoRegressionTests.cs b/src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/GcInfoRegressionTests.cs new file mode 100644 index 00000000000000..1071cf23226d81 --- /dev/null +++ b/src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/GcInfoRegressionTests.cs @@ -0,0 +1,231 @@ +// 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.Collections.Generic; +using System.IO; +using System.Reflection.PortableExecutable; +using ILCompiler.Reflection.ReadyToRun; +using ILCompiler.Reflection.ReadyToRun.Amd64; +using Xunit; + +namespace ILCompiler.ReadyToRun.Tests; + +public class GcInfoRegressionTests +{ + [Theory] + [InlineData(Machine.LoongArch64, 0, 22u)] + [InlineData(Machine.LoongArch64, 1, 3u)] + [InlineData(Machine.RiscV64, 0, 8u)] + [InlineData(Machine.RiscV64, 1, 2u)] + [InlineData(Machine.RiscV64, 10, 2u)] + public void FatHeaderDenormalizesFrameRegister(Machine machine, int encodedRegister, uint expectedRegister) + { + BitWriter writer = new(); + WriteFatHeader(writer, machine, 0x41); + writer.WriteUnsigned(encodedRegister, 2); + writer.WriteUnsigned(0, 3); + writer.WriteUnsigned(0, 3); + writer.WriteUnsigned(0, 1); + writer.Write(0, 2); + + GcInfo info = Decode(writer, machine); + Assert.Equal(expectedRegister, info.StackBaseRegister); + Assert.Empty(info.SlotTable.GcSlots); + Assert.Equal(writer.ByteCount, info.Size); + } + + [Theory] + [InlineData(2)] + [InlineData(33)] + public void Arm64EnCHeaderConsumesFixedFrameSize(int fixedFrameSize) + { + BitWriter writer = new(); + WriteFatHeader(writer, Machine.Arm64, 0x101); + writer.WriteUnsigned(1, 4); + writer.WriteUnsigned(fixedFrameSize, 4); + writer.WriteUnsigned(3, 3); + writer.WriteUnsigned(0, 3); + writer.WriteUnsigned(0, 1); + writer.Write(0, 2); + + GcInfo info = Decode(writer, Machine.Arm64); + Assert.Equal(1u, info.SizeOfEditAndContinuePreservedArea); + Assert.Equal(24u, info.SizeOfStackOutgoingAndScratchArea); + Assert.Equal(0u, info.NumSafePoints); + Assert.Equal(0u, info.NumInterruptibleRanges); + Assert.Empty(info.SlotTable.GcSlots); + Assert.Equal(writer.ByteCount, info.Size); + } + + public static IEnumerable TransitionCases() + { + foreach ((Machine machine, int instructionSize) in new[] + { + (Machine.Amd64, 1), + (Machine.ArmThumb2, 2), + (Machine.Arm64, 4), + (Machine.LoongArch64, 4), + (Machine.RiscV64, 2), + }) + { + yield return [machine, instructionSize, Array.Empty<(int, int)>(), 4, 4]; + yield return [machine, instructionSize, new[] { (8, 16) }, 4, 12]; + foreach ((int transition, int expected) in new[] { (3, 3), (4, 20), (9, 41) }) + yield return [machine, instructionSize, new[] { (0, 4), (20, 24), (40, 44) }, transition, expected]; + } + } + + [Theory] + [MemberData(nameof(TransitionCases))] + public void TransitionsMapNormalizedOffsetsAcrossRanges( + Machine machine, int instructionSize, (int Start, int Stop)[] ranges, int transition, int expected) + { + BitWriter writer = new(); + WriteFatHeader(writer, machine, 0, codeLength: 64); + writer.WriteUnsigned(0, 3); + writer.WriteUnsigned(0, machine == Machine.Amd64 ? 2 : 3); + writer.WriteUnsigned(ranges.Length, machine == Machine.ArmThumb2 ? 2 : 1); + int previousStop = 0; + foreach ((int start, int stop) in ranges) + { + writer.WriteUnsigned(start - previousStop, machine == Machine.ArmThumb2 ? 4 : 6); + writer.WriteUnsigned(stop - start - 1, 6); + previousStop = stop; + } + writer.Write(1, 1); + writer.WriteUnsigned(1, machine is Machine.Amd64 or Machine.ArmThumb2 ? 2 : 3); + writer.Write(0, 1); + writer.WriteUnsigned(0, machine == Machine.ArmThumb2 ? 2 : 3); + writer.Write(0, 2); + writer.WriteUnsigned(1, 3); + writer.Write(1, 1); + writer.Align(); + writer.Write(0, 1); // Simple could-be-live bitmap. + writer.Write(1, 1); + writer.Write(1, 1); // Final state is live. + writer.Write(1, 1); + writer.Write(transition, 6); + writer.Write(0, 1); + + GcInfo info = Decode(writer, machine); + KeyValuePair> entry = Assert.Single(info.Transitions); + Assert.Equal(expected * instructionSize, entry.Key); + Assert.Equal(entry.Key, Assert.Single(entry.Value).CodeOffset); + Assert.Equal(writer.ByteCount, info.Size); + } + + [Theory] + [InlineData(false, false)] + [InlineData(false, true)] + [InlineData(true, false)] + [InlineData(true, true)] + public void IndirectSafePointSizeIncludesEveryReferencedState(bool runLengthEncoded, bool highestStateLast) + { + BitWriter writer = new(); + writer.Write(0, 1); + writer.Write(1, 1); + writer.WriteUnsigned(128, 8); + writer.WriteUnsigned(64, 2); + for (int i = 1; i <= 64; i++) + writer.Write(i, 7); + writer.Write(0, 1); + writer.Write(1, 1); + writer.WriteUnsigned(32, 2); + writer.WriteUnsigned(0, 1); + for (int slot = 0; slot < 32; slot++) + { + writer.Write(1, 2); // SP-relative stack slot. + writer.WriteUnsigned(slot == 0 ? 0 : 1, slot == 0 ? 6 : 4); + if (slot == 0) + writer.Write(0, 2); + } + writer.Write(1, 1); + writer.WriteUnsigned(5, 3); // Six-bit indirect state offsets. + int firstStateSize = runLengthEncoded ? 12 : 33; + for (int i = 0; i < 64; i++) + writer.Write(i == (highestStateLast ? 63 : 0) ? firstStateSize : 0, 6); + writer.Align(); + for (int state = 0; state < 2; state++) + { + writer.Write(runLengthEncoded ? 1 : 0, 1); + if (runLengthEncoded) + { + writer.Write(0, 1); + writer.WriteUnsigned(state == 0 ? 32 : 0, 4); + if (state != 0) + writer.WriteUnsigned(31, 2); + } + else + { + for (int slot = 0; slot < 32; slot++) + writer.Write((slot + state) % 2, 1); + } + } + + GcInfo info = Decode(writer, Machine.Amd64); + Assert.Equal(64, info.LiveSlotsAtSafepoints.Count); + if (runLengthEncoded) + { + Assert.Equal(highestStateLast ? 0 : 32, info.LiveSlotsAtSafepoints[0].Count); + Assert.Equal(highestStateLast ? 32 : 0, info.LiveSlotsAtSafepoints[63].Count); + } + else + { + Assert.Equal(16, info.LiveSlotsAtSafepoints[0].Count); + Assert.Equal(16, info.LiveSlotsAtSafepoints[63].Count); + Assert.Same(info.SlotTable.GcSlots[highestStateLast ? 1 : 0], info.LiveSlotsAtSafepoints[0][0]); + } + Assert.Equal(writer.ByteCount, info.Size); + } + + private static void WriteFatHeader(BitWriter writer, Machine machine, int flags, int codeLength = 8) + { + writer.Write(1, 1); + writer.Write(flags, 10); + writer.WriteUnsigned(codeLength, machine == Machine.ArmThumb2 ? 7 : 8); + } + + private static GcInfo Decode(BitWriter writer, Machine machine) + { + using MemoryStream stream = new(writer.ToArray()); + + return new GcInfo(new NativeReader(stream), 0, machine, 5); + } + + private sealed class BitWriter + { + private readonly List _bytes = []; + private int _bitCount; + + public int ByteCount => (_bitCount + 7) / 8; + + public void Write(int value, int count) + { + for (int i = 0; i < count; i++, _bitCount++) + { + if (_bitCount % 8 == 0) + _bytes.Add(0); + _bytes[^1] |= (byte)(((value >> i) & 1) << (_bitCount % 8)); + } + } + + public void WriteUnsigned(int value, int encodingBase) + { + do + { + int chunk = value & ((1 << encodingBase) - 1); + value >>>= encodingBase; + Write(chunk | (value == 0 ? 0 : 1 << encodingBase), encodingBase + 1); + } while (value != 0); + } + + public void Align() + { + while (_bitCount % 8 != 0) + Write(0, 1); + } + + public byte[] ToArray() => [.. _bytes, .. new byte[8]]; + } +} diff --git a/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/Amd64/GcInfo.cs b/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/Amd64/GcInfo.cs index b38be1644ff478..aaa9f120284c0c 100644 --- a/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/Amd64/GcInfo.cs +++ b/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/Amd64/GcInfo.cs @@ -159,6 +159,10 @@ public GcInfo(NativeReader imageReader, int offset, Machine machine, int version if (_hasSizeOfEditAndContinuePreservedArea) { SizeOfEditAndContinuePreservedArea = imageReader.DecodeVarLengthUnsigned(_gcInfoTypes.SIZE_OF_EDIT_AND_CONTINUE_PRESERVED_AREA_ENCBASE, ref bitOffset); + if (_machine == Machine.Arm64) + { + imageReader.DecodeVarLengthUnsigned(_gcInfoTypes.SIZE_OF_EDIT_AND_CONTINUE_FIXED_STACK_FRAME_ENCBASE, ref bitOffset); + } } if (_hasReversePInvokeFrame) @@ -408,6 +412,7 @@ private List> GetLiveSlotsAtSafepoints(NativeReader imageReader } uint offsetTablePos = (uint)bitOffset; + int maxBitOffset = bitOffset; for (uint safePointIndex = 0; safePointIndex < NumSafePoints; safePointIndex++) { @@ -458,6 +463,7 @@ private List> GetLiveSlotsAtSafepoints(NativeReader imageReader report = !report; } Debug.Assert(readSlots == numSlots); + maxBitOffset = Math.Max(maxBitOffset, bitOffset); result.Add(liveSlots); continue; } @@ -489,9 +495,11 @@ private List> GetLiveSlotsAtSafepoints(NativeReader imageReader } } + maxBitOffset = Math.Max(maxBitOffset, bitOffset); result.Add(liveSlots); } + bitOffset = maxBitOffset; return result; } @@ -666,31 +674,33 @@ private Dictionary> UpdateTransitionCodeOffset(List< { Dictionary> updatedTransitions = new Dictionary>(); int cumInterruptibleLength = 0; // the sum of the lengths of all preceding interruptible ranges - using (IEnumerator interruptibleRangesIter = InterruptibleRanges.GetEnumerator()) + int rangeIndex = 0; + foreach (GcTransition transition in transitions) { - interruptibleRangesIter.MoveNext(); - InterruptibleRange currentRange = interruptibleRangesIter.Current; - int currentRangeLength = (int)(currentRange.StopOffset - currentRange.StartOffset); - foreach (GcTransition transition in transitions) + int codeOffset = (int)_gcInfoTypes.DenormalizeCodeOffset((uint)transition.CodeOffset); + if (InterruptibleRanges.Count != 0) { - int codeOffset = transition.CodeOffset + (int)currentRange.StartOffset - cumInterruptibleLength; - if (codeOffset > currentRange.StopOffset) + InterruptibleRange currentRange = InterruptibleRanges[rangeIndex]; + int currentRangeLength = (int)(currentRange.StopOffset - currentRange.StartOffset); + while (codeOffset >= cumInterruptibleLength + currentRangeLength) { cumInterruptibleLength += currentRangeLength; - interruptibleRangesIter.MoveNext(); - currentRange = interruptibleRangesIter.Current; + if (++rangeIndex >= InterruptibleRanges.Count) + throw new BadImageFormatException("GC transition lies outside the interruptible ranges."); + currentRange = InterruptibleRanges[rangeIndex]; currentRangeLength = (int)(currentRange.StopOffset - currentRange.StartOffset); - codeOffset = transition.CodeOffset + (int)currentRange.StartOffset - cumInterruptibleLength; } + codeOffset += (int)currentRange.StartOffset - cumInterruptibleLength; + } - transition.CodeOffset = codeOffset; - if (!updatedTransitions.ContainsKey(codeOffset)) - { - updatedTransitions[codeOffset] = new List(); - } - updatedTransitions[codeOffset].Add(transition); + transition.CodeOffset = codeOffset; + if (!updatedTransitions.ContainsKey(codeOffset)) + { + updatedTransitions[codeOffset] = new List(); } + updatedTransitions[codeOffset].Add(transition); } + return updatedTransitions; } } diff --git a/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/GCInfoTypes.cs b/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/GCInfoTypes.cs index 6dd5387285826f..96e75281d2cb61 100644 --- a/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/GCInfoTypes.cs +++ b/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/GCInfoTypes.cs @@ -100,6 +100,7 @@ public class GcInfoTypes internal int GENERICS_INST_CONTEXT_STACK_SLOT_ENCBASE { get; } = 6; internal int STACK_BASE_REGISTER_ENCBASE { get; } = 3; internal int SIZE_OF_EDIT_AND_CONTINUE_PRESERVED_AREA_ENCBASE { get; } = 4; + internal int SIZE_OF_EDIT_AND_CONTINUE_FIXED_STACK_FRAME_ENCBASE { get; } = 4; internal int REVERSE_PINVOKE_FRAME_ENCBASE { get; } = 6; internal int SIZE_OF_STACK_AREA_ENCBASE { get; } = 3; internal int NUM_SAFE_POINTS_ENCBASE { get; } = 3; @@ -279,9 +280,9 @@ internal uint DenormalizeStackBaseRegister(uint x) case Machine.Arm64: return (x ^ 29); case Machine.LoongArch64: - return ((x ^ 22) & 0x3); + return x == 0 ? 22u : 3u; case Machine.RiscV64: - return (x ^ 8); + return x == 0 ? 8u : 2u; } return x; } diff --git a/src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/GCInfo/GCInfoDecoder.cs b/src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/GCInfo/GCInfoDecoder.cs index b5ac9c24c5fa9b..0f6aa4bf7a0616 100644 --- a/src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/GCInfo/GCInfoDecoder.cs +++ b/src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/GCInfo/GCInfoDecoder.cs @@ -22,6 +22,7 @@ private enum DecodePoints GenericInstContext, EditAndContinue, ReversePInvoke, + StackParameterArea, InterruptibleRanges, SlotTable, Complete, @@ -395,6 +396,7 @@ private IEnumerable DecodeSlimHeader() yield return DecodePoints.GenericInstContext; yield return DecodePoints.EditAndContinue; yield return DecodePoints.ReversePInvoke; + yield return DecodePoints.StackParameterArea; if (PartiallyInterruptibleGCSupported) { @@ -487,6 +489,7 @@ private IEnumerable DecodeFatHeader() _fixedStackParameterScratchArea = TTraits.DenormalizeSizeOfStackArea(_reader.DecodeVarLengthUnsigned(TTraits.SIZE_OF_STACK_AREA_ENCBASE, ref _bitOffset)); } + yield return DecodePoints.StackParameterArea; if (PartiallyInterruptibleGCSupported) { @@ -514,7 +517,7 @@ private void EnsureDecodedTo(DecodePoints point) public GCInfoHeader GetHeader() { - EnsureDecodedTo(DecodePoints.ReversePInvoke); + EnsureDecodedTo(DecodePoints.StackParameterArea); GcInfoHeaderFlags genericsInstContextFlags = _headerFlags & GcInfoHeaderFlags.GC_INFO_HAS_GENERICS_INST_CONTEXT_MASK; SpecialSlot? gsCookie = _gsCookieStackSlot != TTraits.NO_GS_COOKIE ? new SpecialSlot(_gsCookieStackSlot) : null; diff --git a/src/native/managed/cdac/tests/UnitTests/GCInfoHeaderTests.cs b/src/native/managed/cdac/tests/UnitTests/GCInfoHeaderTests.cs new file mode 100644 index 00000000000000..c55b6cdad1cd39 --- /dev/null +++ b/src/native/managed/cdac/tests/UnitTests/GCInfoHeaderTests.cs @@ -0,0 +1,83 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Microsoft.Diagnostics.DataContractReader.Contracts; +using Microsoft.Diagnostics.DataContractReader.Contracts.GCInfoHelpers; +using Microsoft.Diagnostics.DataContractReader.TestInfrastructure; +using Moq; +using Xunit; + +namespace Microsoft.Diagnostics.DataContractReader.Tests; + +public class GCInfoHeaderTests +{ + [Theory] + [InlineData(RuntimeInfoArchitecture.X64, 24u)] + [InlineData(RuntimeInfoArchitecture.Arm, 12u)] + [InlineData(RuntimeInfoArchitecture.Arm64, 24u)] + [InlineData(RuntimeInfoArchitecture.LoongArch64, 24u)] + [InlineData(RuntimeInfoArchitecture.RiscV64, 24u)] + public void GetHeaderDecodesOutgoingAreaBeforeSlotLifetimes(RuntimeInfoArchitecture architecture, uint expectedSize) + { + byte[] encoded = architecture == RuntimeInfoArchitecture.Arm + ? [0x03, 0x40, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00] + : [0x03, 0x40, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00]; + IGCInfoDecoder decoder = CreateDecoder(architecture, encoded); + + GCInfoHeader header = decoder.GetHeader(); + Assert.Equal(expectedSize, header.SizeOfStackParameterArea); + Assert.Empty(decoder.GetSlotLifetimes()); + Assert.Equal(expectedSize, decoder.GetHeader().SizeOfStackParameterArea); + } + + [Fact] + public void GenericContextLookupStopsBeforeOutgoingArea() + { + // The outgoing-area encoding is deliberately unterminated. + IGCInfoDecoder decoder = CreateDecoder(RuntimeInfoArchitecture.X64, + [0x03, 0x40, 0x80, 0x88, 0x88, 0x88, 0x88, 0x88]); + + Assert.False(decoder.TryGetGenericContextStorage(GenericContextLoc.ThisPtr, 0, out _)); + } + + [Fact] + public void SlimHeaderStopsBeforeSafePointCount() + { + // The safe-point count is deliberately unterminated. + IGCInfoDecoder decoder = CreateDecoder(RuntimeInfoArchitecture.X64, [0x22, 0xF8]); + + GCInfoHeader header = decoder.GetHeader(); + Assert.Equal(8u, header.CodeSize); + Assert.Equal(0u, header.SizeOfStackParameterArea); + } + + private static IGCInfoDecoder CreateDecoder(RuntimeInfoArchitecture architecture, byte[] encoded) + { + MockTarget.Architecture arch = new() + { + IsLittleEndian = true, + Is64Bit = architecture != RuntimeInfoArchitecture.Arm, + }; + TestPlaceholderTarget.Builder builder = new(arch); + const ulong GCInfoAddress = 0x10000; + builder.MemoryBuilder.AddHeapFragment(new MockMemorySpace.HeapFragment + { + Address = GCInfoAddress, + Data = encoded, + Name = "GCInfo header", + }); + Mock runtimeInfo = new(); + runtimeInfo.Setup(c => c.GetTargetArchitecture()).Returns(architecture); + Target target = builder.AddMockContract(runtimeInfo).Build(); + TargetPointer address = new(GCInfoAddress); + return architecture switch + { + RuntimeInfoArchitecture.X64 => new GcInfoDecoder(target, address, 5), + RuntimeInfoArchitecture.Arm => new GcInfoDecoder(target, address, 5), + RuntimeInfoArchitecture.Arm64 => new GcInfoDecoder(target, address, 5), + RuntimeInfoArchitecture.LoongArch64 => new GcInfoDecoder(target, address, 5), + RuntimeInfoArchitecture.RiscV64 => new GcInfoDecoder(target, address, 5), + _ => throw new System.InvalidOperationException(), + }; + } +}