Skip to content

Commit

Permalink
[sanitizer-coverage/libFuzzer] instrument comparisons with __sanitize…
Browse files Browse the repository at this point in the history
…r_cov_trace_cmp[1248] instead of __sanitizer_cov_trace_cmp, don't pass the comparison type to save a bit performance. Use these new callbacks in libFuzzer

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@279027 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
kcc committed Aug 18, 2016
1 parent b5871f9 commit aa4e23e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 12 deletions.
51 changes: 48 additions & 3 deletions lib/Fuzzer/FuzzerTraceState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,8 @@ static void AddValueForStrcmp(void *caller_pc, const char *s1, const char *s2,
}

__attribute__((target("popcnt")))
static void AddValueForCmp(uintptr_t PC, uint64_t Arg1, uint64_t Arg2) {
static void AddValueForCmp(void *PCptr, uint64_t Arg1, uint64_t Arg2) {
uintptr_t PC = reinterpret_cast<uintptr_t>(PCptr);
VP.AddValue((PC & 4095) | (__builtin_popcountl(Arg1 ^ Arg2) << 12));
}

Expand All @@ -598,6 +599,21 @@ void __dfsw___sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1,
TS->DFSanCmpCallback(PC, CmpSize, Type, Arg1, Arg2, L1, L2);
}

#define DFSAN_CMP_CALLBACK(N) \
void __dfsw___sanitizer_cov_trace_cmp##N(uint64_t Arg1, uint64_t Arg2, \
dfsan_label L1, dfsan_label L2) { \
if (RecordingTraces) \
TS->DFSanCmpCallback( \
reinterpret_cast<uintptr_t>(__builtin_return_address(0)), N, \
fuzzer::ICMP_EQ, Arg1, Arg2, L1, L2); \
}

DFSAN_CMP_CALLBACK(1)
DFSAN_CMP_CALLBACK(2)
DFSAN_CMP_CALLBACK(4)
DFSAN_CMP_CALLBACK(8)
#undef DFSAN_CMP_CALLBACK

void __dfsw___sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases,
dfsan_label L1, dfsan_label L2) {
if (!RecordingTraces) return;
Expand Down Expand Up @@ -710,6 +726,7 @@ void __sanitizer_weak_hook_memmem(void *called_pc, const void *s1, size_t len1,

#endif // LLVM_FUZZER_DEFINES_SANITIZER_WEAK_HOOOKS

// TODO: this one will not be used with the newest clang. Remove it.
__attribute__((visibility("default")))
void __sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1,
uint64_t Arg2) {
Expand All @@ -720,8 +737,36 @@ void __sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1,
TS->TraceCmpCallback(PC, CmpSize, Type, Arg1, Arg2);
}
if (RecordingValueProfile)
fuzzer::AddValueForCmp(
reinterpret_cast<uintptr_t>(__builtin_return_address(0)), Arg1, Arg2);
fuzzer::AddValueForCmp(__builtin_return_address(0), Arg1, Arg2);
}

// Adding if(RecordingTraces){...} slows down the VP callbacks.
// Once we prove that VP is as strong as traces, delete this.
#define MAYBE_RECORD_TRACE(N) \
if (RecordingTraces) { \
uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); \
TS->TraceCmpCallback(PC, N, fuzzer::ICMP_EQ, Arg1, Arg2); \
}

__attribute__((visibility("default")))
void __sanitizer_cov_trace_cmp8(uint64_t Arg1, int64_t Arg2) {
fuzzer::AddValueForCmp(__builtin_return_address(0), Arg1, Arg2);
MAYBE_RECORD_TRACE(8);
}
__attribute__((visibility("default")))
void __sanitizer_cov_trace_cmp4(uint32_t Arg1, int32_t Arg2) {
fuzzer::AddValueForCmp(__builtin_return_address(0), Arg1, Arg2);
MAYBE_RECORD_TRACE(4);
}
__attribute__((visibility("default")))
void __sanitizer_cov_trace_cmp2(uint16_t Arg1, int16_t Arg2) {
fuzzer::AddValueForCmp(__builtin_return_address(0), Arg1, Arg2);
MAYBE_RECORD_TRACE(2);
}
__attribute__((visibility("default")))
void __sanitizer_cov_trace_cmp1(uint8_t Arg1, int8_t Arg2) {
fuzzer::AddValueForCmp(__builtin_return_address(0), Arg1, Arg2);
MAYBE_RECORD_TRACE(1);
}

__attribute__((visibility("default")))
Expand Down
2 changes: 2 additions & 0 deletions lib/Fuzzer/test/value-profile-cmp.test
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
CHECK: BINGO
RUN: not LLVMFuzzer-SimpleCmpTest -use_value_profile=1 -runs=100000000 2>&1 | FileCheck %s
RUN: not LLVMFuzzer-SimpleHashTest -use_value_profile=1 -runs=100000000 2>&1 | FileCheck %s


