Skip to content

Commit bc34a83

Browse files
authored
[ASan][Windows] Fix rip-relative instruction replacement (#68432)
The old code incorrectly checked what relative offsets were allowed. The correct check is that the offset from the target to the instruction pointer should be within $[-2^{31}, 2^{31})$; however, the check that was originally written was that the offset was within $[0, 2^{31})$. Negative offsets are certainly allowable (as long as they fit in 32 bits), and this change fixes that.
1 parent ab6334d commit bc34a83

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

compiler-rt/lib/interception/interception_win.cpp

+13-7
Original file line numberDiff line numberDiff line change
@@ -726,16 +726,22 @@ static bool CopyInstructions(uptr to, uptr from, size_t size) {
726726
size_t instruction_size = GetInstructionSize(from + cursor, &rel_offset);
727727
if (!instruction_size)
728728
return false;
729-
_memcpy((void*)(to + cursor), (void*)(from + cursor),
729+
_memcpy((void *)(to + cursor), (void *)(from + cursor),
730730
(size_t)instruction_size);
731731
if (rel_offset) {
732-
uptr delta = to - from;
733-
uptr relocated_offset = *(u32*)(to + cursor + rel_offset) - delta;
734-
#if SANITIZER_WINDOWS64
735-
if (relocated_offset + 0x80000000U >= 0xFFFFFFFFU)
732+
# if SANITIZER_WINDOWS64
733+
// we want to make sure that the new relative offset still fits in 32-bits
734+
// this will be untrue if relocated_offset \notin [-2**31, 2**31)
735+
s64 delta = to - from;
736+
s64 relocated_offset = *(s32 *)(to + cursor + rel_offset) - delta;
737+
if (-0x8000'0000ll > relocated_offset || relocated_offset > 0x7FFF'FFFFll)
736738
return false;
737-
#endif
738-
*(u32*)(to + cursor + rel_offset) = relocated_offset;
739+
# else
740+
// on 32-bit, the relative offset will always be correct
741+
s32 delta = to - from;
742+
s32 relocated_offset = *(s32 *)(to + cursor + rel_offset) - delta;
743+
# endif
744+
*(s32 *)(to + cursor + rel_offset) = relocated_offset;
739745
}
740746
cursor += instruction_size;
741747
}

0 commit comments

Comments
 (0)