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

Adjust Qiling to comply with latest changed in Unicorn 2.1.2 #1499

Open
wants to merge 36 commits into
base: dev
Choose a base branch
from

Conversation

elicn
Copy link
Member

@elicn elicn commented Oct 10, 2024

[Merge only after Unicorn 2.1.2 is officially released]

Highlights:

  • Introduced co-processor register accessors to AArch32 and AArch64 (ql.arch.cpr)
  • Replaced deprecated pseudo registers with cpr definitions to be used with the new accessors
  • Separated x86 64-bit only registers from the rest as Unicorn became less tolerant to accessing unsupported registers
  • SSE instructions are no longer supported on the default Intel CPU model. x86 tests that use SSE instructions are now set to use Intel Haswell CPU model to make sure SSE is supported
  • Various opportunistic styling and annotation fixes

Fixes: #1495, #1329

@elicn elicn requested review from xwings and wtdcode October 10, 2024 15:46
@wtdcode
Copy link
Member

wtdcode commented Oct 10, 2024

The next release will be 2.1.2 and it seems only Separated x86 64-bit only registers from the rest as Unicorn became less tolerant to accessing unsupported registers needs 2.2?

@elicn
Copy link
Member Author

elicn commented Oct 10, 2024

Ah, no, that's probably my mistake. I'll fix the title accordingly.

@elicn elicn changed the title Adjust Qiling to comply with latest changed in Unicorn 2.2 Adjust Qiling to comply with latest changed in Unicorn 2.1.2 Oct 10, 2024
@elicn elicn linked an issue Oct 10, 2024 that may be closed by this pull request
@wtdcode
Copy link
Member

wtdcode commented Feb 13, 2025

2.1.2 is alive. I will give this PR a shot recently.

@xwings
Copy link
Member

xwings commented Feb 16, 2025

@elicn looks like we need few more fix ?

@elicn
Copy link
Member Author

elicn commented Feb 16, 2025

@xwings, this is mostly because Qiling is still set to use the older Unicorn release.
We can start by setting Qiling to use the most recent one, and then see if it fails. From a quick smoke test it looks like there are a few additional fixes to be made.

@wtdcode
Copy link
Member

wtdcode commented Feb 17, 2025

The mips breakage (testing test_mips32eb_fake_urandom) breaks with 2.0.0, 2.0.1, 2.1.0, 2.1.1, 2.1.2 with:

[=] 	brk(inp = 0x0) = 0x56568000
[=] 	mmap2(addr = 0x0, length = 0x2000, prot = 0x3, flags = 0x802, fd = 0xffffffff, pgoffset = 0x0) = 0x1000000
[=] 	uname(buf = 0x7ff3c674) = 0x0
[=] 	access(path = 0x47dcb04, mode = 0x4) = -0x2 (ENOENT)
E
======================================================================
ERROR: test_mips32eb_fake_urandom (__main__.ELFTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/mio/opensource/qiling/tests/test_elf.py", line 462, in test_mips32eb_fake_urandom
    ql.run()
  File "/home/mio/opensource/qiling/qiling/core.py", line 588, in run
    self.os.run()
  File "/home/mio/opensource/qiling/qiling/os/linux/linux.py", line 178, in run
    self.ql.emu_start(self.ql.loader.entry_point, entry_address, self.ql.timeout)
  File "/home/mio/opensource/qiling/qiling/core.py", line 768, in emu_start
    raise self.internal_exception
  File "/home/mio/opensource/qiling/qiling/core_hooks.py", line 141, in wrapper
    return callback(*args, **kwargs)
  File "/home/mio/opensource/qiling/qiling/core_hooks.py", line 190, in _hook_intr_cb
    raise QlErrorCoreHook("_hook_intr_cb : not handled")
qiling.exception.QlErrorCoreHook: _hook_intr_cb : not handled

Is it possible that the breakage was introduced in the Qiling side?

@wtdcode
Copy link
Member

wtdcode commented Feb 17, 2025

Additional context:

2.0.0 2.0.1 2.1.0 2.1.2
dev y y UC_ERR_ARG UC_ERR_WRITE_UNMAPPED
uc2.2-adjustments _hook_intr_cb : not handled _hook_intr_cb : not handled _hook_intr_cb : not handled UC_ERR_WRITE_UNALIGNED

I think this suggests a breakage on Qiling side? Note UC_ERR_WRITE_UNMAPPED seems from the lack of correct regiser access.

@wtdcode
Copy link
Member

wtdcode commented Feb 17, 2025

md = Cs(CS_ARCH_MIPS, CS_MODE_32 | CS_MODE_BIG_ENDIAN)
def instruction_count(ql: Qiling, address: int, size: int):
    a0 = ql.arch.regs.a0
    code = ql.mem.read(address, size)
    insn = list(md.disasm(code, size))[0]
    print(f"address={address:x} a0={a0:x} code={insn.mnemonic}")
    if insn.mnemonic == "sdc1":
        pc = ql.arch.regs.arch_pc
        pc += size
        ql.arch.regs.arch_pc = pc

This hooks fixes mips tests, no idea why ld doesn't respect stack alignment.

@wtdcode
Copy link
Member

wtdcode commented Feb 18, 2025

The left breakages:

x86 multithread: Bug of Unicorn side, fixed on master already
Android: works well dev branch, maybe check history again
mips32eb: The error is different between dev and this branch. The error message of this branch looks like: ../examples/rootfs/mips32_linux/bin/mips32_multithreading error while loading shared libraries /lib/libc.so.6 unexpected reloc type 0x03 while it gets UC_ERR_WRITE_UNMAPPED on dev branch. According to this, the binary itself is statically linked but Qiling tries to execute it via ld. Is that the reason?

@elicn
Copy link
Member Author

elicn commented Feb 18, 2025

Everything should be OK now, depending on the fixes applied in Unicorn 2.1.3.

@wtdcode
Copy link
Member

wtdcode commented Feb 19, 2025

Local test shows test_onlinux.sh passes with unicorn-engine/unicorn@56ba347 except m0hamed_rootkit.ko which I don't see the link to download.

Note this include the fix to MIPS delay slot so no longer need to tune the count of emulation. =)

Hooray!

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 this pull request may close these issues.

3 participants