Skip to content

AArch64 wrong register access read/write flags on "cmp" instruction #1653

@cyanpencil

Description

@cyanpencil

First of all, thank you for your work, this is an amazing library.
Unfortunately, I found a bug when I used the code from the doc page to test register read/write flags here: https://www.capstone-engine.org/op_access.html

The code outputs:

cmp     x1, x2
Registers read: x2
Registers modified: nzcv x1

which is wrong, as x1 is read, not modified.

I'll paste here the exact code I used for easier reproduction:

#include <stdio.h>

#include "include/capstone/capstone.h"

#define CODE "\x3F\x00\x02\xEB" // cmp x1, x2

int main(void)
{
  csh handle;
  cs_insn *insn;
  size_t count, j;
  cs_regs regs_read, regs_write;
  uint8_t read_count, write_count, i;
  
  if (cs_open(CS_ARCH_ARM64, CS_MODE_ARM, &handle) != CS_ERR_OK)
    return -1;
  
  cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
  
  count = cs_disasm(handle, CODE, sizeof(CODE)-1, 0x1000, 0, &insn);
  if (count > 0) {
    for (j = 0; j < count; j++) {
      // Print assembly
      printf("%s\t%s\n", insn[j].mnemonic, insn[j].op_str);

      // Print all registers accessed by this instruction.
      if (cs_regs_access(handle, &insn[j],
            regs_read, &read_count,
            regs_write, &write_count) == 0) {
        if (read_count > 0) {
          printf("\n\tRegisters read:");
          for (i = 0; i < read_count; i++) {
            printf(" %s", cs_reg_name(handle, regs_read[i]));
          }
          printf("\n");
        }

        if (write_count > 0) {
          printf("\n\tRegisters modified:");
          for (i = 0; i < write_count; i++) {
            printf(" %s", cs_reg_name(handle, regs_write[i]));
          }
          printf("\n");
        }
      }
    }

    cs_free(insn, count);
  } else
    printf("ERROR: Failed to disassemble given code!\n");

  cs_close(&handle);

  return 0;
}

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions