2015-05-12 05:16:27 +08:00
|
|
|
//===- FuzzerTraceState.cpp - Trace-based fuzzer mutator ------------------===//
|
2015-03-31 06:09:51 +08: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 14:08:46 +08:00
|
|
|
// Data tracing.
|
2015-03-31 06:09:51 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-09-21 09:50:50 +08:00
|
|
|
#include "FuzzerDictionary.h"
|
2016-12-01 03:06:14 +08:00
|
|
|
#include "FuzzerInternal.h"
|
|
|
|
#include "FuzzerIO.h"
|
2016-09-21 09:50:50 +08:00
|
|
|
#include "FuzzerMutate.h"
|
|
|
|
#include "FuzzerRandom.h"
|
2016-09-23 08:46:18 +08:00
|
|
|
#include "FuzzerTracePC.h"
|
2015-05-08 05:02:11 +08:00
|
|
|
#include <algorithm>
|
2015-03-31 06:09:51 +08:00
|
|
|
#include <cstring>
|
2016-01-12 10:08:37 +08:00
|
|
|
#include <map>
|
2016-07-16 07:27:19 +08:00
|
|
|
#include <set>
|
2016-12-01 03:06:14 +08:00
|
|
|
#include <thread>
|
2015-03-31 06:09:51 +08:00
|
|
|
|
2015-05-12 04:51:19 +08:00
|
|
|
namespace fuzzer {
|
|
|
|
|
2015-05-08 05:02:11 +08:00
|
|
|
// For now, very simple: put Size bytes of Data at position Pos.
|
|
|
|
struct TraceBasedMutation {
|
2016-01-16 11:53:32 +08:00
|
|
|
uint32_t Pos;
|
|
|
|
Word W;
|
2015-05-08 05:02:11 +08:00
|
|
|
};
|
|
|
|
|
2016-01-15 14:24:05 +08:00
|
|
|
// Declared as static globals for faster checks inside the hooks.
|
|
|
|
static bool RecordingMemcmp = false;
|
2016-07-16 07:27:19 +08:00
|
|
|
static bool RecordingMemmem = false;
|
2016-07-20 02:29:06 +08:00
|
|
|
static bool DoingMyOwnMemmem = false;
|
|
|
|
|
2016-10-15 04:20:33 +08:00
|
|
|
ScopedDoingMyOwnMemmem::ScopedDoingMyOwnMemmem() { DoingMyOwnMemmem = true; }
|
|
|
|
ScopedDoingMyOwnMemmem::~ScopedDoingMyOwnMemmem() { DoingMyOwnMemmem = false; }
|
2016-01-15 14:24:05 +08:00
|
|
|
|
2015-05-12 05:16:27 +08:00
|
|
|
class TraceState {
|
2016-06-24 04:44:48 +08:00
|
|
|
public:
|
|
|
|
TraceState(MutationDispatcher &MD, const FuzzingOptions &Options,
|
2016-05-28 05:05:35 +08:00
|
|
|
const Fuzzer *F)
|
2016-05-27 05:32:30 +08:00
|
|
|
: MD(MD), Options(Options), F(F) {}
|
2015-03-31 06:09:51 +08:00
|
|
|
|
2016-01-09 09:39:55 +08:00
|
|
|
void TraceMemcmpCallback(size_t CmpSize, const uint8_t *Data1,
|
|
|
|
const uint8_t *Data2);
|
2015-07-31 09:33:06 +08:00
|
|
|
|
2016-01-09 09:39:55 +08:00
|
|
|
int TryToAddDesiredData(const uint8_t *PresentData,
|
|
|
|
const uint8_t *DesiredData, size_t DataSize);
|
2015-05-08 05:02:11 +08:00
|
|
|
|
|
|
|
void StartTraceRecording() {
|
2017-01-18 07:09:05 +08:00
|
|
|
if (!Options.UseMemcmp && !Options.UseMemmem)
|
2016-05-27 05:32:30 +08:00
|
|
|
return;
|
2016-01-15 14:24:05 +08:00
|
|
|
RecordingMemcmp = Options.UseMemcmp;
|
2016-07-16 07:27:19 +08:00
|
|
|
RecordingMemmem = Options.UseMemmem;
|
2016-01-09 08:38:40 +08:00
|
|
|
NumMutations = 0;
|
2016-07-16 07:27:19 +08:00
|
|
|
InterestingWords.clear();
|
2016-02-13 11:25:16 +08:00
|
|
|
MD.ClearAutoDictionary();
|
2015-05-08 05:02:11 +08:00
|
|
|
}
|
|
|
|
|
2016-01-09 11:08:58 +08:00
|
|
|
void StopTraceRecording() {
|
2017-01-18 07:09:05 +08:00
|
|
|
if (!RecordingMemcmp && !RecordingMemmem)
|
2016-06-24 04:44:48 +08:00
|
|
|
return;
|
2016-01-15 14:24:05 +08:00
|
|
|
RecordingMemcmp = false;
|
2016-01-09 11:08:58 +08:00
|
|
|
for (size_t i = 0; i < NumMutations; i++) {
|
|
|
|
auto &M = Mutations[i];
|
2016-01-12 10:08:37 +08:00
|
|
|
if (Options.Verbosity >= 2) {
|
2016-01-16 11:53:32 +08:00
|
|
|
AutoDictUnitCounts[M.W]++;
|
2016-01-12 10:08:37 +08:00
|
|
|
AutoDictAdds++;
|
|
|
|
if ((AutoDictAdds & (AutoDictAdds - 1)) == 0) {
|
2016-01-16 11:53:32 +08:00
|
|
|
typedef std::pair<size_t, Word> CU;
|
2016-01-12 10:08:37 +08: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 09:50:50 +08:00
|
|
|
PrintASCII(I.second.data(), I.second.size());
|
2016-01-12 10:08:37 +08:00
|
|
|
Printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-16 07:27:19 +08:00
|
|
|
MD.AddWordToAutoDictionary({M.W, M.Pos});
|
2016-01-09 11:08:58 +08:00
|
|
|
}
|
2016-07-16 07:27:19 +08:00
|
|
|
for (auto &W : InterestingWords)
|
|
|
|
MD.AddWordToAutoDictionary({W});
|
2015-05-08 05:02:11 +08:00
|
|
|
}
|
|
|
|
|
2016-01-09 09:39:55 +08:00
|
|
|
void AddMutation(uint32_t Pos, uint32_t Size, const uint8_t *Data) {
|
2016-01-09 08:38:40 +08:00
|
|
|
if (NumMutations >= kMaxMutations) return;
|
|
|
|
auto &M = Mutations[NumMutations++];
|
|
|
|
M.Pos = Pos;
|
2016-01-16 11:53:32 +08:00
|
|
|
M.W.Set(Data, Size);
|
2016-01-09 09:39:55 +08: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 08:38:40 +08:00
|
|
|
}
|
|
|
|
|
2016-07-16 07:27:19 +08: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-31 06:09:51 +08:00
|
|
|
private:
|
2015-05-12 04:51:19 +08:00
|
|
|
bool IsTwoByteData(uint64_t Data) {
|
|
|
|
int64_t Signed = static_cast<int64_t>(Data);
|
|
|
|
Signed >>= 16;
|
|
|
|
return Signed == 0 || Signed == -1L;
|
|
|
|
}
|
2016-02-03 07:17:45 +08: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 11:25:16 +08:00
|
|
|
bool WantThisOne = MD.GetRand()(1 << DiffLog) == 0; // 1 out of DiffLog.
|
2016-02-03 07:17:45 +08:00
|
|
|
return WantThisOne;
|
|
|
|
}
|
|
|
|
|
2016-01-09 08:38:40 +08:00
|
|
|
static const size_t kMaxMutations = 1 << 16;
|
|
|
|
size_t NumMutations;
|
|
|
|
TraceBasedMutation Mutations[kMaxMutations];
|
2016-07-16 07:27:19 +08:00
|
|
|
// TODO: std::set is too inefficient, need to have a custom DS here.
|
|
|
|
std::set<Word> InterestingWords;
|
2016-02-13 11:25:16 +08:00
|
|
|
MutationDispatcher &MD;
|
2016-06-24 04:44:48 +08:00
|
|
|
const FuzzingOptions Options;
|
2016-05-27 05:32:30 +08:00
|
|
|
const Fuzzer *F;
|
2016-01-16 11:53:32 +08:00
|
|
|
std::map<Word, size_t> AutoDictUnitCounts;
|
2016-01-12 10:08:37 +08:00
|
|
|
size_t AutoDictAdds = 0;
|
2015-03-31 06:09:51 +08:00
|
|
|
};
|
|
|
|
|
2016-01-09 09:39:55 +08:00
|
|
|
int TraceState::TryToAddDesiredData(const uint8_t *PresentData,
|
|
|
|
const uint8_t *DesiredData,
|
|
|
|
size_t DataSize) {
|
2016-02-03 07:17:45 +08:00
|
|
|
if (NumMutations >= kMaxMutations || !WantToHandleOneMoreMutation()) return 0;
|
2016-07-20 02:29:06 +08:00
|
|
|
ScopedDoingMyOwnMemmem scoped_doing_my_own_memmem;
|
2016-05-27 05:32:30 +08:00
|
|
|
const uint8_t *UnitData;
|
|
|
|
auto UnitSize = F->GetCurrentUnitInFuzzingThead(&UnitData);
|
2016-01-09 09:39:55 +08:00
|
|
|
int Res = 0;
|
2016-05-27 05:32:30 +08:00
|
|
|
const uint8_t *Beg = UnitData;
|
|
|
|
const uint8_t *End = Beg + UnitSize;
|
2016-01-09 09:39:55 +08:00
|
|
|
for (const uint8_t *Cur = Beg; Cur < End; Cur++) {
|
2016-12-03 07:02:01 +08:00
|
|
|
Cur = (uint8_t *)SearchMemory(Cur, End - Cur, PresentData, DataSize);
|
2016-01-09 09:39:55 +08:00
|
|
|
if (!Cur)
|
|
|
|
break;
|
|
|
|
size_t Pos = Cur - Beg;
|
2016-05-27 05:32:30 +08:00
|
|
|
assert(Pos < UnitSize);
|
2016-01-09 09:39:55 +08:00
|
|
|
AddMutation(Pos, DataSize, DesiredData);
|
|
|
|
Res++;
|
|
|
|
}
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TraceState::TraceMemcmpCallback(size_t CmpSize, const uint8_t *Data1,
|
|
|
|
const uint8_t *Data2) {
|
2016-05-27 05:32:30 +08:00
|
|
|
if (!RecordingMemcmp || !F->InFuzzingThread()) return;
|
2016-01-16 11:53:32 +08:00
|
|
|
CmpSize = std::min(CmpSize, Word::GetMaxSize());
|
2016-01-09 11:46:08 +08: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 10:36:59 +08:00
|
|
|
if (Added1) PrintASCII(Data1, CmpSize);
|
|
|
|
if (Added2) PrintASCII(Data2, CmpSize);
|
2016-01-09 11:46:08 +08:00
|
|
|
Printf("\n");
|
|
|
|
}
|
2016-01-09 09:39:55 +08:00
|
|
|
}
|
|
|
|
|
2015-05-12 05:16:27 +08:00
|
|
|
static TraceState *TS;
|
2015-03-31 06:09:51 +08:00
|
|
|
|
2015-05-08 05:02:11 +08:00
|
|
|
void Fuzzer::StartTraceRecording() {
|
2015-05-12 05:16:27 +08:00
|
|
|
if (!TS) return;
|
|
|
|
TS->StartTraceRecording();
|
2015-05-08 05:02:11 +08:00
|
|
|
}
|
|
|
|
|
2016-01-09 11:08:58 +08:00
|
|
|
void Fuzzer::StopTraceRecording() {
|
|
|
|
if (!TS) return;
|
|
|
|
TS->StopTraceRecording();
|
2015-03-31 06:09:51 +08:00
|
|
|
}
|
|
|
|
|
2015-05-12 05:16:27 +08:00
|
|
|
void Fuzzer::InitializeTraceState() {
|
2017-01-18 07:09:05 +08:00
|
|
|
if (!Options.UseMemcmp && !Options.UseMemmem) return;
|
2016-05-27 05:32:30 +08:00
|
|
|
TS = new TraceState(MD, Options, this);
|
2015-03-31 06:09:51 +08:00
|
|
|
}
|
|
|
|
|
2015-07-31 05:22:22 +08: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-31 06:09:51 +08:00
|
|
|
} // namespace fuzzer
|
|
|
|
|
2015-05-12 05:16:27 +08:00
|
|
|
using fuzzer::TS;
|
2016-01-15 14:24:05 +08:00
|
|
|
using fuzzer::RecordingMemcmp;
|
2015-05-12 04:51:19 +08:00
|
|
|
|
2015-03-31 06:09:51 +08:00
|
|
|
extern "C" {
|
2015-08-06 02:23:01 +08:00
|
|
|
|
2016-01-13 00:50:18 +08: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
|
2017-01-18 07:50:21 +08:00
|
|
|
|
|
|
|
ATTRIBUTE_NO_SANITIZE_MEMORY
|
2015-07-30 09:34:58 +08:00
|
|
|
void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1,
|
2016-01-12 08:43:42 +08:00
|
|
|
const void *s2, size_t n, int result) {
|
|
|
|
if (result == 0) return; // No reason to mutate.
|
2016-01-09 09:39:55 +08:00
|
|
|
if (n <= 1) return; // Not interesting.
|
2017-01-18 07:09:05 +08:00
|
|
|
fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/false);
|
|
|
|
if (!RecordingMemcmp) return;
|
2016-01-09 09:39:55 +08:00
|
|
|
TS->TraceMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1),
|
|
|
|
reinterpret_cast<const uint8_t *>(s2));
|
2015-07-30 10:33:45 +08:00
|
|
|
}
|
|
|
|
|
2017-01-18 07:50:21 +08:00
|
|
|
ATTRIBUTE_NO_SANITIZE_MEMORY
|
2015-07-30 10:33:45 +08:00
|
|
|
void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1,
|
2016-01-12 08:43:42 +08:00
|
|
|
const char *s2, size_t n, int result) {
|
|
|
|
if (result == 0) return; // No reason to mutate.
|
2015-08-01 01:05:05 +08: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-18 07:09:05 +08:00
|
|
|
fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/true);
|
|
|
|
if (!RecordingMemcmp) return;
|
2016-01-09 09:39:55 +08:00
|
|
|
TS->TraceMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1),
|
|
|
|
reinterpret_cast<const uint8_t *>(s2));
|
2015-07-30 09:34:58 +08:00
|
|
|
}
|
|
|
|
|
2017-01-18 07:50:21 +08:00
|
|
|
|
|
|
|
ATTRIBUTE_NO_SANITIZE_MEMORY
|
2015-08-06 02:23:01 +08:00
|
|
|
void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1,
|
2016-01-12 08:43:42 +08:00
|
|
|
const char *s2, int result) {
|
|
|
|
if (result == 0) return; // No reason to mutate.
|
2015-08-06 02:23:01 +08: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-18 07:09:05 +08:00
|
|
|
fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, N, /*StopAtZero*/true);
|
|
|
|
if (!RecordingMemcmp) return;
|
2016-01-09 09:39:55 +08:00
|
|
|
TS->TraceMemcmpCallback(N, reinterpret_cast<const uint8_t *>(s1),
|
|
|
|
reinterpret_cast<const uint8_t *>(s2));
|
2015-08-06 02:23:01 +08:00
|
|
|
}
|
|
|
|
|
2017-01-18 07:50:21 +08:00
|
|
|
ATTRIBUTE_NO_SANITIZE_MEMORY
|
2016-07-16 07:27:19 +08: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);
|
|
|
|
}
|
2017-01-18 07:50:21 +08:00
|
|
|
|
|
|
|
ATTRIBUTE_NO_SANITIZE_MEMORY
|
2016-07-16 07:27:19 +08:00
|
|
|
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);
|
|
|
|
}
|
2017-01-18 07:50:21 +08:00
|
|
|
|
|
|
|
ATTRIBUTE_NO_SANITIZE_MEMORY
|
2016-07-16 07:27:19 +08:00
|
|
|
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));
|
|
|
|
}
|
2017-01-18 07:50:21 +08:00
|
|
|
|
|
|
|
ATTRIBUTE_NO_SANITIZE_MEMORY
|
2016-07-16 07:27:19 +08:00
|
|
|
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));
|
|
|
|
}
|
2017-01-18 07:50:21 +08:00
|
|
|
|
|
|
|
ATTRIBUTE_NO_SANITIZE_MEMORY
|
2016-07-16 07:27:19 +08:00
|
|
|
void __sanitizer_weak_hook_memmem(void *called_pc, const void *s1, size_t len1,
|
|
|
|
const void *s2, size_t len2, void *result) {
|
2016-07-20 02:29:06 +08:00
|
|
|
if (fuzzer::DoingMyOwnMemmem) return;
|
|
|
|
TS->AddInterestingWord(reinterpret_cast<const uint8_t *>(s2), len2);
|
2016-07-16 07:27:19 +08:00
|
|
|
}
|
|
|
|
|
2016-01-13 00:50:18 +08:00
|
|
|
#endif // LLVM_FUZZER_DEFINES_SANITIZER_WEAK_HOOOKS
|
2015-03-31 06:09:51 +08:00
|
|
|
} // extern "C"
|