Skip to content

[ARM32] Hook for "svc" always returns interrupt number 2 #2153

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

Open
kittennbfive opened this issue Apr 11, 2025 · 7 comments
Open

[ARM32] Hook for "svc" always returns interrupt number 2 #2153

kittennbfive opened this issue Apr 11, 2025 · 7 comments

Comments

@kittennbfive
Copy link

Hello,

sorry to bother you again. I think this time i found a real bug regarding ARM32 "svc" instruction (supervisor call).

I compiled current master (f8c6db9) on Linux.

Please consider the attached testcase.

Expected output:

start test
svc 0
svc 1
svc 2
svc 3
svc 4
svc 5
svc 6
test done

Real output:

start test
svc 2
svc 2
svc 2
svc 2
svc 2
svc 2
svc 2
test done

I spent a lot of time tracking this down but as i am not familiar with QEMU/Unicorn-internals i don't know the correct way to fix this.

If an svc $n instruction is encountered then https://github.com/unicorn-engine/unicorn/blob/master/qemu/target/arm/op_helper.c#L30 is somehow called with excp set to EXCP_SWI (==2, the wrong value we are always seeing, defined inside https://github.com/unicorn-engine/unicorn/blob/master/qemu/target/arm/cpu.h ). The actual svc call number is inside syndrome with some other informations (i don't understand the details).

At https://github.com/unicorn-engine/unicorn/blob/master/qemu/target/arm/op_helper.c#L49 cs->exception_index is then set to excp (which is 2), but syndrome (which contails the actual call number we are interested in) is not stored inside cs. Maybe there is the (first part of) the problem?

See also the comment at https://github.com/unicorn-engine/unicorn/blob/master/qemu/target/arm/cpu.h#L528 .

I have hacked together a patch, attached here, that works for me; but as i have basically no idea what i am doing i won't open a PR. Somebody else should look at this. No hurry, for me the bug is kinda "resolved".

Thank you.

main.c.txt
arm_svc_patch.diff.txt

@wtdcode
Copy link
Member

wtdcode commented Apr 11, 2025

Qiling workarounds this by: https://github.com/qilingframework/qiling/blob/a430518ef2026c3fa9d69df5831581b9ac3aa368/qiling/os/posix/syscall/abi/arm.py#L26

Your patch is correct at first glance. Nice catch!

@kittennbfive
Copy link
Author

Thank you. Should i open a PR?

@wtdcode
Copy link
Member

wtdcode commented Apr 11, 2025

Thank you. Should i open a PR?

Sure please do so. This also let CI check if it breaks other things.

kittennbfive added a commit to kittennbfive/unicorn that referenced this issue Apr 11, 2025
Currently svc $n always passes intno 2 to the hook, this PR should fix this.
@kittennbfive
Copy link
Author

PR is there after some false start. I am not very familiar with github...

@wtdcode
Copy link
Member

wtdcode commented Apr 12, 2025

@amaanq Gives a much better way to handle this, please refer to this example:

https://github.com/unicorn-engine/unicorn/pull/2155/files#diff-b1dab5e34001e4643d754694930562a45821b8bcc8fda0a72720c06d1c5d032fR1001

The change will live at dev branch until next release.

@kittennbfive
Copy link
Author

No problem for me, thank you!

@kittennbfive
Copy link
Author

kittennbfive commented Apr 12, 2025

Just for the record: My patch (that has been superseeded) would break unit test test_arm_m_exc_return_hook_interrupt().

This issue can be closed.

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