Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Revert "Use BinaryPrimitives in ILImporter"
* One day ILVerify drops NS2.0, hopefully.

This reverts commit caeb467.
  • Loading branch information
PaulusParssinen committed Jul 24, 2024
commit ca90179babc4c35106dac8122b9ff86e6a8e89a8
42 changes: 17 additions & 25 deletions src/coreclr/tools/Common/TypeSystem/IL/ILImporter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// 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.Buffers.Binary;
using Internal.TypeSystem;

namespace Internal.IL
Expand All @@ -22,28 +20,30 @@ internal sealed partial class ILImporter

private byte ReadILByte()
{
if (_currentOffset + 1 > _ilBytes.Length)
if (_currentOffset >= _ilBytes.Length)
ReportMethodEndInsideInstruction();

return _ilBytes[_currentOffset++];
}

private ushort ReadILUInt16()
{
if (!BinaryPrimitives.TryReadUInt16LittleEndian(_ilBytes.AsSpan(_currentOffset), out ushort value))
if (_currentOffset + 1 >= _ilBytes.Length)
ReportMethodEndInsideInstruction();

_currentOffset += sizeof(ushort);
return value;
ushort val = (ushort)(_ilBytes[_currentOffset] + (_ilBytes[_currentOffset + 1] << 8));
_currentOffset += 2;
return val;
}

private uint ReadILUInt32()
{
if (!BinaryPrimitives.TryReadUInt32LittleEndian(_ilBytes.AsSpan(_currentOffset), out uint value))
if (_currentOffset + 3 >= _ilBytes.Length)
ReportMethodEndInsideInstruction();

_currentOffset += sizeof(uint);
return value;
uint val = (uint)(_ilBytes[_currentOffset] + (_ilBytes[_currentOffset + 1] << 8) + (_ilBytes[_currentOffset + 2] << 16) + (_ilBytes[_currentOffset + 3] << 24));
_currentOffset += 4;
return val;
}

private int ReadILToken()
Expand All @@ -53,29 +53,21 @@ private int ReadILToken()

private ulong ReadILUInt64()
{
if (!BinaryPrimitives.TryReadUInt64LittleEndian(_ilBytes.AsSpan(_currentOffset), out ulong value))
ReportMethodEndInsideInstruction();

_currentOffset += sizeof(ulong);
ulong value = ReadILUInt32();
value |= (((ulong)ReadILUInt32()) << 32);
return value;
}

private float ReadILFloat()
private unsafe float ReadILFloat()
{
if (!BinaryPrimitives.TryReadSingleLittleEndian(_ilBytes.AsSpan(_currentOffset), out float value))
ReportMethodEndInsideInstruction();

_currentOffset += sizeof(float);
return value;
uint value = ReadILUInt32();
return *(float*)(&value);
}

private double ReadILDouble()
private unsafe double ReadILDouble()
{
if (!BinaryPrimitives.TryReadDoubleLittleEndian(_ilBytes.AsSpan(_currentOffset), out double value))
ReportMethodEndInsideInstruction();

_currentOffset += sizeof(double);
return value;
ulong value = ReadILUInt64();
return *(double*)(&value);
}

private void SkipIL(int bytes)
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/tools/Common/TypeSystem/IL/ILReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public float ReadILFloat()
return value;
}

public double ReadILDouble()
public unsafe double ReadILDouble()
{
if (!BinaryPrimitives.TryReadDoubleLittleEndian(_ilBytes.Slice(_currentOffset), out double value))
ThrowHelper.ThrowInvalidProgramException();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// 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.Buffers.Binary;
using Internal.TypeSystem;
using Internal.ReadyToRunConstants;

Expand Down Expand Up @@ -1382,7 +1380,10 @@ private void ImportFallthrough(BasicBlock next, object condition = null)

private int ReadILTokenAt(int ilOffset)
{
return BinaryPrimitives.ReadInt32LittleEndian(_ilBytes.AsSpan(ilOffset, sizeof(int)));
return (int)(_ilBytes[ilOffset]
+ (_ilBytes[ilOffset + 1] << 8)
+ (_ilBytes[ilOffset + 2] << 16)
+ (_ilBytes[ilOffset + 3] << 24));
}

private static void ReportInvalidBranchTarget(int targetOffset)
Expand Down