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

detour_sign_extend is implemented incorrectly #296

Open
valco1994 opened this issue Jul 21, 2023 · 0 comments
Open

detour_sign_extend is implemented incorrectly #296

valco1994 opened this issue Jul 21, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@valco1994
Copy link

Currently, detour_sign_extend has the following implementation:

inline INT64 detour_sign_extend(UINT64 value, UINT bits)
{
    const UINT left = 64 - bits;
    const INT64 m1 = -1;
    const INT64 wide = (INT64)(value << left);
    const INT64 sign = (wide < 0) ? (m1 << left) : 0;
    return value | sign;
}

But the sign computation is incorrect here. The result should be the following: left upper bits of sign and then bits lower bits of value. But m1 is shifted left by left bits in the current implementation, so we will use bits bits of the sign. It's an error.

The proper sign computation should be

const INT64 sign = (wide < 0) ? (m1 << bits) : 0;

As an example, you can consider the following case that I met:

  • value is 0xffea2e4, bits is 28.
  • The expected result is 0xfffffffffffea2e4: 64 - 28 = 36 upper bits of the sign and then 28 bits of the value.
  • The actual result of the current implementation is 0xfffffff00ffea2e4 , that is obviously wrong.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant