diff --git a/compiler-rt/lib/interception/interception_win.cpp b/compiler-rt/lib/interception/interception_win.cpp --- a/compiler-rt/lib/interception/interception_win.cpp +++ b/compiler-rt/lib/interception/interception_win.cpp @@ -738,7 +738,7 @@ return false; if (orig_old_func) { - uptr relative_offset = *(u32*)(old_func + 1); + ptrdiff_t relative_offset = *(i32*)(old_func + 1); uptr absolute_target = old_func + relative_offset + kJumpInstructionLength; *orig_old_func = absolute_target; } diff --git a/compiler-rt/lib/interception/tests/interception_win_test.cpp b/compiler-rt/lib/interception/tests/interception_win_test.cpp --- a/compiler-rt/lib/interception/tests/interception_win_test.cpp +++ b/compiler-rt/lib/interception/tests/interception_win_test.cpp @@ -85,6 +85,15 @@ 0xC3, // ret }; +const u8 kIdentityCodeWithJumpBackwards[] = { + 0x89, 0xC8, // mov eax, ecx + 0xC3, // ret + 0xE9, 0xF9, 0xFF, 0xFF, + 0xFF, // jmp - 7 + 0xCC, 0xCC, 0xCC, 0xCC, +}; +const u8 kIdentityCodeWithJumpBackwardsOffset = 3; + #else const u8 kIdentityCodeWithPrologue[] = { @@ -134,6 +143,15 @@ 0xC3, // ret }; +const u8 kIdentityCodeWithJumpBackwards[] = { + 0x8B, 0x44, 0x24, 0x04, // mov eax,dword ptr [esp + 4] + 0xC3, // ret + 0xE9, 0xF7, 0xFF, 0xFF, + 0xFF, // jmp - 9 + 0xCC, 0xCC, 0xCC, 0xCC, +}; +const u8 kIdentityCodeWithJumpBackwardsOffset = 5; + #endif const u8 kPatchableCode1[] = { @@ -370,10 +388,11 @@ static void TestIdentityFunctionPatching( const T &code, TestOverrideFunction override, - FunctionPrefixKind prefix_kind = FunctionPrefixNone) { + FunctionPrefixKind prefix_kind = FunctionPrefixNone, + int function_start_offset = 0) { uptr identity_address; LoadActiveCode(code, &identity_address, prefix_kind); - IdentityFunction identity = (IdentityFunction)identity_address; + IdentityFunction identity = (IdentityFunction)(identity_address + function_start_offset); // Validate behavior before dynamic patching. InterceptorFunctionCalled = 0; @@ -424,6 +443,10 @@ TEST(Interception, OverrideFunctionWithRedirectJump) { TestOverrideFunction override = OverrideFunctionWithRedirectJump; TestIdentityFunctionPatching(kIdentityCodeWithJump, override); + TestIdentityFunctionPatching(kIdentityCodeWithJumpBackwards, + override, + FunctionPrefixNode, + kIdentityCodeWithJumpBackwardsOffset); } TEST(Interception, OverrideFunctionWithHotPatch) {