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

AArch64 - wa write assembly instruction gives wrong results #14052

Closed
MatejKastak opened this issue May 13, 2019 · 4 comments
Closed

AArch64 - wa write assembly instruction gives wrong results #14052

MatejKastak opened this issue May 13, 2019 · 4 comments

Comments

@MatejKastak
Copy link
Contributor

MatejKastak commented May 13, 2019

Work environment

Questions Answers
OS/arch/bits (mandatory) Linux kali 4.14.71-v8 #1 SMP PREEMPT Wed Oct 31 21:41:06 UTC 2018 aarch64 GNU/Linux
File format of the file you reverse (mandatory) ELF
Architecture/bits of the file (mandatory) ARMv8-A AArch64
r2 -v full output, not truncated (mandatory) radare2 3.6.0-git 21712 @ linux-arm-64 git.3.5.0-9-g34f2f9f25 commit: 34f2f9f build: 2019-05-13__16:01:05

Expected behavior

I was using the wa ( wa write opcode, separated by ';' (use '"' around the command) command to write a instruction to binary that I am debugging. It seems like the assembler is giving the wrong results.

  1. Write the instruction wa add x0, x1, #3
  2. Step the debugger ds
  3. The written instruction should be add x0, x1, #3 (but its add x0, x1, #0)
  4. Print the registers drj and see x0 == 3 (given everything is zero)

I tried this also on x86 and it seems to work.

Actual behavior

For example:
wa add x0, x1, #3 -> "add x0, x1, #0"
wa neg x0, x1 -> Cannot assemble 'neg x0, x1' at line 3
wa sub x0, xzr, x1 -> "sub x0, x0, x1"

Steps to reproduce the behavior

root@kali:[insrunner]# r2 -d source
Process with PID 18235 started...
= attach 18235 18235
bin.baddr 0x00400000
Using 0x400000
asm.bits 64
 -- (gdb) ^D
[0x00400078]> dr pc = main
0x00400078 ->0x00400078
[0x00400078]> s main
[0x00400078]> wa add x0, x1, #3
Written 4 byte(s) (add x0, x1, #3) = wx 20000091
[0x00400078]> pd 1 @ pc
            ;-- main:
            ;-- entry0:
            ;-- section..text:
            ;-- .text:
            ;-- $d:
            ;-- __start:
            ;-- _start:
            ;-- pc:
            0x00400078      20000091       add x0, x1, 0               ; [01] -r-x section size 512 named .text
[0x00400078]> ds
[0x00400078]> drj
{"x0":0,"x1":0,"x2":0,"x3":0,"x4":0,"x5":0,"x6":0,"x7":0,"x8":0,"x9":0,"x10":0,"x11":0,"x12":0,"x13":0,"x14":0,"x15":0,"x16":0,"x17":0,"x18":0,"x19":0,"x20":0,"x21":0,"x22":0,"x23":0,"x24":0,"x25":0,"x26":0,"x27":0,"x28":0,"x29":0,"x30":0,"sp":549076061536,"pc":4194428,"d0":0,"d1":0,"d2":0,"d3":0,"d4":0,"d5":0,"d6":0,"d7":0,"d8":0,"d9":0,"d10":0,"d11":0,"d12":0,"d13":0,"d14":0,"d15":0,"d16":0,"d17":0,"d18":0,"d19":0,"d20":0,"d21":0,"d22":0,"d23":0,"d24":0,"d25":0,"d26":0,"d27":0,"d28":0,"d29":0,"d30":0,"dsp":549076061536,"pstate":2097152,"fstate":2097152}
[0x00400078]> 

or

root@kali:[insrunner]# r2 source
 -- What do you want to debug today?
[0x00400078]> ood
Process with PID 18256 started...
File dbg:///root/dev/insrunner/source  reopened in read-write mode
= attach 18256 18256
ptrace (PT_ATTACH): Operation not permitted
18256
[0x00400078]> dr pc = main
0x00400078 ->0x00400078
[0x00400078]> s main
[0x00400078]> wa neg x0, x1
Cannot assemble 'neg x0, x1' at line 3
[0x00400078]> wa sub x0, xzr, x1
Written 4 byte(s) (sub x0, xzr, x1) = wx 000001cb
[0x00400078]> pd 1 @ pc
            ;-- main:
            ;-- entry0:
            ;-- section..text:
            ;-- .text:
            ;-- $d:
            ;-- __start:
            ;-- _start:
            ;-- pc:
            0x00400078      000001cb       sub x0, x0, x1              ; [01] -r-x section size 512 named .text
[0x00400078]> ds
[0x00400078]> drj
{"x0":0,"x1":0,"x2":0,"x3":0,"x4":0,"x5":0,"x6":0,"x7":0,"x8":0,"x9":0,"x10":0,"x11":0,"x12":0,"x13":0,"x14":0,"x15":0,"x16":0,"x17":0,"x18":0,"x19":0,"x20":0,"x21":0,"x22":0,"x23":0,"x24":0,"x25":0,"x26":0,"x27":0,"x28":0,"x29":0,"x30":0,"sp":549705955536,"pc":4194428,"d0":0,"d1":0,"d2":0,"d3":0,"d4":0,"d5":0,"d6":0,"d7":0,"d8":0,"d9":0,"d10":0,"d11":0,"d12":0,"d13":0,"d14":0,"d15":0,"d16":0,"d17":0,"d18":0,"d19":0,"d20":0,"d21":0,"d22":0,"d23":0,"d24":0,"d25":0,"d26":0,"d27":0,"d28":0,"d29":0,"d30":0,"dsp":549705955536,"pstate":2097152,"fstate":2097152}
[0x00400078]> 

source.zip

@radare
Copy link
Collaborator

radare commented May 13, 2019 via email

@MatejKastak
Copy link
Contributor Author

Oh, thanks for quick response.

I forgot/didn't know that r2 is I guess assembling those instructions on its own. If I will have some free time I will definitely look into that.

Thanks.

@radare radare reopened this May 13, 2019
@radare
Copy link
Collaborator

radare commented May 13, 2019

r2pm -i keystone and then use e asm.assembler=arm.ks and you may be able to assemble them using keystone inside r2. But its desirable to avoid this dependency and use our own assembler to not depend on c++

@radare
Copy link
Collaborator

radare commented Sep 1, 2019

Just tested them , and they all work fine, closing

@radare radare closed this as completed Sep 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants