1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2025-01-31 20:51:52 +01:00

[LibFuzzer] Introducing FUZZER_FLAG_UNSIGNED and using it for seeding.

Differential Revision: http://reviews.llvm.org/D15339

done

llvm-svn: 255296
This commit is contained in:
Mike Aizatsky 2015-12-10 20:41:53 +00:00
parent b5effc7a01
commit ea27e92765
5 changed files with 25 additions and 9 deletions

View File

@ -32,23 +32,30 @@ struct FlagDescription {
int Default; int Default;
int *IntFlag; int *IntFlag;
const char **StrFlag; const char **StrFlag;
unsigned int *UIntFlag;
}; };
struct { struct {
#define FUZZER_FLAG_INT(Name, Default, Description) int Name; #define FUZZER_FLAG_INT(Name, Default, Description) int Name;
#define FUZZER_FLAG_UNSIGNED(Name, Default, Description) unsigned int Name;
#define FUZZER_FLAG_STRING(Name, Description) const char *Name; #define FUZZER_FLAG_STRING(Name, Description) const char *Name;
#include "FuzzerFlags.def" #include "FuzzerFlags.def"
#undef FUZZER_FLAG_INT #undef FUZZER_FLAG_INT
#undef FUZZER_FLAG_UNSIGNED
#undef FUZZER_FLAG_STRING #undef FUZZER_FLAG_STRING
} Flags; } Flags;
static const FlagDescription FlagDescriptions [] { static const FlagDescription FlagDescriptions [] {
#define FUZZER_FLAG_INT(Name, Default, Description) \ #define FUZZER_FLAG_INT(Name, Default, Description) \
{ #Name, Description, Default, &Flags.Name, nullptr}, {#Name, Description, Default, &Flags.Name, nullptr, nullptr},
#define FUZZER_FLAG_UNSIGNED(Name, Default, Description) \
{#Name, Description, static_cast<int>(Default), \
nullptr, nullptr, &Flags.Name},
#define FUZZER_FLAG_STRING(Name, Description) \ #define FUZZER_FLAG_STRING(Name, Description) \
{ #Name, Description, 0, nullptr, &Flags.Name }, {#Name, Description, 0, nullptr, &Flags.Name, nullptr},
#include "FuzzerFlags.def" #include "FuzzerFlags.def"
#undef FUZZER_FLAG_INT #undef FUZZER_FLAG_INT
#undef FUZZER_FLAG_UNSIGNED
#undef FUZZER_FLAG_STRING #undef FUZZER_FLAG_STRING
}; };
@ -106,6 +113,12 @@ static bool ParseOneFlag(const char *Param) {
if (Flags.verbosity >= 2) if (Flags.verbosity >= 2)
Printf("Flag: %s %d\n", Name, Val);; Printf("Flag: %s %d\n", Name, Val);;
return true; return true;
} else if (FlagDescriptions[F].UIntFlag) {
unsigned int Val = std::stoul(Str);
*FlagDescriptions[F].UIntFlag = Val;
if (Flags.verbosity >= 2)
Printf("Flag: %s %u\n", Name, Val);
return true;
} else if (FlagDescriptions[F].StrFlag) { } else if (FlagDescriptions[F].StrFlag) {
*FlagDescriptions[F].StrFlag = Str; *FlagDescriptions[F].StrFlag = Str;
if (Flags.verbosity >= 2) if (Flags.verbosity >= 2)
@ -123,6 +136,9 @@ static void ParseFlags(const std::vector<std::string> &Args) {
for (size_t F = 0; F < kNumFlags; F++) { for (size_t F = 0; F < kNumFlags; F++) {
if (FlagDescriptions[F].IntFlag) if (FlagDescriptions[F].IntFlag)
*FlagDescriptions[F].IntFlag = FlagDescriptions[F].Default; *FlagDescriptions[F].IntFlag = FlagDescriptions[F].Default;
if (FlagDescriptions[F].UIntFlag)
*FlagDescriptions[F].UIntFlag =
static_cast<unsigned int>(FlagDescriptions[F].Default);
if (FlagDescriptions[F].StrFlag) if (FlagDescriptions[F].StrFlag)
*FlagDescriptions[F].StrFlag = nullptr; *FlagDescriptions[F].StrFlag = nullptr;
} }

View File

@ -11,7 +11,7 @@
// portability and independence. // portability and independence.
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
FUZZER_FLAG_INT(verbosity, 1, "Verbosity level.") FUZZER_FLAG_INT(verbosity, 1, "Verbosity level.")
FUZZER_FLAG_INT(seed, 0, "Random seed. If 0, seed is generated.") FUZZER_FLAG_UNSIGNED(seed, 0, "Random seed. If 0, seed is generated.")
FUZZER_FLAG_INT(runs, -1, FUZZER_FLAG_INT(runs, -1,
"Number of individual test runs (-1 for infinite runs).") "Number of individual test runs (-1 for infinite runs).")
FUZZER_FLAG_INT(max_len, 64, "Maximum length of the test input.") FUZZER_FLAG_INT(max_len, 64, "Maximum length of the test input.")

View File

@ -15,7 +15,7 @@
namespace fuzzer { namespace fuzzer {
void FuzzerRandomLibc::ResetSeed(int seed) { srand(seed); } void FuzzerRandomLibc::ResetSeed(unsigned int seed) { srand(seed); }
size_t FuzzerRandomLibc::Rand() { return rand(); } size_t FuzzerRandomLibc::Rand() { return rand(); }

View File

@ -50,7 +50,7 @@ class FuzzerRandomBase {
public: public:
FuzzerRandomBase(){} FuzzerRandomBase(){}
virtual ~FuzzerRandomBase(){}; virtual ~FuzzerRandomBase(){};
virtual void ResetSeed(int seed) = 0; virtual void ResetSeed(unsigned int seed) = 0;
// Return a random number. // Return a random number.
virtual size_t Rand() = 0; virtual size_t Rand() = 0;
// Return a random number in range [0,n). // Return a random number in range [0,n).
@ -60,8 +60,8 @@ class FuzzerRandomBase {
class FuzzerRandomLibc : public FuzzerRandomBase { class FuzzerRandomLibc : public FuzzerRandomBase {
public: public:
FuzzerRandomLibc(int seed) { ResetSeed(seed); } FuzzerRandomLibc(unsigned int seed) { ResetSeed(seed); }
void ResetSeed(int seed) override; void ResetSeed(unsigned int seed) override;
~FuzzerRandomLibc() override {} ~FuzzerRandomLibc() override {}
size_t Rand() override; size_t Rand() override;
}; };

View File

@ -48,8 +48,8 @@ RUN: not LLVMFuzzer-SimpleCmpTest -use_traces=1 -seed=1 -runs=1000000 -timeout=5
RUN: not LLVMFuzzer-UserSuppliedFuzzerTest -seed=1 -timeout=15 2>&1 | FileCheck %s RUN: not LLVMFuzzer-UserSuppliedFuzzerTest -seed=1 -timeout=15 2>&1 | FileCheck %s
RUN: not LLVMFuzzer-MemcmpTest -use_traces=1 -seed=1 -runs=100000 2>&1 | FileCheck %s RUN: not LLVMFuzzer-MemcmpTest -use_traces=1 -seed=4294967295 -runs=100000 2>&1 | FileCheck %s
RUN: LLVMFuzzer-MemcmpTest -seed=1 -runs=1000000 2>&1 | FileCheck %s --check-prefix=Done1000000 RUN: LLVMFuzzer-MemcmpTest -seed=4294967295 -runs=1000000 2>&1 | FileCheck %s --check-prefix=Done1000000
Done1000000: Done 1000000 runs in Done1000000: Done 1000000 runs in
RUN: not LLVMFuzzer-StrncmpTest -use_traces=1 -seed=1 -runs=100000 2>&1 | FileCheck %s RUN: not LLVMFuzzer-StrncmpTest -use_traces=1 -seed=1 -runs=100000 2>&1 | FileCheck %s