2016-02-27 05:33:56 +08:00
|
|
|
//===- FuzzerTracePC.cpp - PC tracing--------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Trace PCs.
|
2016-09-14 10:13:06 +08:00
|
|
|
// This module implements __sanitizer_cov_trace_pc_guard[_init],
|
|
|
|
// the callback required for -fsanitize-coverage=trace-pc-guard instrumentation.
|
2016-02-27 05:33:56 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-10-06 06:56:21 +08:00
|
|
|
#include "FuzzerCorpus.h"
|
2016-09-22 05:17:23 +08:00
|
|
|
#include "FuzzerDefs.h"
|
2016-09-21 09:50:50 +08:00
|
|
|
#include "FuzzerTracePC.h"
|
2016-09-22 05:17:23 +08:00
|
|
|
#include "FuzzerValueBitMap.h"
|
2016-02-27 05:33:56 +08:00
|
|
|
|
|
|
|
namespace fuzzer {
|
2016-05-11 07:43:15 +08:00
|
|
|
|
2016-09-14 10:13:06 +08:00
|
|
|
TracePC TPC;
|
2016-05-11 07:43:15 +08:00
|
|
|
|
2016-09-30 01:43:24 +08:00
|
|
|
void TracePC::HandleTrace(uint32_t *Guard, uintptr_t PC) {
|
|
|
|
uint32_t Idx = *Guard;
|
2016-09-18 12:52:23 +08:00
|
|
|
if (!Idx) return;
|
2016-09-24 07:51:58 +08:00
|
|
|
uint8_t *CounterPtr = &Counters[Idx % kNumCounters];
|
|
|
|
uint8_t Counter = *CounterPtr;
|
2016-09-23 09:20:07 +08:00
|
|
|
if (Counter == 0) {
|
2016-10-11 09:14:41 +08:00
|
|
|
if (!PCs[Idx % kNumPCs]) {
|
2016-09-24 07:51:58 +08:00
|
|
|
AddNewPCID(Idx);
|
2016-09-23 09:20:07 +08:00
|
|
|
TotalPCCoverage++;
|
2016-10-11 09:14:41 +08:00
|
|
|
PCs[Idx % kNumPCs] = PC;
|
2016-09-15 12:36:45 +08:00
|
|
|
}
|
2016-09-15 09:30:18 +08:00
|
|
|
}
|
2016-09-24 07:51:58 +08:00
|
|
|
if (UseCounters) {
|
|
|
|
if (Counter < 128)
|
|
|
|
*CounterPtr = Counter + 1;
|
|
|
|
else
|
|
|
|
*Guard = 0;
|
|
|
|
} else {
|
|
|
|
*CounterPtr = 1;
|
2016-09-23 09:20:07 +08:00
|
|
|
*Guard = 0;
|
2016-09-24 07:51:58 +08:00
|
|
|
}
|
2016-02-27 05:33:56 +08:00
|
|
|
}
|
2016-09-15 09:30:18 +08:00
|
|
|
|
2016-09-30 01:43:24 +08:00
|
|
|
void TracePC::HandleInit(uint32_t *Start, uint32_t *Stop) {
|
2016-09-17 13:04:47 +08:00
|
|
|
if (Start == Stop || *Start) return;
|
|
|
|
assert(NumModules < sizeof(Modules) / sizeof(Modules[0]));
|
2016-09-30 01:43:24 +08:00
|
|
|
for (uint32_t *P = Start; P < Stop; P++)
|
2016-09-18 12:52:23 +08:00
|
|
|
*P = ++NumGuards;
|
2016-09-17 13:04:47 +08:00
|
|
|
Modules[NumModules].Start = Start;
|
|
|
|
Modules[NumModules].Stop = Stop;
|
|
|
|
NumModules++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TracePC::PrintModuleInfo() {
|
|
|
|
Printf("INFO: Loaded %zd modules (%zd guards): ", NumModules, NumGuards);
|
|
|
|
for (size_t i = 0; i < NumModules; i++)
|
|
|
|
Printf("[%p, %p), ", Modules[i].Start, Modules[i].Stop);
|
|
|
|
Printf("\n");
|
2016-09-15 09:30:18 +08:00
|
|
|
}
|
|
|
|
|
2016-09-17 14:01:55 +08:00
|
|
|
void TracePC::ResetGuards() {
|
2016-09-30 01:43:24 +08:00
|
|
|
uint32_t N = 0;
|
2016-09-17 14:01:55 +08:00
|
|
|
for (size_t M = 0; M < NumModules; M++)
|
2016-10-12 15:26:46 +08:00
|
|
|
for (uint32_t *X = Modules[M].Start; X < Modules[M].Stop; X++)
|
2016-09-18 12:52:23 +08:00
|
|
|
*X = ++N;
|
|
|
|
assert(N == NumGuards);
|
2016-09-17 14:01:55 +08:00
|
|
|
}
|
|
|
|
|
2016-10-06 06:56:21 +08:00
|
|
|
size_t TracePC::FinalizeTrace(InputCorpus *C, size_t InputSize, bool Shrink) {
|
|
|
|
if (!UsingTracePcGuard()) return 0;
|
|
|
|
size_t Res = 0;
|
|
|
|
const size_t Step = 8;
|
|
|
|
assert(reinterpret_cast<uintptr_t>(Counters) % Step == 0);
|
|
|
|
size_t N = Min(kNumCounters, NumGuards + 1);
|
|
|
|
N = (N + Step - 1) & ~(Step - 1); // Round up.
|
|
|
|
for (size_t Idx = 0; Idx < N; Idx += Step) {
|
|
|
|
uint64_t Bundle = *reinterpret_cast<uint64_t*>(&Counters[Idx]);
|
|
|
|
if (!Bundle) continue;
|
|
|
|
for (size_t i = Idx; i < Idx + Step; i++) {
|
|
|
|
uint8_t Counter = (Bundle >> (i * 8)) & 0xff;
|
|
|
|
if (!Counter) continue;
|
|
|
|
Counters[i] = 0;
|
|
|
|
unsigned Bit = 0;
|
|
|
|
/**/ if (Counter >= 128) Bit = 7;
|
|
|
|
else if (Counter >= 32) Bit = 6;
|
|
|
|
else if (Counter >= 16) Bit = 5;
|
|
|
|
else if (Counter >= 8) Bit = 4;
|
|
|
|
else if (Counter >= 4) Bit = 3;
|
|
|
|
else if (Counter >= 3) Bit = 2;
|
|
|
|
else if (Counter >= 2) Bit = 1;
|
|
|
|
size_t Feature = (i * 8 + Bit);
|
|
|
|
if (C->AddFeature(Feature, InputSize, Shrink))
|
|
|
|
Res++;
|
2016-09-15 09:30:18 +08:00
|
|
|
}
|
|
|
|
}
|
2016-10-06 06:56:21 +08:00
|
|
|
if (UseValueProfile)
|
|
|
|
ValueProfileMap.ForEach([&](size_t Idx) {
|
|
|
|
if (C->AddFeature(NumGuards + Idx, InputSize, Shrink))
|
|
|
|
Res++;
|
|
|
|
});
|
2016-10-01 09:04:29 +08:00
|
|
|
return Res;
|
2016-09-15 09:30:18 +08:00
|
|
|
}
|
|
|
|
|
2016-09-16 06:16:15 +08:00
|
|
|
void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) {
|
|
|
|
const uintptr_t kBits = 12;
|
|
|
|
const uintptr_t kMask = (1 << kBits) - 1;
|
2016-10-06 06:56:21 +08:00
|
|
|
uintptr_t Idx = (Caller & kMask) | ((Callee & kMask) << kBits);
|
|
|
|
HandleValueProfile(Idx);
|
2016-09-16 06:16:15 +08:00
|
|
|
}
|
|
|
|
|
2016-09-19 05:47:08 +08:00
|
|
|
void TracePC::PrintCoverage() {
|
|
|
|
Printf("COVERAGE:\n");
|
2016-09-27 08:10:20 +08:00
|
|
|
for (size_t i = 0; i < Min(NumGuards + 1, kNumPCs); i++) {
|
2016-09-19 05:47:08 +08:00
|
|
|
if (PCs[i])
|
|
|
|
PrintPC("COVERED: %p %F %L\n", "COVERED: %p\n", PCs[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-05 09:09:40 +08:00
|
|
|
// Value profile.
|
|
|
|
// We keep track of various values that affect control flow.
|
|
|
|
// These values are inserted into a bit-set-based hash map.
|
|
|
|
// Every new bit in the map is treated as a new coverage.
|
|
|
|
//
|
|
|
|
// For memcmp/strcmp/etc the interesting value is the length of the common
|
|
|
|
// prefix of the parameters.
|
|
|
|
// For cmp instructions the interesting value is a XOR of the parameters.
|
|
|
|
// The interesting value is mixed up with the PC and is then added to the map.
|
|
|
|
|
|
|
|
void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
|
|
|
|
size_t n) {
|
|
|
|
if (!n) return;
|
|
|
|
size_t Len = std::min(n, (size_t)32);
|
|
|
|
const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
|
|
|
|
const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
|
|
|
|
size_t I = 0;
|
|
|
|
for (; I < Len; I++)
|
|
|
|
if (A1[I] != A2[I])
|
|
|
|
break;
|
|
|
|
size_t PC = reinterpret_cast<size_t>(caller_pc);
|
|
|
|
size_t Idx = I;
|
|
|
|
// if (I < Len)
|
|
|
|
// Idx += __builtin_popcountl((A1[I] ^ A2[I])) - 1;
|
|
|
|
TPC.HandleValueProfile((PC & 4095) | (Idx << 12));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TracePC::AddValueForStrcmp(void *caller_pc, const char *s1, const char *s2,
|
|
|
|
size_t n) {
|
|
|
|
if (!n) return;
|
|
|
|
size_t Len = std::min(n, (size_t)32);
|
|
|
|
const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
|
|
|
|
const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
|
|
|
|
size_t I = 0;
|
|
|
|
for (; I < Len; I++)
|
|
|
|
if (A1[I] != A2[I] || A1[I] == 0)
|
|
|
|
break;
|
|
|
|
size_t PC = reinterpret_cast<size_t>(caller_pc);
|
|
|
|
size_t Idx = I;
|
|
|
|
// if (I < Len && A1[I])
|
|
|
|
// Idx += __builtin_popcountl((A1[I] ^ A2[I])) - 1;
|
|
|
|
TPC.HandleValueProfile((PC & 4095) | (Idx << 12));
|
|
|
|
}
|
|
|
|
|
|
|
|
ATTRIBUTE_TARGET_POPCNT
|
2016-10-12 15:26:46 +08:00
|
|
|
static void AddValueForCmp(void *PCptr, uint64_t Arg1, uint64_t Arg2) {
|
|
|
|
uintptr_t PC = reinterpret_cast<uintptr_t>(PCptr);
|
2016-10-06 06:56:21 +08:00
|
|
|
uint64_t ArgDistance = __builtin_popcountl(Arg1 ^ Arg2) + 1; // [1,65]
|
2016-10-12 15:26:46 +08:00
|
|
|
uintptr_t Idx = ((PC & 4095) + 1) * ArgDistance;
|
2016-10-05 09:09:40 +08:00
|
|
|
TPC.HandleValueProfile(Idx);
|
|
|
|
}
|
|
|
|
|
2016-10-12 15:26:46 +08:00
|
|
|
static void AddValueForSingleVal(void *PCptr, uintptr_t Val) {
|
|
|
|
if (!Val) return;
|
|
|
|
uintptr_t PC = reinterpret_cast<uintptr_t>(PCptr);
|
|
|
|
uint64_t ArgDistance = __builtin_popcountl(Val) - 1; // [0,63]
|
|
|
|
uintptr_t Idx = (PC & 4095) | (ArgDistance << 12);
|
|
|
|
TPC.HandleValueProfile(Idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-27 05:33:56 +08:00
|
|
|
} // namespace fuzzer
|
|
|
|
|
2016-06-07 04:27:09 +08:00
|
|
|
extern "C" {
|
2016-08-19 04:52:52 +08:00
|
|
|
__attribute__((visibility("default")))
|
2016-09-30 01:43:24 +08:00
|
|
|
void __sanitizer_cov_trace_pc_guard(uint32_t *Guard) {
|
2016-09-14 10:13:06 +08:00
|
|
|
uintptr_t PC = (uintptr_t)__builtin_return_address(0);
|
2016-09-15 09:30:18 +08:00
|
|
|
fuzzer::TPC.HandleTrace(Guard, PC);
|
2016-02-27 05:33:56 +08:00
|
|
|
}
|
2016-06-07 04:27:09 +08:00
|
|
|
|
2016-08-19 04:52:52 +08:00
|
|
|
__attribute__((visibility("default")))
|
2016-09-30 01:43:24 +08:00
|
|
|
void __sanitizer_cov_trace_pc_guard_init(uint32_t *Start, uint32_t *Stop) {
|
2016-09-15 09:30:18 +08:00
|
|
|
fuzzer::TPC.HandleInit(Start, Stop);
|
2016-06-07 04:27:09 +08:00
|
|
|
}
|
2016-09-16 06:16:15 +08:00
|
|
|
|
|
|
|
__attribute__((visibility("default")))
|
|
|
|
void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) {
|
|
|
|
uintptr_t PC = (uintptr_t)__builtin_return_address(0);
|
|
|
|
fuzzer::TPC.HandleCallerCallee(PC, Callee);
|
|
|
|
}
|
2016-10-05 09:09:40 +08:00
|
|
|
|
2016-10-12 15:26:46 +08:00
|
|
|
// 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) {
|
|
|
|
fuzzer::AddValueForCmp(__builtin_return_address(0), Arg1, Arg2);
|
|
|
|
}
|
|
|
|
|
2016-10-05 09:09:40 +08:00
|
|
|
__attribute__((visibility("default")))
|
2016-10-12 15:26:46 +08:00
|
|
|
void __sanitizer_cov_trace_cmp8(uint64_t Arg1, int64_t Arg2) {
|
|
|
|
fuzzer::AddValueForCmp(__builtin_return_address(0), Arg1, Arg2);
|
2016-10-05 09:09:40 +08:00
|
|
|
}
|
|
|
|
__attribute__((visibility("default")))
|
2016-10-12 15:26:46 +08:00
|
|
|
void __sanitizer_cov_trace_cmp4(uint32_t Arg1, int32_t Arg2) {
|
|
|
|
fuzzer::AddValueForCmp(__builtin_return_address(0), Arg1, Arg2);
|
2016-10-05 09:09:40 +08:00
|
|
|
}
|
|
|
|
__attribute__((visibility("default")))
|
2016-10-12 15:26:46 +08:00
|
|
|
void __sanitizer_cov_trace_cmp2(uint16_t Arg1, int16_t Arg2) {
|
|
|
|
fuzzer::AddValueForCmp(__builtin_return_address(0), Arg1, Arg2);
|
2016-10-05 09:09:40 +08:00
|
|
|
}
|
|
|
|
__attribute__((visibility("default")))
|
2016-10-12 15:26:46 +08:00
|
|
|
void __sanitizer_cov_trace_cmp1(uint8_t Arg1, int8_t Arg2) {
|
|
|
|
fuzzer::AddValueForCmp(__builtin_return_address(0), Arg1, Arg2);
|
2016-10-05 09:09:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((visibility("default")))
|
|
|
|
void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) {
|
2016-10-11 09:14:41 +08:00
|
|
|
uint64_t N = Cases[0];
|
|
|
|
uint64_t *Vals = Cases + 2;
|
|
|
|
char *PC = (char*)__builtin_return_address(0);
|
|
|
|
for (size_t i = 0; i < N; i++)
|
|
|
|
if (Val != Vals[i])
|
2016-10-12 15:26:46 +08:00
|
|
|
fuzzer::AddValueForCmp(PC + i, Val, Vals[i]);
|
2016-06-07 04:27:09 +08:00
|
|
|
}
|
2016-10-05 09:09:40 +08:00
|
|
|
|
|
|
|
__attribute__((visibility("default")))
|
|
|
|
void __sanitizer_cov_trace_div4(uint32_t Val) {
|
2016-10-12 15:26:46 +08:00
|
|
|
fuzzer::AddValueForSingleVal(__builtin_return_address(0), Val);
|
2016-10-05 09:09:40 +08:00
|
|
|
}
|
|
|
|
__attribute__((visibility("default")))
|
|
|
|
void __sanitizer_cov_trace_div8(uint64_t Val) {
|
2016-10-12 15:26:46 +08:00
|
|
|
fuzzer::AddValueForSingleVal(__builtin_return_address(0), Val);
|
2016-10-05 09:09:40 +08:00
|
|
|
}
|
|
|
|
__attribute__((visibility("default")))
|
|
|
|
void __sanitizer_cov_trace_gep(uintptr_t Idx) {
|
2016-10-12 15:26:46 +08:00
|
|
|
fuzzer::AddValueForSingleVal(__builtin_return_address(0), Idx);
|
2016-10-05 09:09:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // extern "C"
|