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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "FuzzerInternal.h"
|
|
|
|
|
|
|
|
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-17 13:04:47 +08:00
|
|
|
void TracePC::HandleTrace(uint64_t *Guard, uintptr_t PC) {
|
|
|
|
const uint64_t kBit63 = 1ULL << 63;
|
|
|
|
uint64_t Value = *Guard;
|
|
|
|
if (Value & kBit63) return;
|
|
|
|
// Printf(" >> %16zx %p\n", Value, Guard);
|
2016-09-15 09:30:18 +08:00
|
|
|
if (UseCounters) {
|
2016-09-17 13:04:47 +08:00
|
|
|
uint64_t Counter = Value & 0xff;
|
|
|
|
if (Counter == 0) {
|
|
|
|
size_t Idx = Value >> 32;
|
2016-09-15 12:36:45 +08:00
|
|
|
if (TotalCoverageMap.AddValue(Idx)) {
|
|
|
|
TotalCoverage++;
|
|
|
|
AddNewPC(PC);
|
|
|
|
}
|
|
|
|
}
|
2016-09-17 13:04:47 +08:00
|
|
|
if (Counter < 255)
|
|
|
|
Value++;
|
2016-09-17 14:01:55 +08:00
|
|
|
else
|
|
|
|
Value |= kBit63;
|
2016-09-15 09:30:18 +08:00
|
|
|
} else {
|
2016-09-17 13:04:47 +08:00
|
|
|
Value |= kBit63;
|
2016-09-15 09:30:18 +08:00
|
|
|
TotalCoverage++;
|
2016-09-15 12:36:45 +08:00
|
|
|
AddNewPC(PC);
|
2016-09-15 09:30:18 +08:00
|
|
|
}
|
2016-09-17 13:04:47 +08:00
|
|
|
// Printf(" << %16zx\n", Value);
|
|
|
|
*Guard = Value;
|
2016-02-27 05:33:56 +08:00
|
|
|
}
|
2016-09-15 09:30:18 +08:00
|
|
|
|
2016-09-17 13:04:47 +08:00
|
|
|
void TracePC::HandleInit(uint64_t *Start, uint64_t *Stop) {
|
|
|
|
if (Start == Stop || *Start) return;
|
|
|
|
assert(NumModules < sizeof(Modules) / sizeof(Modules[0]));
|
|
|
|
for (uint64_t *P = Start; P < Stop; P++)
|
|
|
|
*P = (++NumGuards) << 32;
|
|
|
|
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() {
|
|
|
|
for (size_t M = 0; M < NumModules; M++)
|
|
|
|
for (uint64_t *X = Modules[M].Start; X < Modules[M].Stop; X++)
|
|
|
|
*X = (*X >> 32) << 32;
|
|
|
|
}
|
|
|
|
|
2016-09-15 09:30:18 +08:00
|
|
|
void TracePC::FinalizeTrace() {
|
|
|
|
if (UseCounters && TotalCoverage) {
|
2016-09-17 13:04:47 +08:00
|
|
|
for (size_t M = 0; M < NumModules; M++) {
|
|
|
|
for (uint64_t *X = Modules[M].Start; X < Modules[M].Stop; X++) {
|
|
|
|
uint64_t Value = *X & 0xff;
|
|
|
|
uint64_t Idx = *X >> 32;
|
|
|
|
if (Value >= 1) {
|
|
|
|
unsigned Bit = 0;
|
|
|
|
/**/ if (Value >= 128) Bit = 7;
|
|
|
|
else if (Value >= 32) Bit = 6;
|
|
|
|
else if (Value >= 16) Bit = 5;
|
|
|
|
else if (Value >= 8) Bit = 4;
|
|
|
|
else if (Value >= 4) Bit = 3;
|
|
|
|
else if (Value >= 3) Bit = 2;
|
|
|
|
else if (Value >= 2) Bit = 1;
|
|
|
|
CounterMap.AddValue(Idx * 8 + Bit);
|
|
|
|
}
|
|
|
|
*X = Idx << 32;
|
2016-09-15 09:30:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t TracePC::UpdateCounterMap(ValueBitMap *Map) {
|
|
|
|
if (!TotalCoverage) return 0;
|
|
|
|
size_t NewTotalCounterBits = Map->MergeFrom(CounterMap);
|
|
|
|
size_t Delta = NewTotalCounterBits - TotalCounterBits;
|
|
|
|
TotalCounterBits = NewTotalCounterBits;
|
|
|
|
return Delta;
|
2016-02-27 05:33:56 +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;
|
|
|
|
CounterMap.AddValue((Caller & kMask) | ((Callee & kMask) << kBits));
|
|
|
|
}
|
|
|
|
|
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-17 13:04:47 +08:00
|
|
|
void __sanitizer_cov_trace_pc_guard(uint64_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-17 13:04:47 +08:00
|
|
|
void __sanitizer_cov_trace_pc_guard_init(uint64_t *Start, uint64_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-06-07 04:27:09 +08:00
|
|
|
}
|