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

Handle pathological cases more consistently #1651

Closed
wants to merge 6 commits into from

Conversation

rhelmot
Copy link
Contributor

@rhelmot rhelmot commented Jul 13, 2022

These are two changes I've needed to make to make angr work with unicorn 2.

First commit: presently, if we try to execute a non-executable region, the lifting code never checks to see if reads succeed, it just blindly trusts the result. As a consequence, we may execute some non-executable guest code or hooks before checking cpu->exit_request and breaking out of the loop. This fixes that.

Second commit: If you respond to an unmapped memory fault during instruction fetch by mapping the related memory in a callback, qemu will only attempt to lift a single instruction for the current block. This is incredibly annoying (and breaks angr's tests, which are sensitive to block counts and boundaries), so I fixed it.

@rhelmot
Copy link
Contributor Author

rhelmot commented Jul 13, 2022

I have added a third commit which should address my concerns on #1627. This uses the uc->size_recur_mem field, which from what I can tell was completely unused prior to this, to detect whether we are in an unaligned write (which is implementing by a recursive call into the write helper).

@wtdcode
Copy link
Member

wtdcode commented Jul 14, 2022

I have added a third commit which should address my concerns on #1627. This uses the uc->size_recur_mem field, which from what I can tell was completely unused prior to this, to detect whether we are in an unaligned write (which is implementing by a recursive call into the write helper).

Regarding #1627, could you add a unit test? I assume it should be easy to reproduce?

@rhelmot
Copy link
Contributor Author

rhelmot commented Jul 14, 2022

Pushed!

@rhelmot
Copy link
Contributor Author

rhelmot commented Jul 14, 2022

Idk what's up with the failing test - it passed on my previous commit and I only changed an unrelated testcase. Is it flaky?

@wtdcode
Copy link
Member

wtdcode commented Jul 14, 2022

Idk what's up with the failing test - it passed on my previous commit and I only changed an unrelated testcase. Is it flaky?

mingw32 runner has some bugs for a while… I really should disable it. Anyway let me rerun and it will be fine.

cflags &= ~CF_COUNT_MASK;
cflags |= CF_NOCACHE | 1;
/* Generate a temporary TB; do not cache */
cflags |= CF_NOCACHE;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried your patch but I failed to understand the change here. Any reproduction code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you asking for a testcase?

The meaning of the original code is to set the count bitfield of the cflags to 1 (only lift 1 instruction) and set the CF_NOCACHE bit (do not cache the tb). The new code performs only the latter change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you asking for a testcase?

The meaning of the original code is to set the count bitfield of the cflags to 1 (only lift 1 instruction) and set the CF_NOCACHE bit (do not cache the tb). The new code performs only the latter change.

It's better if a testcase could be provided so that I can understand it better. My question is that, why setting the count to 1 solves the problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're misunderstanding the order of the diff - the old code set the count to 1, and this was undesired behavior because then it would only lift one instruction. Presumably the old code optimizes guests using exceptional control flow, but it produces inaccurate metrics (block counts).

@firodj
Copy link

firodj commented Jul 18, 2022

thanks @wtdcode for directing to this. I try this patch for issue #1041, this patch fixes the write hook, but not for the read hook.

@rhelmot
Copy link
Contributor Author

rhelmot commented Aug 2, 2022

@firodj please try the commit that I just pushed!

@wtdcode I believe this is ready to merge.

@rhelmot
Copy link
Contributor Author

rhelmot commented Aug 2, 2022

It seems that CI is failing on aarch64 and ppc64le platforms. I don't have either of those machines to debug this. How can I proceed?

@wtdcode
Copy link
Member

wtdcode commented Aug 7, 2022

It seems that CI is failing on aarch64 and ppc64le platforms. I don't have either of those machines to debug this. How can I proceed?

Weird, I can't come up with why your code will fail on these platforms. Maybe any undefined behaviors? Let me re-run it firstly.

@wtdcode
Copy link
Member

wtdcode commented Aug 14, 2022

Merged and fixed in dev branch. Thanks!

@wtdcode wtdcode closed this Aug 14, 2022
@wtdcode
Copy link
Member

wtdcode commented Aug 14, 2022

When performing memory read operation on aarch64 and ppc64, no load_helper is generated...

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.

None yet

3 participants