[llvm] r258178 - [libFuzzer] use std::mt19937 for generating random numbers by default. Fix MyStoll to handle negative values. Use std::any_of instead of std::find_if
Kostya Serebryany via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 19 12:33:57 PST 2016
Author: kcc
Date: Tue Jan 19 14:33:57 2016
New Revision: 258178
URL: http://llvm.org/viewvc/llvm-project?rev=258178&view=rev
Log:
[libFuzzer] use std::mt19937 for generating random numbers by default. Fix MyStoll to handle negative values. Use std::any_of instead of std::find_if
Modified:
llvm/trunk/lib/Fuzzer/FuzzerDriver.cpp
llvm/trunk/lib/Fuzzer/FuzzerInterface.cpp
llvm/trunk/lib/Fuzzer/FuzzerInterface.h
llvm/trunk/lib/Fuzzer/FuzzerMutate.cpp
llvm/trunk/lib/Fuzzer/test/fuzzer.test
Modified: llvm/trunk/lib/Fuzzer/FuzzerDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/FuzzerDriver.cpp?rev=258178&r1=258177&r2=258178&view=diff
==============================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerDriver.cpp (original)
+++ llvm/trunk/lib/Fuzzer/FuzzerDriver.cpp Tue Jan 19 14:33:57 2016
@@ -100,13 +100,18 @@ static const char *FlagValue(const char
// Avoid calling stol as it triggers a bug in clang/glibc build.
static long MyStol(const char *Str) {
long Res = 0;
+ long Sign = 1;
+ if (*Str == '-') {
+ Str++;
+ Sign = -1;
+ }
for (size_t i = 0; Str[i]; i++) {
char Ch = Str[i];
if (Ch < '0' || Ch > '9')
return Res;
Res = Res * 10 + (Ch - '0');
}
- return Res;
+ return Res * Sign;
}
static bool ParseOneFlag(const char *Param) {
@@ -223,7 +228,7 @@ int RunOneTest(Fuzzer *F, const char *In
}
int FuzzerDriver(int argc, char **argv, UserCallback Callback) {
- FuzzerRandomLibc Rand(0);
+ FuzzerRandom_mt19937 Rand(0);
SimpleUserSuppliedFuzzer SUSF(&Rand, Callback);
return FuzzerDriver(argc, argv, SUSF);
}
@@ -234,7 +239,7 @@ int FuzzerDriver(int argc, char **argv,
}
int FuzzerDriver(const std::vector<std::string> &Args, UserCallback Callback) {
- FuzzerRandomLibc Rand(0);
+ FuzzerRandom_mt19937 Rand(0);
SimpleUserSuppliedFuzzer SUSF(&Rand, Callback);
return FuzzerDriver(Args, SUSF);
}
@@ -326,7 +331,8 @@ int FuzzerDriver(const std::vector<std::
unsigned Seed = Flags.seed;
// Initialize Seed.
if (Seed == 0)
- Seed = time(0) * 10000 + getpid();
+ Seed = (std::chrono::system_clock::now().time_since_epoch().count() << 10) +
+ getpid();
if (Flags.verbosity)
Printf("Seed: %u\n", Seed);
USF.GetRand().ResetSeed(Seed);
Modified: llvm/trunk/lib/Fuzzer/FuzzerInterface.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/FuzzerInterface.cpp?rev=258178&r1=258177&r2=258178&view=diff
==============================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerInterface.cpp (original)
+++ llvm/trunk/lib/Fuzzer/FuzzerInterface.cpp Tue Jan 19 14:33:57 2016
@@ -12,6 +12,7 @@
#include "FuzzerInterface.h"
#include "FuzzerInternal.h"
+#include <random>
namespace fuzzer {
@@ -19,6 +20,19 @@ void FuzzerRandomLibc::ResetSeed(unsigne
size_t FuzzerRandomLibc::Rand() { return rand(); }
+struct FuzzerRandom_mt19937::Impl : public std::mt19937 {
+ Impl(unsigned seed) : std::mt19937(seed) {}
+};
+
+void FuzzerRandom_mt19937::ResetSeed(unsigned int seed) {
+ delete R;
+ R = new Impl(seed);
+}
+
+FuzzerRandom_mt19937::~FuzzerRandom_mt19937() { delete R; }
+
+size_t FuzzerRandom_mt19937::Rand() { return (*R)(); }
+
UserSuppliedFuzzer::UserSuppliedFuzzer(FuzzerRandomBase *Rand)
: Rand(Rand), MD(new MutationDispatcher(*Rand)) {}
Modified: llvm/trunk/lib/Fuzzer/FuzzerInterface.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/FuzzerInterface.h?rev=258178&r1=258177&r2=258178&view=diff
==============================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerInterface.h (original)
+++ llvm/trunk/lib/Fuzzer/FuzzerInterface.h Tue Jan 19 14:33:57 2016
@@ -68,14 +68,27 @@ class FuzzerRandomBase {
bool RandBool() { return Rand() % 2; }
};
+// Using libc's stand/rand.
class FuzzerRandomLibc : public FuzzerRandomBase {
public:
FuzzerRandomLibc(unsigned int seed) { ResetSeed(seed); }
void ResetSeed(unsigned int seed) override;
- ~FuzzerRandomLibc() override {}
+ ~FuzzerRandomLibc() override {};
size_t Rand() override;
};
+// Using std::mt19937
+class FuzzerRandom_mt19937 : public FuzzerRandomBase {
+ public:
+ FuzzerRandom_mt19937(unsigned int seed) { ResetSeed(seed); }
+ void ResetSeed(unsigned int seed) override;
+ ~FuzzerRandom_mt19937() override;
+ size_t Rand() override;
+ private:
+ struct Impl;
+ Impl *R = nullptr;
+};
+
// For backward compatibility only, deprecated.
size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize,
FuzzerRandomBase &Rand);
Modified: llvm/trunk/lib/Fuzzer/FuzzerMutate.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/FuzzerMutate.cpp?rev=258178&r1=258177&r2=258178&view=diff
==============================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerMutate.cpp (original)
+++ llvm/trunk/lib/Fuzzer/FuzzerMutate.cpp Tue Jan 19 14:33:57 2016
@@ -29,9 +29,8 @@ struct DictionaryEntry {
struct Dictionary : public std::vector<DictionaryEntry>{
bool ContainsWord(const Word &W) const {
- return end() !=
- std::find_if(begin(), end(),
- [&](const DictionaryEntry &DE) { return DE.W == W; });
+ return std::any_of(begin(), end(),
+ [&](const DictionaryEntry &DE) { return DE.W == W; });
}
};
Modified: llvm/trunk/lib/Fuzzer/test/fuzzer.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/test/fuzzer.test?rev=258178&r1=258177&r2=258178&view=diff
==============================================================================
--- llvm/trunk/lib/Fuzzer/test/fuzzer.test (original)
+++ llvm/trunk/lib/Fuzzer/test/fuzzer.test Tue Jan 19 14:33:57 2016
@@ -39,3 +39,6 @@ OOB: AddressSanitizer: heap-buffer-overf
OOB: is located 0 bytes to the right of 3-byte region
RUN: not LLVMFuzzer-InitializeTest 2>&1 | FileCheck %s
+
+RUN: LLVMFuzzer-SimpleCmpTest -seed=-1 -runs=0 2>&1 | FileCheck %s --check-prefix=CHECK_SEED_MINUS_ONE
+CHECK_SEED_MINUS_ONE: Seed: 4294967295
More information about the llvm-commits
mailing list