Skip to content

Commit 9c2a220

Browse files
committedJul 12, 2016
[compiler-rt] Enhance function padding detection for function interception
Summary: Many CRT (64-bits) functions contains a "hint-nop". The current padding detection is not able to recognize the 10-bytes padding and the HotPatch hooking technique cannot be used. Other patterns may be discover and may be added later. Reviewers: rnk Subscribers: llvm-commits, wang0109, chrisha Differential Revision: http://reviews.llvm.org/D22258 llvm-svn: 275180
1 parent 2d84ec6 commit 9c2a220

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed
 

‎compiler-rt/lib/interception/interception_win.cc

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,29 @@ static bool IsMemoryPadding(uptr address, uptr size) {
202202
return true;
203203
}
204204

205+
static const u8 kHintNop10Bytes[] = {
206+
0x66, 0x66, 0x0F, 0x1F, 0x84,
207+
0x00, 0x00, 0x00, 0x00, 0x00
208+
};
209+
210+
template<class T>
211+
static bool FunctionHasPrefix(uptr address, const T &pattern) {
212+
u8* function = (u8*)address - sizeof(pattern);
213+
for (size_t i = 0; i < sizeof(pattern); ++i)
214+
if (function[i] != pattern[i])
215+
return false;
216+
return true;
217+
}
218+
219+
static bool FunctionHasPadding(uptr address, uptr size) {
220+
if (IsMemoryPadding(address - size, size))
221+
return true;
222+
if (size <= sizeof(kHintNop10Bytes) &&
223+
FunctionHasPrefix(address, kHintNop10Bytes))
224+
return true;
225+
return false;
226+
}
227+
205228
static void WritePadding(uptr from, uptr size) {
206229
_memset((void*)from, 0xCC, (size_t)size);
207230
}
@@ -617,7 +640,7 @@ bool OverrideFunctionWithHotPatch(
617640
// Validate that the function is hot patchable.
618641
size_t instruction_size = GetInstructionSize(old_func);
619642
if (instruction_size < kShortJumpInstructionLength ||
620-
!IsMemoryPadding(header, kHotPatchHeaderLen))
643+
!FunctionHasPadding(old_func, kHotPatchHeaderLen))
621644
return false;
622645

623646
if (orig_old_func) {

0 commit comments

Comments
 (0)
Please sign in to comment.