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

X64: cmpxchg16b raises UC_ERR_INSN_INVALID #1095

Closed
domenukk opened this issue Jun 17, 2019 · 4 comments
Closed

X64: cmpxchg16b raises UC_ERR_INSN_INVALID #1095

domenukk opened this issue Jun 17, 2019 · 4 comments

Comments

@domenukk
Copy link
Contributor

domenukk commented Jun 17, 2019

While emulating the Linux Kernel in x64 bit mode, I encountered crashes with cmpxchg16b.
The following testcase ("lock cmpxchg16b xmmword ptr [r12]") crashes, for example:

#!/usr/bin/python
from unicorn import *
from unicorn.x86_const import *
from capstone import *
from capstone.x86 import *

def main():
    cs = Cs(CS_ARCH_X86, CS_MODE_64) # type: capstone.Cs
    uc = Uc(UC_ARCH_X86, UC_MODE_64) # type: unicorn.Uc

    # lock cmpxchg16b xmmword ptr [r12]
    cmpxchange16b = b"\xf0\x49\x0f\xc7\x0c\x24"
    insn_pos = 0xFFFFFFFF00b9a6000

    uc.mem_map(insn_pos, 4096)
    uc.mem_write(insn_pos, cmpxchange16b)
    try:
        uc.emu_start(insn_pos, len(cmpxchange16b), count=1)
    except Exception as ex:
        print("Error {}: ".format(ex), *(list(cs.disasm_lite(bytes(uc.mem_read(uc.reg_read(UC_X86_REG_RIP), 10)), 0))[0]))

    return 0

if __name__ == "__main__":
    main()
@domenukk
Copy link
Contributor Author

Stepping through the latest version of unicorn, I can confirm the

void helper_cmpxchg16b(CPUX86State *env, target_ulong a0)

function is emitted and called during execution.
To not hit any segfaults, I had to add a random jmp to the testcase from above:

cmpxchange16b = b"\xf0\x49\x0f\xc7\x0c\x24" + b"\xff\x25\x00\x00\x00\x00"

Still investigating why I see/(saw?) illegal instruction in the first place and if it might still be an issue.

@aquynh
Copy link
Member

aquynh commented Jul 13, 2019 via email

@domenukk
Copy link
Contributor Author

There were quite some changes for this opcode... My understanding of the qemu/unicorn loop is not deep enough to understand if this might fix a bug or just reordered things/introduced new functionality.
I opened a p/r with a backport to play around with

@domenukk
Copy link
Contributor Author

Even without my PR this seems to be fixed for master.
Closing :)

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

No branches or pull requests

2 participants