Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARM Thumb: disassembly for BL instruction resolves incorrect immediate value. #1935

Closed
rchtsang opened this issue Nov 12, 2022 · 12 comments · Fixed by #1949
Closed

ARM Thumb: disassembly for BL instruction resolves incorrect immediate value. #1935

rchtsang opened this issue Nov 12, 2022 · 12 comments · Fixed by #1949

Comments

@rchtsang
Copy link

capstone v4.0.2 installed from pip (Mac OSX 12.6 and Ubuntu 20.04)

It seems like the ARM Thumb BL immediate values are being incorrectly decoded.

Manually decoding the instruction b"\xff\xf7\xad\xff" ought to yield bl 0xffffff5a, however, capstone gives the following:

>>> from capstone import *
>>> from capstone.arm_const import *
>>> cs = Cs(CS_ARCH_ARM, CS_MODE_THUMB)
>>> insn = next(cs.disasm(b"\xff\xf7\xad\xff", 4))
>>> insn
<CsInsn 0x4 [fff7adff]: bl #0xffffff62>

I have tried with some other bl instructions and the immediate values are also off by 8.

@rchtsang
Copy link
Author

it also seems to affect some unconditional branch instructions, in particular
b'\xfe\xe7' ought to yield b.n 0xfffffffc (b.n -4), but instead

>>> insn = next(cs.disasm(b"\xfe\xe7", 2))
>>> insn
<CsInsn 0x2 [fee7]: b #2>

@rchtsang
Copy link
Author

rchtsang commented Nov 17, 2022

also happening with cbz/cbnz

>>> next(cs.disasm(b'\x13\xb1', 2))
<CsInsn 0x2 [13b1]: cbz r3, #0xa>

expected cbz r3, #0x4

@rchtsang
Copy link
Author

This still seems to be failing.

installed via pip install --pre --no-binary capstone capstone per #2147 installs v5.0.1

expecting bl 0xffffff5a

>>> from capstone import *
>>> from capstone.arm_const import *
>>> cs = Cs(CS_ARCH_ARM, CS_MODE_THUMB)
>>> insn = next(cs.disasm(b"\xff\xf7\xad\xff", 4))
>>> insn
<CsInsn 0x4 [fff7adff]: bl #0xffffff62>

Mac M1 Pro, MacOS 14.2.1

@Rot127
Copy link
Collaborator

Rot127 commented Feb 22, 2024

I assume this is because it uses Capstone v5. In the next branch the instruction disassembles correctly:

./cstool -d thumb "\xff\xf7\xad\xff"
 0  ff f7 ad ff  bl	0xffffff5e
	ID: 46 (bl)
	op_count: 1
		operands[0].type: IMM = 0xffffff5e
		operands[0].access: READ
	Registers read: r13
	Registers modified: r14
	Groups: call branch_relative IsThumb

In general I would recommend you to use the next branch. It supports way more instructions and is more correct.
If you can't and have to rely on v5 for now, feel free to open the issue again, we can add it to #2081

@rchtsang
Copy link
Author

Thanks for the tip, though I still seem to be getting the issue on next, but only when trying to install the python bindings.

I'm using a conda virtual environment for python 3.11 with pip installed.

$ cd bindings/python
$ sudo make install3

and that should be it?
I rebuilt libcapstone.5.dylib, but can't confirm if setup.py is using it?

I can confirm that cstool disassembles it correctly.

Am I building the bindings wrong or should I actually reopen the issue?

@Rot127
Copy link
Collaborator

Rot127 commented Feb 22, 2024

Have you ran make clean before building next? Python bindings still use the make method unfortunately.

@rchtsang
Copy link
Author

I did, yeah.

@Rot127
Copy link
Collaborator

Rot127 commented Feb 22, 2024

Yeah, tried it as well. It is something with the Python bindings. Apologies. We work on better testing. Hope I'll come to it in the next days.

@rchtsang
Copy link
Author

Got it, thank you very much for your response! Should I open a new issue re:python bindings?

@Rot127
Copy link
Collaborator

Rot127 commented Feb 22, 2024

Nah, just reopen this one, since it wasn't properly resolved. Thanks!

@rchtsang
Copy link
Author

I don't have permission to reopen...

will open a new issue and reference this one (the underlying problem is likely different this time anyways)

@Rot127
Copy link
Collaborator

Rot127 commented Feb 22, 2024

Please do that than. I don't have permission either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants