Skip to content

Commit

Permalink
Add aarch64 testing
Browse files Browse the repository at this point in the history
  • Loading branch information
romainthomas committed Dec 8, 2024
1 parent 9c0c01a commit 5aa3d72
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
2 changes: 2 additions & 0 deletions api/rust/cargo/lief/src/assembly/aarch64/operands/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl Operand for Memory {
}

/// Wraps a memory offset as an integer offset or as a register offset
#[derive(Debug)]
pub enum Offset {
/// Register offset
Reg(Reg),
Expand Down Expand Up @@ -68,6 +69,7 @@ impl From<i32> for Shift {
}

/// This structure holds shift info (type + value)
#[derive(Debug)]
pub struct ShiftInfo {
pub shift_type: Shift,
pub value: i8,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl Operand for Register {
}
}

#[derive(Debug)]
pub enum Value {
Reg(Reg),
SysReg(SysReg),
Expand Down
80 changes: 77 additions & 3 deletions tests/assembly/test_arm64.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
if not lief.__extended__:
pytest.skip("skipping: extended version only", allow_module_level=True)

def _create_inst(elf: lief.ELF.Binary, addr: int,
asm: str) -> lief.assembly.aarch64.Instruction:
return next(elf.disassemble_from_bytes(elf.assemble(addr, asm), addr))

def test_arm64e():
macho = lief.MachO.parse(get_sample("MachO/ios17/DebugHierarchyKit")).at(0)

Expand Down Expand Up @@ -59,6 +63,76 @@ def test_elf_arm64():
assert isinstance(instructions[0], lief.assembly.aarch64.Instruction)
assert instructions[0].opcode == lief.assembly.aarch64.OPCODE.PACIASP

#def test_arm64_operands():
# elf = lief.ELF.parse(get_sample("ELF/libmonochrome-arm64.so"))
# instructions = list(elf.disassemble(elf.get_section((".text").virtual_address)))
def test_arm64_operands():
elf = lief.ELF.parse(get_sample("ELF/libmonochrome-arm64.so"))

inst = _create_inst(elf, 0x18a5000, "mov x0, #1")
operands = list(inst.operands)
assert len(operands) == 3
assert isinstance(operands[0], lief.assembly.aarch64.operands.Register)
assert operands[0].value == lief.assembly.aarch64.REG.X0

assert isinstance(operands[1], lief.assembly.aarch64.operands.Immediate)
assert operands[1].value == 1

inst = _create_inst(elf, 0x18a5000, "mrs x23, TPIDR_EL0")
operands = list(inst.operands)
assert len(operands) == 2
assert isinstance(operands[0], lief.assembly.aarch64.operands.Register)
assert operands[0].value == lief.assembly.aarch64.REG.X23

assert isinstance(operands[1], lief.assembly.aarch64.operands.Register)
assert operands[1].value == lief.assembly.aarch64.SYSREG.TPIDR_EL0

inst = _create_inst(elf, 0x18a5000, "ldr x1, [x2, x3, lsl #3]")
operands = list(inst.operands)
assert len(operands) == 2
assert isinstance(operands[0], lief.assembly.aarch64.operands.Register)
assert operands[0].value == lief.assembly.aarch64.REG.X1

assert isinstance(operands[1], lief.assembly.aarch64.operands.Memory)
mem_info: lief.assembly.aarch64.operands.Memory = operands[1]
assert mem_info.base == lief.assembly.aarch64.REG.X2
assert mem_info.offset == lief.assembly.aarch64.REG.X3
assert mem_info.shift.type == lief.assembly.aarch64.operands.Memory.SHIFT.LSL
assert mem_info.shift.value == 3

inst = _create_inst(elf, 0x18a5000, "str x3, [x2], #8")
operands = list(inst.operands)
assert len(operands) == 4
assert isinstance(operands[0], lief.assembly.aarch64.operands.Register)
assert operands[0].value == lief.assembly.aarch64.REG.X2

assert isinstance(operands[1], lief.assembly.aarch64.operands.Register)
assert operands[1].value == lief.assembly.aarch64.REG.X3

assert isinstance(operands[2], lief.assembly.aarch64.operands.Memory)
mem_info: lief.assembly.aarch64.operands.Memory = operands[2]
assert mem_info.base == lief.assembly.aarch64.REG.X2
assert mem_info.offset is None
assert mem_info.shift.value == -1

assert isinstance(operands[3], lief.assembly.aarch64.operands.Immediate)
assert operands[3].value == 8

inst = _create_inst(elf, 0x18a5000, "str x3, [x2, #8]")
operands = list(inst.operands)
assert len(operands) == 2

assert isinstance(operands[0], lief.assembly.aarch64.operands.Register)
assert operands[0].value == lief.assembly.aarch64.REG.X3

assert isinstance(operands[1], lief.assembly.aarch64.operands.Memory)
mem_info: lief.assembly.aarch64.operands.Memory = operands[1]
assert mem_info.base == lief.assembly.aarch64.REG.X2
assert mem_info.offset == 8

inst = _create_inst(elf, 0x18a5000, "adrp x0, #0x1000")
operands = list(inst.operands)
assert len(operands) == 2

assert isinstance(operands[0], lief.assembly.aarch64.operands.Register)
assert operands[0].value == lief.assembly.aarch64.REG.X0

assert isinstance(operands[1], lief.assembly.aarch64.operands.PCRelative)
assert operands[1].value == 1

0 comments on commit 5aa3d72

Please sign in to comment.