Skip to content

Fix ARM64 sign extension helper#380

Open
RatinCN wants to merge 1 commit into
microsoft:mainfrom
RatinCN:fix/arm64-sign-extend-import-thunks
Open

Fix ARM64 sign extension helper#380
RatinCN wants to merge 1 commit into
microsoft:mainfrom
RatinCN:fix/arm64-sign-extend-import-thunks

Conversation

@RatinCN
Copy link
Copy Markdown
Contributor

@RatinCN RatinCN commented May 27, 2026

Fixes #296. I fixed this in my SlimDetours commit fc42391, and now create this PR to Detours.

detour_sign_extend should build the sign-extension mask from the source bit width, not from the amount used to shift the sign bit into position.

Using 64 - bits places the mask at the wrong bit position and produces incorrect results for signed values whose sign bit is set. One affected path is ARM64 import thunk decoding, where negative ADRP page offsets can be decoded incorrectly.

Using the example from #296, with value = 0x0ffea2e4 and bits = 28, the sign bit is set, so the value should be sign-extended.

The current code uses m1 << left, where left = 64 - bits = 36:

0xfffffff000000000 | 0x000000000ffea2e4 = 0xfffffff00ffea2e4

This is incorrect because the mask starts at bit 36. It should start at the source width, bit 28:

0xfffffffff0000000 | 0x000000000ffea2e4 = 0xfffffffffffea2e4

Therefore the sign mask should use m1 << bits.

detour_sign_extend should build the sign-extension mask from the source bit width, not from the amount used to shift the sign bit into position.

Using 64 - bits places the mask at the wrong bit position and produces incorrect results for signed values whose sign bit is set. One affected path is ARM64 import thunk decoding, where negative ADRP page offsets can be decoded incorrectly.
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.

detour_sign_extend is implemented incorrectly

1 participant