Skip to content

Find writeback registers#2960

Merged
Rot127 merged 1 commit into
capstone-engine:nextfrom
slate5:fix/writeback-riscv
Jun 13, 2026
Merged

Find writeback registers#2960
Rot127 merged 1 commit into
capstone-engine:nextfrom
slate5:fix/writeback-riscv

Conversation

@slate5

@slate5 slate5 commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Your checklist for this pull request

  • I've documented or updated the documentation of every API function and struct this PR changes.
  • I've added tests that prove my fix is effective or that my feature works (if possible)

Detailed description

Detection of writebacks was only partially implemented:

  • MCInst_handleWriteback() was not called, so tied_op_idx was not filled
  • map_get_op_access() in RISCV_add_cs_detail_0() queried tied_op_idx unnecessarily, because all elements were -1 (initialization value)

This uses auto-sync to find writebacks instead of having a custom function in arch/RISCV/

Test plan

...

Closing issues

...

Detection of writebacks was only partially implemented:
- MCInst_handleWriteback() was not called, so tied_op_idx was not filled
- map_get_op_access() in RISCV_add_cs_detail_0() queried tied_op_idx
  unnecessarily, because all elements were -1 (initialization value)
@github-actions github-actions Bot added the RISCV Arch label Jun 9, 2026

@Rot127 Rot127 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the instruction which let you find the bug as test.

@slate5

slate5 commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

I didn't, i could run a lot of instructions in the hope of finding a discrepancy between manually setting RW access (as it was) and relying on auto-sync.

I came across this accidentally, and i thought it's neater and more robust...

@Rot127

Rot127 commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

@moste00 Can you please take a look. Maybe there was a reason you did it like that?

@moste00

moste00 commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

This is correct, thank you @slate5 !

@Rot127 No, there was no any particular reason except that I just didn't know the writeback function existed, so I wrote a very narrow fix and moved on. I think Slate5 fix is probably better, if nothing else it net-removes code which is always a good thing.

I tested it manually on 2 instruction that were fixed up by my narrow function and it worked, producing the same READ|WRITE access.

LGTM.

@Rot127 Rot127 merged commit 3aa1511 into capstone-engine:next Jun 13, 2026
35 checks passed
@moste00

moste00 commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

@slate5 @Rot127 Just FYI, the original add_missing_write_access might have just been completely dead logic.

I just commented it in my next branch before this PR was merged, no test failure happened (I distinctly remember that I added it originally due to a test failure), and the 2 manual test cases also work just fine.

So I'm not sure what MC_handlwriteback is doing, but removing add_missing_write_access is the correct move. If MC_handlewriteback is present in other archiectures then leave it be too.

@Rot127

Rot127 commented Jun 13, 2026

Copy link
Copy Markdown
Collaborator

LLVM defines what registers are read + written. In the .td files they are in the Constraints field. It looks something like that: Constraints = "Rb = RB_wb" (wb for write back). Those constraints are of course in the instruction info tables.
And MC_handlwriteback just checks for those and sets the flags.

@slate5

slate5 commented Jun 13, 2026

Copy link
Copy Markdown
Contributor Author

@slate5 @Rot127 Just FYI, the original add_missing_write_access might have just been completely dead logic.

To your defense, I don't think it was dead logic. It did its job properly (detecting writebacks), but it relied on more hard-coded LLVM output (the first op has to be RISCV_OP_INVALID), so I don't know if that would be robust in the future :)

@slate5 slate5 deleted the fix/writeback-riscv branch June 13, 2026 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

RISCV Arch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants