Skip to content

Commit b7e0d21

Browse files
committed
Fix LDR not assigning immediate as memory offset.
See: capstone-engine#2015 (comment)
1 parent cc01c04 commit b7e0d21

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

arch/AArch64/AArch64Mapping.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2559,6 +2559,17 @@ void AArch64_set_detail_op_reg(MCInst *MI, unsigned OpNum, aarch64_reg Reg)
25592559
AArch64_inc_op_count(MI);
25602560
}
25612561

2562+
/// Check if the previous operand is a memory operand
2563+
/// with only the base register set AND if this base register
2564+
/// is write-back.
2565+
/// This indicates the following immediate is a post-indexed
2566+
/// memory offset.
2567+
static bool prev_is_membase_wb(MCInst *MI) {
2568+
return AArch64_get_detail_op(MI, -1)->type == AARCH64_OP_MEM &&
2569+
AArch64_get_detail_op(MI, -1)->mem.disp == 0 &&
2570+
get_detail(MI)->writeback;
2571+
}
2572+
25622573
/// Adds an immediate AArch64 operand at position OpNum and increases the op_count
25632574
/// by one.
25642575
void AArch64_set_detail_op_imm(MCInst *MI, unsigned OpNum,
@@ -2581,7 +2592,7 @@ void AArch64_set_detail_op_imm(MCInst *MI, unsigned OpNum,
25812592
}
25822593
return;
25832594
}
2584-
if (map_get_op_type(MI, OpNum) & CS_OP_MEM) {
2595+
if (map_get_op_type(MI, OpNum) & CS_OP_MEM || prev_is_membase_wb(MI)) {
25852596
AArch64_set_detail_op_mem(MI, OpNum, Imm);
25862597
return;
25872598
}
@@ -2635,7 +2646,6 @@ void AArch64_set_detail_op_mem(MCInst *MI, unsigned OpNum, uint64_t Val)
26352646
if (!detail_is_set(MI))
26362647
return;
26372648
AArch64_check_safe_inc();
2638-
assert(map_get_op_type(MI, OpNum) & CS_OP_MEM);
26392649

26402650
AArch64_set_mem_access(MI, true);
26412651

@@ -2644,7 +2654,6 @@ void AArch64_set_detail_op_mem(MCInst *MI, unsigned OpNum, uint64_t Val)
26442654
default:
26452655
assert(0 && "Secondary type not supported yet.");
26462656
case CS_OP_REG: {
2647-
assert(secondary_type == CS_OP_REG);
26482657
bool is_index_reg = AArch64_get_detail_op(MI, 0)->mem.base !=
26492658
AARCH64_REG_INVALID;
26502659
if (is_index_reg)
@@ -2666,7 +2675,6 @@ void AArch64_set_detail_op_mem(MCInst *MI, unsigned OpNum, uint64_t Val)
26662675
break;
26672676
}
26682677
case CS_OP_IMM: {
2669-
assert(secondary_type == CS_OP_IMM);
26702678
AArch64_get_detail_op(MI, 0)->mem.disp = Val;
26712679
break;
26722680
}

tests/details/aarch64.yaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,7 @@ test_cases:
361361
-
362362
type: AARCH64_OP_MEM
363363
mem_base: sp
364-
access: CS_AC_READ
365-
-
366-
type: AARCH64_OP_IMM
367-
imm: 0x3c
364+
mem_disp: 0x3c
368365
access: CS_AC_READ
369366
post_indexed: 1
370367
cc: AArch64CC_Invalid

tests/issues/issues.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5411,3 +5411,30 @@ test_cases:
54115411
mem_base: x0
54125412
access: CS_AC_READ_WRITE
54135413
regs_read: [ w1, w2, x0 ]
5414+
-
5415+
input:
5416+
name: "issue ldr offset as imm: https://github.com/capstone-engine/capstone/issues/2015#issuecomment-2373660217"
5417+
bytes: [ 0x01, 0xa4, 0x40, 0xf8 ]
5418+
arch: "CS_ARCH_AARCH64"
5419+
options: [ CS_OPT_DETAIL ]
5420+
address: 0x0
5421+
expected:
5422+
insns:
5423+
-
5424+
asm_text: "ldr x1, [x0], #0xa"
5425+
details:
5426+
aarch64:
5427+
operands:
5428+
-
5429+
type: AARCH64_OP_REG
5430+
reg: x1
5431+
access: CS_AC_WRITE
5432+
-
5433+
type: AARCH64_OP_MEM
5434+
mem_base: x0
5435+
mem_disp: 0xa
5436+
access: CS_AC_READ
5437+
post_indexed: 1
5438+
writeback: 1
5439+
regs_read: [ x0 ]
5440+
regs_write: [ x0, x1 ]

0 commit comments

Comments
 (0)