Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d38af0c
chore: cleanup arm64 disassembler related code
filzrev Aug 23, 2026
9b385c9
chore: modify clrmd related code to use interface
filzrev Aug 25, 2026
a10f3cc
chore: modify arm64 instruction formatter padding logics
filzrev Sep 1, 2026
586f111
chore: add arm64 disassembler unit tests
filzrev Sep 2, 2026
ca360a7
chore: update address to 4gb as minimum base address
filzrev Sep 4, 2026
a1233fc
chore: update AsmArm64 version and update related tests
filzrev Sep 6, 2026
961c406
chore: modify code based on review comment
filzrev Sep 6, 2026
aaa2671
chore: fix wrong movz test assertion (currently it's skipped)
filzrev Sep 7, 2026
025c00a
chore: fix code that pointed out by code review
filzrev Sep 8, 2026
28946dd
chore: add DebuggerHidden attribute on helper methods
filzrev Sep 9, 2026
3e393b3
chore: add code to throw dedicated exception when unexpectedly MockCl…
filzrev Sep 9, 2026
95bb6b4
chore: add using for MockClrInstance instance
filzrev Sep 10, 2026
11dfa26
chore: fix wrong comment on instructions
filzrev Sep 10, 2026
132e096
chore: add MockMemory class and refator codes
filzrev Sep 10, 2026
3094f3c
chore: add remaining issue comment link
filzrev Sep 10, 2026
2304b83
chore: cleanup MockDataReader
filzrev Sep 10, 2026
0ad2fd1
chore: fix typo
filzrev Sep 10, 2026
7124b6c
chore: add unit tests for code coverage
filzrev Sep 10, 2026
89efdaa
chore: fix UnreachableException visibility to internal and #if directive
filzrev Sep 10, 2026
924add6
chore: modify CreateValueAccumulator logics with initial state
filzrev Sep 10, 2026
edcdf9e
chore: remove not used extension methods
filzrev Sep 10, 2026
5249d5c
chore: update wrong doc comment
filzrev Sep 10, 2026
eee29f8
chore: add reset api to set initial accumulator state
filzrev Sep 15, 2026
2d6b4e8
chore: remove unused baseAddress parameter
filzrev Sep 15, 2026
5aa4678
chore: cleanup #if directives
filzrev Sep 15, 2026
edc10f5
chore: add ThrowExceptionMode property and not throw by default
filzrev Sep 15, 2026
8ddcd31
chore: suppress padding when instruction has no operands
filzrev Sep 16, 2026
abcc4ea
chore: modify test instructions to pass all trampoline code pass
filzrev Sep 16, 2026
a3ca150
chore: add todo comment that be handled on next pr
filzrev Sep 17, 2026
2d97e70
chore: fix wrong comment
filzrev Sep 17, 2026
5ea2d39
chore: enable throwExceptionMode per test and return 0 when address i…
filzrev Sep 17, 2026
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
148 changes: 10 additions & 138 deletions src/BenchmarkDotNet/Disassemblers/Arm64Disassembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,143 +2,13 @@
using Gee.External.Capstone;
using Gee.External.Capstone.Arm64;
using Microsoft.Diagnostics.Runtime;
using Microsoft.Diagnostics.Runtime.Interfaces;

namespace BenchmarkDotNet.Disassemblers
{
internal struct RegisterValueAccumulator
{
private enum State
{
LookingForPattern,
ExpectingMovk,
ExpectingAdd,
LookingForPossibleLdr
}

private State _state;
private long _value;
private int _expectedMovkShift;
private Arm64RegisterId _registerId;
private ClrRuntime _runtime;

public void Init(ClrRuntime runtime)
{
_state = State.LookingForPattern;
_expectedMovkShift = 0;
_value = 0;
_registerId = Arm64RegisterId.Invalid;
_runtime = runtime;
}

public void Feed(Arm64Instruction instruction)
{
Arm64InstructionDetail details = instruction.Details;

switch (_state)
{
case State.LookingForPattern:
if (instruction.Id == Arm64InstructionId.ARM64_INS_MOVZ)
{
_registerId = details.Operands[0].Register.Id;
_value = details.Operands[1].Immediate;
_state = State.ExpectingMovk;
_expectedMovkShift = 16;
}
else if (instruction.Id == Arm64InstructionId.ARM64_INS_ADRP)
{
_registerId = details.Operands[0].Register.Id;
_value = details.Operands[1].Immediate;
_state = State.ExpectingAdd;
}
break;
case State.ExpectingMovk:
if (instruction.Id == Arm64InstructionId.ARM64_INS_MOVK &&
details.Operands[0].Register.Id == _registerId &&
details.Operands[1].ShiftOperation == Arm64ShiftOperation.ARM64_SFT_LSL &&
details.Operands[1].ShiftValue == _expectedMovkShift)
{
_value = _value | (instruction.Details.Operands[1].Immediate << details.Operands[1].ShiftValue);
_expectedMovkShift += 16;
break;
}
_state = State.LookingForPossibleLdr;
goto case State.LookingForPossibleLdr;
case State.ExpectingAdd:
if (instruction.Id == Arm64InstructionId.ARM64_INS_ADD &&
details.Operands[0].Register.Id == _registerId &&
details.Operands[1].Register.Id == _registerId &&
details.Operands[2].Type == Arm64OperandType.Immediate)
{
_value = _value | instruction.Details.Operands[2].Immediate;
_state = State.LookingForPossibleLdr;
}
break;
case State.LookingForPossibleLdr:
if (instruction.Id == Arm64InstructionId.ARM64_INS_LDR &&
details.Operands[1].Type == Arm64OperandType.Memory &&
details.Operands[1].Memory.Base.Id == _registerId && // The source address is in the register we are tracking
details.Operands[1].Memory.Displacement == 0 && // There is no displacement
details.Operands[1].Memory.Index == null) // And there is no extra index register
{
// Simulate the LDR instruction.
long newValue = (long)_runtime.DataTarget.DataReader.ReadPointer((ulong)_value);
_value = newValue;
if (_value == 0)
{
_state = State.LookingForPattern;
}
else
{
// The LDR might have loaded the result in another register
_registerId = details.Operands[0].Register.Id;
}
}
else if (instruction.Id == Arm64InstructionId.ARM64_INS_CBZ ||
instruction.Id == Arm64InstructionId.ARM64_INS_CBNZ ||
instruction.Id == Arm64InstructionId.ARM64_INS_B && details.ConditionCode != Arm64ConditionCode.Invalid)
{
// ignore conditional branches
}
else if (details.BelongsToGroup(Arm64InstructionGroupId.ARM64_GRP_BRANCH_RELATIVE) ||
details.BelongsToGroup(Arm64InstructionGroupId.ARM64_GRP_CALL) ||
details.BelongsToGroup(Arm64InstructionGroupId.ARM64_GRP_JUMP))
{
// We've encountered an unconditional jump or call, the accumulated registers value is not valid anymore
_state = State.LookingForPattern;
}
else if (instruction.Id == Arm64InstructionId.ARM64_INS_MOVZ)
{
// Another constant loading is starting
_state = State.LookingForPattern;
goto case State.LookingForPattern;
}
else
{
// Finally check if the current instruction modified the register that was accumulating the constant
// and reset the state machine in case it did.
foreach (Arm64Register reg in details.AllWrittenRegisters)
{
// Some unexpected instruction overwriting the accumulated register
if (reg.Id == _registerId)
{
_state = State.LookingForPattern;
}
}
}
break;
}
}

public bool HasValue => _state == State.ExpectingMovk || _state == State.LookingForPossibleLdr;

public long Value { get { return _value; } }

public Arm64RegisterId RegisterId { get { return _registerId; } }
}

internal class Arm64Disassembler : ClrMdDisassembler
{
protected override IEnumerable<Asm> Decode(byte[] code, ulong startAddress, State state, int depth, ClrMethod currentMethod, DisassemblySyntax syntax)
protected override IEnumerable<Asm> Decode(byte[] code, ulong startAddress, State state, int depth, IClrMethod currentMethod, DisassemblySyntax syntax)
{
const Arm64DisassembleMode disassembleMode = Arm64DisassembleMode.Arm;
using (CapstoneArm64Disassembler disassembler = CapstoneDisassembler.CreateArm64Disassembler(disassembleMode))
Expand All @@ -147,7 +17,8 @@ protected override IEnumerable<Asm> Decode(byte[] code, ulong startAddress, Stat
// disassembled binary code.
disassembler.EnableInstructionDetails = true;
disassembler.DisassembleSyntax = Map(syntax);
RegisterValueAccumulator accumulator = new RegisterValueAccumulator();

Arm64RegisterValueAccumulator accumulator = new();
accumulator.Init(state.Runtime);

Arm64Instruction[] instructions = disassembler.Disassemble(code, (long)startAddress);
Expand Down Expand Up @@ -267,26 +138,27 @@ private static bool TryResolvePrecode(IDataReader reader, ref ulong address, out
return false;
}

// .NET 10 prefixes some AArch64 precode/stub shapes with `DMB ISH` for concurrent
// .NET 10 prefixes some AArch64 precode/stub shapes with `DMB ISHLD` for concurrent
// stub-patching safety. Detect and skip the barrier so the existing 3-instruction pattern
// match still works, and return the effective PC of the first real stub instruction so
// LDR-literal offsets are calculated relative to it.
// Encoding: DMB ISH = 0xD50339BF.
private const uint DmbIshInstr = 0xD50339BFu;
// Encoding: DMB ISHLD = 0xD50339BF.
private const uint DmbIshLdInstr = 0xD50339BFu;

private static bool TryReadStubHead(IDataReader reader, ulong address, out ulong parseBase, out uint instr0, out uint instr1, out uint instr2)
{
parseBase = address;
instr0 = instr1 = instr2 = 0;

byte[] buffer = new byte[16];
// TODO: IDataReader.Read might return partial data. It need to call multiple times until buffer filled or readLength<=0.
int read = reader.Read(address, buffer);
if (read < 12)
return false;

int offset = 0;
uint first = ReadInstr(buffer, 0);
if (first == DmbIshInstr)
if (first == DmbIshLdInstr)
{
if (read < 16)
Comment thread
timcassell marked this conversation as resolved.
return false;
Expand Down Expand Up @@ -324,7 +196,7 @@ private static bool IsLdrLiteral64(uint instr, out int rt, out int offsetBytes)
return true;
}

private static bool TryGetReferencedAddress(Arm64Instruction instruction, RegisterValueAccumulator accumulator, uint pointerSize, out ulong referencedAddress, out bool isReferencedAddressIndirect)
private static bool TryGetReferencedAddress(Arm64Instruction instruction, Arm64RegisterValueAccumulator accumulator, uint pointerSize, out ulong referencedAddress, out bool isReferencedAddressIndirect)
{
if ((instruction.Id == Arm64InstructionId.ARM64_INS_BR || instruction.Id == Arm64InstructionId.ARM64_INS_BLR) && instruction.Details.Operands[0].Register.Id == accumulator.RegisterId && accumulator.HasValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ internal static string Format(Arm64Asm asm, FormatterOptions formatterOptions,
FormatInstructionPointer(instruction, formatterOptions, pointerSize, output);
}

output.Append(instruction.Mnemonic.ToString().PadRight(formatterOptions.FirstOperandCharIndex));
var padRight = instruction.Details.Operands.Length > 0
? Math.Max(formatterOptions.FirstOperandCharIndex, instruction.Mnemonic.Length + 1)
: 0;
output.Append(instruction.Mnemonic.PadRight(padRight));

if (asm.ReferencedAddress.HasValue && !asm.IsReferencedAddressIndirect && symbols.TryGetValue(asm.ReferencedAddress.Value, out var name))
{
Expand Down
144 changes: 144 additions & 0 deletions src/BenchmarkDotNet/Disassemblers/Arm64RegisterValueAccumulator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
using Gee.External.Capstone.Arm64;
using Microsoft.Diagnostics.Runtime.Interfaces;

namespace BenchmarkDotNet.Disassemblers;

internal struct Arm64RegisterValueAccumulator
{
internal enum State
{
LookingForPattern,
ExpectingMovk,
ExpectingAdd,
LookingForPossibleLdr
}

private State _state;
private long _value;
private int _expectedMovkShift;
private Arm64RegisterId _registerId;
private IClrRuntime _runtime;

public void Init(IClrRuntime runtime)
{
_runtime = runtime;
Reset();
}

internal void Reset(
State state = State.LookingForPattern,
int expectedMovkShift = 0,
long value = 0,
Arm64RegisterId registerId = Arm64RegisterId.Invalid)
{
_state = state;
_expectedMovkShift = expectedMovkShift;
_value = value;
_registerId = registerId;
}

public void Feed(Arm64Instruction instruction)
{
Arm64InstructionDetail details = instruction.Details;

switch (_state)
{
case State.LookingForPattern:
if (instruction.Id == Arm64InstructionId.ARM64_INS_MOVZ)
{
_registerId = details.Operands[0].Register.Id;
_value = details.Operands[1].Immediate;
_state = State.ExpectingMovk;
_expectedMovkShift = 16;
}
else if (instruction.Id == Arm64InstructionId.ARM64_INS_ADRP)
{
_registerId = details.Operands[0].Register.Id;
_value = details.Operands[1].Immediate;
_state = State.ExpectingAdd;
}
break;
case State.ExpectingMovk:
if (instruction.Id == Arm64InstructionId.ARM64_INS_MOVK &&
details.Operands[0].Register.Id == _registerId &&
details.Operands[1].ShiftOperation == Arm64ShiftOperation.ARM64_SFT_LSL &&
details.Operands[1].ShiftValue == _expectedMovkShift)
{
_value = _value | (instruction.Details.Operands[1].Immediate << details.Operands[1].ShiftValue);
_expectedMovkShift += 16;
break;
}
_state = State.LookingForPossibleLdr;
goto case State.LookingForPossibleLdr;
case State.ExpectingAdd:
if (instruction.Id == Arm64InstructionId.ARM64_INS_ADD &&
details.Operands[0].Register.Id == _registerId &&
details.Operands[1].Register.Id == _registerId &&
details.Operands[2].Type == Arm64OperandType.Immediate)
{
_value = _value | instruction.Details.Operands[2].Immediate;
_state = State.LookingForPossibleLdr;
}
break;
case State.LookingForPossibleLdr:
if (instruction.Id == Arm64InstructionId.ARM64_INS_LDR &&
details.Operands[1].Type == Arm64OperandType.Memory &&
details.Operands[1].Memory.Base.Id == _registerId && // The source address is in the register we are tracking
details.Operands[1].Memory.Displacement == 0 && // There is no displacement
details.Operands[1].Memory.Index == null) // And there is no extra index register
{
// Simulate the LDR instruction.
long newValue = (long)_runtime.DataTarget.DataReader.ReadPointer((ulong)_value);
_value = newValue;
if (_value == 0)
{
_state = State.LookingForPattern;
}
else
{
// The LDR might have loaded the result in another register
_registerId = details.Operands[0].Register.Id;
}
}
else if (instruction.Id == Arm64InstructionId.ARM64_INS_CBZ ||
instruction.Id == Arm64InstructionId.ARM64_INS_CBNZ ||
instruction.Id == Arm64InstructionId.ARM64_INS_B && details.ConditionCode != Arm64ConditionCode.Invalid)
{
// ignore conditional branches
}
else if (details.BelongsToGroup(Arm64InstructionGroupId.ARM64_GRP_BRANCH_RELATIVE) ||
details.BelongsToGroup(Arm64InstructionGroupId.ARM64_GRP_CALL) ||
details.BelongsToGroup(Arm64InstructionGroupId.ARM64_GRP_JUMP))
{
// We've encountered an unconditional jump or call, the accumulated registers value is not valid anymore
_state = State.LookingForPattern;
}
else if (instruction.Id == Arm64InstructionId.ARM64_INS_MOVZ)
{
// Another constant loading is starting
_state = State.LookingForPattern;
goto case State.LookingForPattern;
}
else
{
// Finally check if the current instruction modified the register that was accumulating the constant
// and reset the state machine in case it did.
foreach (Arm64Register reg in details.AllWrittenRegisters)
{
// Some unexpected instruction overwriting the accumulated register
if (reg.Id == _registerId)
{
_state = State.LookingForPattern;
}
}
}
break;
}
}

public bool HasValue => _state == State.ExpectingMovk || _state == State.LookingForPossibleLdr;

public long Value => _value;

public Arm64RegisterId RegisterId => _registerId;
}
Loading
Loading