2015-05-11 21:16:27 +00:00
|
|
|
//===- FuzzerTraceState.cpp - Trace-based fuzzer mutator ------------------===//
|
2015-03-30 22:09:51 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2016-10-04 06:08:46 +00:00
|
|
|
// Data tracing.
|
2015-03-30 22:09:51 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-09-21 01:50:50 +00:00
|
|
|
#include "FuzzerDictionary.h"
|
2016-11-30 19:06:14 +00:00
|
|
|
#include "FuzzerInternal.h"
|
|
|
|
#include "FuzzerIO.h"
|
2016-09-21 01:50:50 +00:00
|
|
|
#include "FuzzerMutate.h"
|
|
|
|
#include "FuzzerRandom.h"
|
2016-09-23 00:46:18 +00:00
|
|
|
#include "FuzzerTracePC.h"
|
2015-05-07 21:02:11 +00:00
|
|
|
#include <algorithm>
|
2015-03-30 22:09:51 +00:00
|
|
|
#include <cstring>
|
2016-01-12 02:08:37 +00:00
|
|
|
#include <map>
|
2016-07-15 23:27:19 +00:00
|
|
|
#include <set>
|
2016-11-30 19:06:14 +00:00
|
|
|
#include <thread>
|
2015-03-30 22:09:51 +00:00
|
|
|
|
2015-05-11 20:51:19 +00:00
|
|
|
namespace fuzzer {
|
|
|
|
|
2015-05-07 21:02:11 +00:00
|
|
|
// For now, very simple: put Size bytes of Data at position Pos.
|
|
|
|
struct TraceBasedMutation {
|
2016-01-16 03:53:32 +00:00
|
|
|
uint32_t Pos;
|
|
|
|
Word W;
|
2015-05-07 21:02:11 +00:00
|
|
|
};
|
|
|
|
|
2016-01-15 06:24:05 +00:00
|
|
|
// Declared as static globals for faster checks inside the hooks.
|
|
|
|
static bool RecordingMemcmp = false;
|
2016-07-15 23:27:19 +00:00
|
|
|
static bool RecordingMemmem = false;
|
2016-07-19 18:29:06 +00:00
|
|
|
static bool DoingMyOwnMemmem = false;
|
|
|
|
|
2016-10-14 20:20:33 +00:00
|
|
|
ScopedDoingMyOwnMemmem::ScopedDoingMyOwnMemmem() { DoingMyOwnMemmem = true; }
|
|
|
|
ScopedDoingMyOwnMemmem::~ScopedDoingMyOwnMemmem() { DoingMyOwnMemmem = false; }
|
2016-01-15 06:24:05 +00:00
|
|
|
|
2015-05-11 21:16:27 +00:00
|
|
|
class TraceState {
|
2016-06-23 20:44:48 +00:00
|
|
|
public:
|
|
|
|
TraceState(MutationDispatcher &MD, const FuzzingOptions &Options,
|
2016-05-27 21:05:35 +00:00
|
|
|
const Fuzzer *F)
|
2016-05-26 21:32:30 +00:00
|
|
|
: MD(MD), Options(Options), F(F) {}
|
2015-03-30 22:09:51 +00:00
|
|
|
|
2016-01-09 01:39:55 +00:00
|
|
|
void TraceMemcmpCallback(size_t CmpSize, const uint8_t *Data1,
|
|
|
|
const uint8_t *Data2);
|
2015-07-31 01:33:06 +00:00
|
|
|
|
2016-01-09 01:39:55 +00:00
|
|
|
int TryToAddDesiredData(const uint8_t *PresentData,
|
|
|
|
const uint8_t *DesiredData, size_t DataSize);
|
2015-05-07 21:02:11 +00:00
|
|
|
|
|
|
|
void StartTraceRecording() {
|
2017-01-17 23:09:05 +00:00
|
|
|
if (!Options.UseMemcmp && !Options.UseMemmem)
|
2016-05-26 21:32:30 +00:00
|
|
|
return;
|
2016-01-15 06:24:05 +00:00
|
|
|
RecordingMemcmp = Options.UseMemcmp;
|
2016-07-15 23:27:19 +00:00
|
|
|
RecordingMemmem = Options.UseMemmem;
|
2016-01-09 00:38:40 +00:00
|
|
|
NumMutations = 0;
|
2016-07-15 23:27:19 +00:00
|
|
|
InterestingWords.clear();
|
2016-02-13 03:25:16 +00:00
|
|
|
MD.ClearAutoDictionary();
|
2015-05-07 21:02:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-09 03:08:58 +00:00
|
|
|
void StopTraceRecording() {
|
2017-01-17 23:09:05 +00:00
|
|
|
if (!RecordingMemcmp && !RecordingMemmem)
|
2016-06-23 20:44:48 +00:00
|
|
|
return;
|
2016-01-15 06:24:05 +00:00
|
|
|
RecordingMemcmp = false;
|
2016-01-09 03:08:58 +00:00
|
|
|
for (size_t i = 0; i < NumMutations; i++) {
|
|
|
|
auto &M = Mutations[i];
|
2016-01-12 02:08:37 +00:00
|
|
|
if (Options.Verbosity >= 2) {
|
2016-01-16 03:53:32 +00:00
|
|
|
AutoDictUnitCounts[M.W]++;
|
2016-01-12 02:08:37 +00:00
|
|
|
AutoDictAdds++;
|
|
|
|
if ((AutoDictAdds & (AutoDictAdds - 1)) == 0) {
|
2016-01-16 03:53:32 +00:00
|
|
|
typedef std::pair<size_t, Word> CU;
|
2016-01-12 02:08:37 +00:00
|
|
|
std::vector<CU> CountedUnits;
|
|
|
|
for (auto &I : AutoDictUnitCounts)
|
|
|
|
CountedUnits.push_back(std::make_pair(I.second, I.first));
|
|
|
|
std::sort(CountedUnits.begin(), CountedUnits.end(),
|
|
|
|
[](const CU &a, const CU &b) { return a.first > b.first; });
|
|
|
|
Printf("AutoDict:\n");
|
|
|
|
for (auto &I : CountedUnits) {
|
|
|
|
Printf(" %zd ", I.first);
|
2016-09-21 01:50:50 +00:00
|
|
|
PrintASCII(I.second.data(), I.second.size());
|
2016-01-12 02:08:37 +00:00
|
|
|
Printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-15 23:27:19 +00:00
|
|
|
MD.AddWordToAutoDictionary({M.W, M.Pos});
|
2016-01-09 03:08:58 +00:00
|
|
|
}
|
2016-07-15 23:27:19 +00:00
|
|
|
for (auto &W : InterestingWords)
|
|
|
|
MD.AddWordToAutoDictionary({W});
|
2015-05-07 21:02:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-09 01:39:55 +00:00
|
|
|
void AddMutation(uint32_t Pos, uint32_t Size, const uint8_t *Data) {
|
2016-01-09 00:38:40 +00:00
|
|
|
if (NumMutations >= kMaxMutations) return;
|
|
|
|
auto &M = Mutations[NumMutations++];
|
|
|
|
M.Pos = Pos;
|
2016-01-16 03:53:32 +00:00
|
|
|
M.W.Set(Data, Size);
|
2016-01-09 01:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddMutation(uint32_t Pos, uint32_t Size, uint64_t Data) {
|
|
|
|
assert(Size <= sizeof(Data));
|
|
|
|
AddMutation(Pos, Size, reinterpret_cast<uint8_t*>(&Data));
|
2016-01-09 00:38:40 +00:00
|
|
|
}
|
|
|
|
|
2016-07-15 23:27:19 +00:00
|
|
|
void AddInterestingWord(const uint8_t *Data, size_t Size) {
|
|
|
|
if (!RecordingMemmem || !F->InFuzzingThread()) return;
|
|
|
|
if (Size <= 1) return;
|
|
|
|
Size = std::min(Size, Word::GetMaxSize());
|
|
|
|
Word W(Data, Size);
|
|
|
|
InterestingWords.insert(W);
|
|
|
|
}
|
|
|
|
|
2015-03-30 22:09:51 +00:00
|
|
|
private:
|
2015-05-11 20:51:19 +00:00
|
|
|
bool IsTwoByteData(uint64_t Data) {
|
|
|
|
int64_t Signed = static_cast<int64_t>(Data);
|
|
|
|
Signed >>= 16;
|
|
|
|
return Signed == 0 || Signed == -1L;
|
|
|
|
}
|
2016-02-02 23:17:45 +00:00
|
|
|
|
|
|
|
// We don't want to create too many trace-based mutations as it is both
|
|
|
|
// expensive and useless. So after some number of mutations is collected,
|
|
|
|
// start rejecting some of them. The more there are mutations the more we
|
|
|
|
// reject.
|
|
|
|
bool WantToHandleOneMoreMutation() {
|
|
|
|
const size_t FirstN = 64;
|
|
|
|
// Gladly handle first N mutations.
|
|
|
|
if (NumMutations <= FirstN) return true;
|
|
|
|
size_t Diff = NumMutations - FirstN;
|
|
|
|
size_t DiffLog = sizeof(long) * 8 - __builtin_clzl((long)Diff);
|
|
|
|
assert(DiffLog > 0 && DiffLog < 64);
|
2016-02-13 03:25:16 +00:00
|
|
|
bool WantThisOne = MD.GetRand()(1 << DiffLog) == 0; // 1 out of DiffLog.
|
2016-02-02 23:17:45 +00:00
|
|
|
return WantThisOne;
|
|
|
|
}
|
|
|
|
|
2016-01-09 00:38:40 +00:00
|
|
|
static const size_t kMaxMutations = 1 << 16;
|
|
|
|
size_t NumMutations;
|
|
|
|
TraceBasedMutation Mutations[kMaxMutations];
|
2016-07-15 23:27:19 +00:00
|
|
|
// TODO: std::set is too inefficient, need to have a custom DS here.
|
|
|
|
std::set<Word> InterestingWords;
|
2016-02-13 03:25:16 +00:00
|
|
|
MutationDispatcher &MD;
|
2016-06-23 20:44:48 +00:00
|
|
|
const FuzzingOptions Options;
|
2016-05-26 21:32:30 +00:00
|
|
|
const Fuzzer *F;
|
2016-01-16 03:53:32 +00:00
|
|
|
std::map<Word, size_t> AutoDictUnitCounts;
|
2016-01-12 02:08:37 +00:00
|
|
|
size_t AutoDictAdds = 0;
|
2015-03-30 22:09:51 +00:00
|
|
|
};
|
|
|
|
|
2016-01-09 01:39:55 +00:00
|
|
|
int TraceState::TryToAddDesiredData(const uint8_t *PresentData,
|
|
|
|
const uint8_t *DesiredData,
|
|
|
|
size_t DataSize) {
|
2016-02-02 23:17:45 +00:00
|
|
|
if (NumMutations >= kMaxMutations || !WantToHandleOneMoreMutation()) return 0;
|
2016-07-19 18:29:06 +00:00
|
|
|
ScopedDoingMyOwnMemmem scoped_doing_my_own_memmem;
|
2016-05-26 21:32:30 +00:00
|
|
|
const uint8_t *UnitData;
|
|
|
|
auto UnitSize = F->GetCurrentUnitInFuzzingThead(&UnitData);
|
2016-01-09 01:39:55 +00:00
|
|
|
int Res = 0;
|
2016-05-26 21:32:30 +00:00
|
|
|
const uint8_t *Beg = UnitData;
|
|
|
|
const uint8_t *End = Beg + UnitSize;
|
2016-01-09 01:39:55 +00:00
|
|
|
for (const uint8_t *Cur = Beg; Cur < End; Cur++) {
|
2016-12-02 23:02:01 +00:00
|
|
|
Cur = (uint8_t *)SearchMemory(Cur, End - Cur, PresentData, DataSize);
|
2016-01-09 01:39:55 +00:00
|
|
|
if (!Cur)
|
|
|
|
break;
|
|
|
|
size_t Pos = Cur - Beg;
|
2016-05-26 21:32:30 +00:00
|
|
|
assert(Pos < UnitSize);
|
2016-01-09 01:39:55 +00:00
|
|
|
AddMutation(Pos, DataSize, DesiredData);
|
|
|
|
Res++;
|
|
|
|
}
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TraceState::TraceMemcmpCallback(size_t CmpSize, const uint8_t *Data1,
|
|
|
|
const uint8_t *Data2) {
|
2016-05-26 21:32:30 +00:00
|
|
|
if (!RecordingMemcmp || !F->InFuzzingThread()) return;
|
2016-01-16 03:53:32 +00:00
|
|
|
CmpSize = std::min(CmpSize, Word::GetMaxSize());
|
2016-01-09 03:46:08 +00:00
|
|
|
int Added2 = TryToAddDesiredData(Data1, Data2, CmpSize);
|
|
|
|
int Added1 = TryToAddDesiredData(Data2, Data1, CmpSize);
|
|
|
|
if ((Added1 || Added2) && Options.Verbosity >= 3) {
|
|
|
|
Printf("MemCmp Added %d%d: ", Added1, Added2);
|
2016-01-12 02:36:59 +00:00
|
|
|
if (Added1) PrintASCII(Data1, CmpSize);
|
|
|
|
if (Added2) PrintASCII(Data2, CmpSize);
|
2016-01-09 03:46:08 +00:00
|
|
|
Printf("\n");
|
|
|
|
}
|
2016-01-09 01:39:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 21:16:27 +00:00
|
|
|
static TraceState *TS;
|
2015-03-30 22:09:51 +00:00
|
|
|
|
2015-05-07 21:02:11 +00:00
|
|
|
void Fuzzer::StartTraceRecording() {
|
2015-05-11 21:16:27 +00:00
|
|
|
if (!TS) return;
|
|
|
|
TS->StartTraceRecording();
|
2015-05-07 21:02:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-09 03:08:58 +00:00
|
|
|
void Fuzzer::StopTraceRecording() {
|
|
|
|
if (!TS) return;
|
|
|
|
TS->StopTraceRecording();
|
2015-03-30 22:09:51 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 21:16:27 +00:00
|
|
|
void Fuzzer::InitializeTraceState() {
|
2017-01-17 23:09:05 +00:00
|
|
|
if (!Options.UseMemcmp && !Options.UseMemmem) return;
|
2016-05-26 21:32:30 +00:00
|
|
|
TS = new TraceState(MD, Options, this);
|
2015-03-30 22:09:51 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:22:22 +00:00
|
|
|
static size_t InternalStrnlen(const char *S, size_t MaxLen) {
|
|
|
|
size_t Len = 0;
|
|
|
|
for (; Len < MaxLen && S[Len]; Len++) {}
|
|
|
|
return Len;
|
|
|
|
}
|
|
|
|
|
2015-03-30 22:09:51 +00:00
|
|
|
} // namespace fuzzer
|
|
|
|
|
2015-05-11 21:16:27 +00:00
|
|
|
using fuzzer::TS;
|
2016-01-15 06:24:05 +00:00
|
|
|
using fuzzer::RecordingMemcmp;
|
2015-05-11 20:51:19 +00:00
|
|
|
|
2015-03-30 22:09:51 +00:00
|
|
|
extern "C" {
|
2015-08-05 18:23:01 +00:00
|
|
|
|
2016-01-12 16:50:18 +00:00
|
|
|
// We may need to avoid defining weak hooks to stay compatible with older clang.
|
|
|
|
#ifndef LLVM_FUZZER_DEFINES_SANITIZER_WEAK_HOOOKS
|
|
|
|
# define LLVM_FUZZER_DEFINES_SANITIZER_WEAK_HOOOKS 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if LLVM_FUZZER_DEFINES_SANITIZER_WEAK_HOOOKS
|
2015-07-30 01:34:58 +00:00
|
|
|
void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1,
|
2016-01-12 00:43:42 +00:00
|
|
|
const void *s2, size_t n, int result) {
|
|
|
|
if (result == 0) return; // No reason to mutate.
|
2016-01-09 01:39:55 +00:00
|
|
|
if (n <= 1) return; // Not interesting.
|
2017-01-17 23:09:05 +00:00
|
|
|
fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/false);
|
|
|
|
if (!RecordingMemcmp) return;
|
2016-01-09 01:39:55 +00:00
|
|
|
TS->TraceMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1),
|
|
|
|
reinterpret_cast<const uint8_t *>(s2));
|
2015-07-30 02:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1,
|
2016-01-12 00:43:42 +00:00
|
|
|
const char *s2, size_t n, int result) {
|
|
|
|
if (result == 0) return; // No reason to mutate.
|
2015-07-31 17:05:05 +00:00
|
|
|
size_t Len1 = fuzzer::InternalStrnlen(s1, n);
|
|
|
|
size_t Len2 = fuzzer::InternalStrnlen(s2, n);
|
|
|
|
n = std::min(n, Len1);
|
|
|
|
n = std::min(n, Len2);
|
|
|
|
if (n <= 1) return; // Not interesting.
|
2017-01-17 23:09:05 +00:00
|
|
|
fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/true);
|
|
|
|
if (!RecordingMemcmp) return;
|
2016-01-09 01:39:55 +00:00
|
|
|
TS->TraceMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1),
|
|
|
|
reinterpret_cast<const uint8_t *>(s2));
|
2015-07-30 01:34:58 +00:00
|
|
|
}
|
|
|
|
|
2015-08-05 18:23:01 +00:00
|
|
|
void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1,
|
2016-01-12 00:43:42 +00:00
|
|
|
const char *s2, int result) {
|
|
|
|
if (result == 0) return; // No reason to mutate.
|
2015-08-05 18:23:01 +00:00
|
|
|
size_t Len1 = strlen(s1);
|
|
|
|
size_t Len2 = strlen(s2);
|
|
|
|
size_t N = std::min(Len1, Len2);
|
|
|
|
if (N <= 1) return; // Not interesting.
|
2017-01-17 23:09:05 +00:00
|
|
|
fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, N, /*StopAtZero*/true);
|
|
|
|
if (!RecordingMemcmp) return;
|
2016-01-09 01:39:55 +00:00
|
|
|
TS->TraceMemcmpCallback(N, reinterpret_cast<const uint8_t *>(s1),
|
|
|
|
reinterpret_cast<const uint8_t *>(s2));
|
2015-08-05 18:23:01 +00:00
|
|
|
}
|
|
|
|
|
2016-07-15 23:27:19 +00:00
|
|
|
void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1,
|
|
|
|
const char *s2, size_t n, int result) {
|
|
|
|
return __sanitizer_weak_hook_strncmp(called_pc, s1, s2, n, result);
|
|
|
|
}
|
|
|
|
void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1,
|
|
|
|
const char *s2, int result) {
|
|
|
|
return __sanitizer_weak_hook_strcmp(called_pc, s1, s2, result);
|
|
|
|
}
|
|
|
|
void __sanitizer_weak_hook_strstr(void *called_pc, const char *s1,
|
|
|
|
const char *s2, char *result) {
|
|
|
|
TS->AddInterestingWord(reinterpret_cast<const uint8_t *>(s2), strlen(s2));
|
|
|
|
}
|
|
|
|
void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1,
|
|
|
|
const char *s2, char *result) {
|
|
|
|
TS->AddInterestingWord(reinterpret_cast<const uint8_t *>(s2), strlen(s2));
|
|
|
|
}
|
|
|
|
void __sanitizer_weak_hook_memmem(void *called_pc, const void *s1, size_t len1,
|
|
|
|
const void *s2, size_t len2, void *result) {
|
2016-07-19 18:29:06 +00:00
|
|
|
if (fuzzer::DoingMyOwnMemmem) return;
|
|
|
|
TS->AddInterestingWord(reinterpret_cast<const uint8_t *>(s2), len2);
|
2016-07-15 23:27:19 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 16:50:18 +00:00
|
|
|
#endif // LLVM_FUZZER_DEFINES_SANITIZER_WEAK_HOOOKS
|
2015-03-30 22:09:51 +00:00
|
|
|
} // extern "C"
|