diff options
author | Kostya Serebryany <kcc@google.com> | 2017-05-09 01:17:29 +0000 |
---|---|---|
committer | Kostya Serebryany <kcc@google.com> | 2017-05-09 01:17:29 +0000 |
commit | fe4ed9bd854d1230723568a8cc6289bf9da7fff7 (patch) | |
tree | c9d6415d69ed700008c4c4783eb3cb80048411f4 /llvm/lib/Fuzzer/FuzzerLoop.cpp | |
parent | 9f29914d4018c0c8ef2b7b7bceffb0d07483e89c (diff) | |
download | llvm-fe4ed9bd854d1230723568a8cc6289bf9da7fff7.zip llvm-fe4ed9bd854d1230723568a8cc6289bf9da7fff7.tar.gz llvm-fe4ed9bd854d1230723568a8cc6289bf9da7fff7.tar.bz2 |
[libFuzzer] make sure the input data is not overwritten in the fuzz target (if it is -- report an error)
llvm-svn: 302494
Diffstat (limited to 'llvm/lib/Fuzzer/FuzzerLoop.cpp')
-rw-r--r-- | llvm/lib/Fuzzer/FuzzerLoop.cpp | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/llvm/lib/Fuzzer/FuzzerLoop.cpp b/llvm/lib/Fuzzer/FuzzerLoop.cpp index d84c3db..14caa20 100644 --- a/llvm/lib/Fuzzer/FuzzerLoop.cpp +++ b/llvm/lib/Fuzzer/FuzzerLoop.cpp @@ -422,6 +422,24 @@ size_t Fuzzer::GetCurrentUnitInFuzzingThead(const uint8_t **Data) const { return CurrentUnitSize; } +void Fuzzer::CrashOnOverwrittenData() { + Printf("==%d== ERROR: libFuzzer: fuzz target overwrites it's const input\n", + GetPid()); + DumpCurrentUnit("crash-"); + Printf("SUMMARY: libFuzzer: out-of-memory\n"); + _Exit(Options.ErrorExitCode); // Stop right now. +} + +// Compare two arrays, but not all bytes if the arrays are large. +static bool LooseMemeq(const uint8_t *A, const uint8_t *B, size_t Size) { + const size_t Limit = 64; + if (Size <= 64) + return !memcmp(A, B, Size); + // Compare first and last Limit/2 bytes. + return !memcmp(A, B, Limit / 2) && + !memcmp(A + Size - Limit / 2, B + Size - Limit / 2, Limit / 2); +} + void Fuzzer::ExecuteCallback(const uint8_t *Data, size_t Size) { assert(InFuzzingThread()); if (SMR.IsClient()) @@ -443,6 +461,8 @@ void Fuzzer::ExecuteCallback(const uint8_t *Data, size_t Size) { (void)Res; assert(Res == 0); HasMoreMallocsThanFrees = AllocTracer.Stop(); + if (!LooseMemeq(DataCopy, Data, Size)) + CrashOnOverwrittenData(); CurrentUnitSize = 0; delete[] DataCopy; } |