summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Fuzzer/FuzzerInternal.h
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/Fuzzer/FuzzerInternal.h')
-rw-r--r--llvm/lib/Fuzzer/FuzzerInternal.h47
1 files changed, 11 insertions, 36 deletions
diff --git a/llvm/lib/Fuzzer/FuzzerInternal.h b/llvm/lib/Fuzzer/FuzzerInternal.h
index f57e24f739f..c966b267786 100644
--- a/llvm/lib/Fuzzer/FuzzerInternal.h
+++ b/llvm/lib/Fuzzer/FuzzerInternal.h
@@ -98,41 +98,16 @@ bool IsASCII(const Unit &U);
int NumberOfCpuCores();
int GetPid();
-class FuzzerRandomBase {
+class Random {
public:
- FuzzerRandomBase(){}
- virtual ~FuzzerRandomBase(){};
- virtual void ResetSeed(unsigned int seed) = 0;
- // Return a random number.
- virtual size_t Rand() = 0;
- // Return a random number in range [0,n).
+ Random(unsigned int seed) : R(seed) {}
+ size_t Rand() { return R(); }
+ size_t RandBool() { return Rand() % 2; }
size_t operator()(size_t n) { return n ? Rand() % n : 0; }
- 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 {};
- 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;
+ std::mt19937 R;
};
-
-
// Dictionary.
// Parses one dictionary entry.
@@ -145,7 +120,7 @@ bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units);
class MutationDispatcher {
public:
- MutationDispatcher(FuzzerRandomBase &Rand);
+ MutationDispatcher(Random &Rand);
~MutationDispatcher();
/// Indicate that we are about to start a new sequence of mutations.
void StartMutationSequence();
@@ -199,14 +174,14 @@ public:
void SetCorpus(const std::vector<Unit> *Corpus);
private:
- FuzzerRandomBase &Rand;
+ Random &Rand;
struct Impl;
Impl *MDImpl;
};
class UserSuppliedFuzzer {
public:
- UserSuppliedFuzzer(FuzzerRandomBase *Rand);
+ UserSuppliedFuzzer(Random *Rand);
/// Executes the target function on 'Size' bytes of 'Data'.
virtual int TargetFunction(const uint8_t *Data, size_t Size) = 0;
/// Mutates 'Size' bytes of data in 'Data' inplace into up to 'MaxSize' bytes,
@@ -219,13 +194,13 @@ class UserSuppliedFuzzer {
uint8_t *Out, size_t MaxOutSize);
virtual ~UserSuppliedFuzzer();
- FuzzerRandomBase &GetRand() { return *Rand; }
+ Random &GetRand() { return *Rand; }
MutationDispatcher &GetMD() { return *MD; }
private:
bool OwnRand = false;
- FuzzerRandomBase *Rand;
+ Random *Rand;
MutationDispatcher *MD;
};
@@ -370,7 +345,7 @@ private:
class SimpleUserSuppliedFuzzer : public UserSuppliedFuzzer {
public:
- SimpleUserSuppliedFuzzer(FuzzerRandomBase *Rand, UserCallback Callback)
+ SimpleUserSuppliedFuzzer(Random *Rand, UserCallback Callback)
: UserSuppliedFuzzer(Rand), Callback(Callback) {}
virtual int TargetFunction(const uint8_t *Data, size_t Size) override {
OpenPOWER on IntegriCloud