Skip to content

Fixing stale code tlb after fetch-prot hook - #2365

Open
bwhitn wants to merge 1 commit into
unicorn-engine:devfrom
bwhitn:fix-fetch-prot-tlb-refill
Open

Fixing stale code tlb after fetch-prot hook#2365
bwhitn wants to merge 1 commit into
unicorn-engine:devfrom
bwhitn:fix-fetch-prot-tlb-refill

Conversation

@bwhitn

@bwhitn bwhitn commented Jul 14, 2026

Copy link
Copy Markdown

Fixes a stale code TLB path after a handled UC_HOOK_MEM_FETCH_PROT callback.

When a fetch-protection callback returns true, Unicorn treats the access as handled and continues translation. The read/write protected paths already reload TLB state after a handled callback, but the fetch-protection path did not. If the callback changes permissions, for example:

uc_mem_protect(uc, page, size, UC_PROT_ALL);
return true;

the translator can continue using stale fetch TLB state.

This patch re-checks that the page is still mapped and now executable after a handled fetch-protection callback, then refills the code TLB entry before instruction bytes are read.

I believe this fixes #1780 and is related to #1393.

Added regression test.

@PhilippTakacs

Copy link
Copy Markdown
Contributor

nice catch. I haven't run the test yet, but in general it looks good.

Can you change the target branche to dev?

@bwhitn
bwhitn changed the base branch from master to dev July 15, 2026 06:29
@bwhitn

bwhitn commented Jul 15, 2026

Copy link
Copy Markdown
Author

Done

@PhilippTakacs

Copy link
Copy Markdown
Contributor

Can you also rebase your branch to dev and remove the 3 unrelated commits? If you have problems you can also just create a new PR and cherry-pick your commit.

@bwhitn
bwhitn force-pushed the fix-fetch-prot-tlb-refill branch from eb8f246 to 4c1f7b9 Compare July 15, 2026 07:05
@bwhitn

bwhitn commented Jul 15, 2026

Copy link
Copy Markdown
Author

Sorry about that, I think it is good now.

@PhilippTakacs

Copy link
Copy Markdown
Contributor

LGTM

@wtdcode

wtdcode commented Jul 15, 2026

Copy link
Copy Markdown
Member

Looks good.

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.

Incorect behaviour while using memory protection UC_PROT_ALL in UC_MEM_FETCH_PROT hook

3 participants