32 changes: 24 additions & 8 deletions lib/Transforms/Instrumentation/SanitizerCoverage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ static const char *const SanCovTraceEnterName =
static const char *const SanCovTraceBBName =
"__sanitizer_cov_trace_basic_block";
static const char *const SanCovTracePCName = "__sanitizer_cov_trace_pc";
static const char *const SanCovTraceCmpName = "__sanitizer_cov_trace_cmp";
static const char *const SanCovTraceCmp1 = "__sanitizer_cov_trace_cmp1";
static const char *const SanCovTraceCmp2 = "__sanitizer_cov_trace_cmp2";
static const char *const SanCovTraceCmp4 = "__sanitizer_cov_trace_cmp4";
static const char *const SanCovTraceCmp8 = "__sanitizer_cov_trace_cmp8";
static const char *const SanCovTraceSwitchName = "__sanitizer_cov_trace_switch";
static const char *const SanCovModuleCtorName = "sancov.module_ctor";
static const uint64_t SanCtorAndDtorPriority = 2;
Expand Down Expand Up @@ -188,7 +191,7 @@ class SanitizerCoverageModule : public ModulePass {
Function *SanCovWithCheckFunction;
Function *SanCovIndirCallFunction, *SanCovTracePCIndir;
Function *SanCovTraceEnter, *SanCovTraceBB, *SanCovTracePC;
Function *SanCovTraceCmpFunction;
Function *SanCovTraceCmpFunction[4];
Function *SanCovTraceSwitchFunction;
InlineAsm *EmptyAsm;
Type *IntptrTy, *Int64Ty, *Int64PtrTy;
Expand Down Expand Up @@ -227,9 +230,18 @@ bool SanitizerCoverageModule::runOnModule(Module &M) {
SanCovIndirCallFunction =
checkSanitizerInterfaceFunction(M.getOrInsertFunction(
SanCovIndirCallName, VoidTy, IntptrTy, IntptrTy, nullptr));
SanCovTraceCmpFunction =
SanCovTraceCmpFunction[0] =
checkSanitizerInterfaceFunction(M.getOrInsertFunction(
SanCovTraceCmpName, VoidTy, Int64Ty, Int64Ty, Int64Ty, nullptr));
SanCovTraceCmp1, VoidTy, IRB.getInt8Ty(), IRB.getInt8Ty(), nullptr));
SanCovTraceCmpFunction[1] = checkSanitizerInterfaceFunction(
M.getOrInsertFunction(SanCovTraceCmp2, VoidTy, IRB.getInt16Ty(),
IRB.getInt16Ty(), nullptr));
SanCovTraceCmpFunction[2] = checkSanitizerInterfaceFunction(
M.getOrInsertFunction(SanCovTraceCmp4, VoidTy, IRB.getInt32Ty(),
IRB.getInt32Ty(), nullptr));
SanCovTraceCmpFunction[3] =
checkSanitizerInterfaceFunction(M.getOrInsertFunction(
SanCovTraceCmp8, VoidTy, Int64Ty, Int64Ty, nullptr));
SanCovTraceSwitchFunction =
checkSanitizerInterfaceFunction(M.getOrInsertFunction(
SanCovTraceSwitchName, VoidTy, Int64Ty, Int64PtrTy, nullptr));
Expand Down Expand Up @@ -497,12 +509,16 @@ void SanitizerCoverageModule::InjectTraceForCmp(
if (!A0->getType()->isIntegerTy())
continue;
uint64_t TypeSize = DL->getTypeStoreSizeInBits(A0->getType());
int CallbackIdx = TypeSize == 8 ? 0 :
TypeSize == 16 ? 1 :
TypeSize == 32 ? 2 :
TypeSize == 64 ? 3 : -1;
if (CallbackIdx < 0) continue;
// __sanitizer_cov_trace_cmp((type_size << 32) | predicate, A0, A1);
auto Ty = Type::getIntNTy(*C, TypeSize);
IRB.CreateCall(
SanCovTraceCmpFunction,
{ConstantInt::get(Int64Ty, (TypeSize << 32) | ICMP->getPredicate()),
IRB.CreateIntCast(A0, Int64Ty, true),
IRB.CreateIntCast(A1, Int64Ty, true)});
SanCovTraceCmpFunction[CallbackIdx],
{IRB.CreateIntCast(A0, Ty, true), IRB.CreateIntCast(A1, Ty, true)});
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/Instrumentation/SanitizerCoverage/cmp-tracing.ll
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ target triple = "x86_64-unknown-linux-gnu"
define i32 @foo(i32 %a, i32 %b) #0 {
entry:
%cmp = icmp slt i32 %a, %b
; CHECK: call void @__sanitizer_cov_trace_cmp
; CHECK: call void @__sanitizer_cov_trace_cmp4
; CHECK-NEXT: icmp slt i32 %a, %b
%conv = zext i1 %cmp to i32
ret i32 %conv
Expand Down

0 comments on commit aa4e23e

Please sign in to comment.