Skip to content

Commit

Permalink
Further reduce the maximum repeat count when fuzzing.
Browse files Browse the repository at this point in the history
Change-Id: Icd0a5ce36d7b131996da18d7186605fdaaa28357
Reviewed-on: https://code-review.googlesource.com/c/re2/+/59090
Reviewed-by: Paul Wankadia <[email protected]>
  • Loading branch information
junyer committed Jul 24, 2021
1 parent 892ed21 commit 3a95199
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
8 changes: 4 additions & 4 deletions re2/dfa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ namespace re2 {
// Controls whether the DFA should bail out early if the NFA would be faster.
static bool dfa_should_bail_when_slow = true;

void Prog::TESTING_ONLY_set_dfa_should_bail_when_slow(bool b) {
dfa_should_bail_when_slow = b;
}

// Changing this to true compiles in prints that trace execution of the DFA.
// Generates a lot of output -- only useful for debugging.
static const bool ExtraDebug = false;
Expand Down Expand Up @@ -1966,10 +1970,6 @@ int Prog::BuildEntireDFA(MatchKind kind, const DFAStateCallback& cb) {
return GetDFA(kind)->BuildAllStates(cb);
}

void Prog::TEST_dfa_should_bail_when_slow(bool b) {
dfa_should_bail_when_slow = b;
}

// Computes min and max for matching string.
// Won't return strings bigger than maxlen.
bool DFA::PossibleMatchRange(std::string* min, std::string* max, int maxlen) {
Expand Down
5 changes: 5 additions & 0 deletions re2/fuzzing/re2_fuzzer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "re2/prefilter.h"
#include "re2/re2.h"
#include "re2/regexp.h"

using re2::StringPiece;

Expand Down Expand Up @@ -50,6 +51,10 @@ void TestOneInput(StringPiece pattern, const RE2::Options& options,
if (backslash_p > 1)
return;

// The default is 1000. Even 100 turned out to be too generous
// for fuzzing, empirically speaking, so let's try 10 instead.
re2::Regexp::FUZZING_ONLY_set_maximum_repeat_count(10);

RE2 re(pattern, options);
if (!re.ok())
return;
Expand Down
18 changes: 10 additions & 8 deletions re2/parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@

namespace re2 {

// Reduce the maximum repeat count by an order of magnitude when fuzzing.
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
static const int kMaxRepeat = 100;
#else
static const int kMaxRepeat = 1000;
#endif
// Controls the maximum repeat count permitted by the parser.
static int maximum_repeat_count = 1000;

void Regexp::FUZZING_ONLY_set_maximum_repeat_count(int i) {
maximum_repeat_count = i;
}

// Regular expression parse state.
// The list of parsed regexps so far is maintained as a vector of
Expand Down Expand Up @@ -568,7 +568,9 @@ int RepetitionWalker::ShortVisit(Regexp* re, int parent_arg) {
bool Regexp::ParseState::PushRepetition(int min, int max,
const StringPiece& s,
bool nongreedy) {
if ((max != -1 && max < min) || min > kMaxRepeat || max > kMaxRepeat) {
if ((max != -1 && max < min) ||
min > maximum_repeat_count ||
max > maximum_repeat_count) {
status_->set_code(kRegexpRepeatSize);
status_->set_error_arg(s);
return false;
Expand All @@ -591,7 +593,7 @@ bool Regexp::ParseState::PushRepetition(int min, int max,
stacktop_ = re;
if (min >= 2 || max >= 2) {
RepetitionWalker w;
if (w.Walk(stacktop_, kMaxRepeat) == 0) {
if (w.Walk(stacktop_, maximum_repeat_count) == 0) {
status_->set_code(kRegexpRepeatSize);
status_->set_error_arg(s);
return false;
Expand Down
8 changes: 4 additions & 4 deletions re2/prog.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,6 @@ class Prog {
// FOR TESTING OR EXPERIMENTAL PURPOSES ONLY.
int BuildEntireDFA(MatchKind kind, const DFAStateCallback& cb);

// Controls whether the DFA should bail out early if the NFA would be faster.
// FOR TESTING ONLY.
static void TEST_dfa_should_bail_when_slow(bool b);

// Compute bytemap.
void ComputeByteMap();

Expand Down Expand Up @@ -402,6 +398,10 @@ class Prog {
// Computes hints for ByteRange instructions in [begin, end).
void ComputeHints(std::vector<Inst>* flat, int begin, int end);

// Controls whether the DFA should bail out early if the NFA would be faster.
// FOR TESTING ONLY.
static void TESTING_ONLY_set_dfa_should_bail_when_slow(bool b);

private:
friend class Compiler;

Expand Down
4 changes: 4 additions & 0 deletions re2/regexp.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,10 @@ class Regexp {
// regardless of the return value.
bool RequiredPrefixForAccel(std::string* prefix, bool* foldcase);

// Controls the maximum repeat count permitted by the parser.
// FOR FUZZING ONLY.
static void FUZZING_ONLY_set_maximum_repeat_count(int i);

private:
// Constructor allocates vectors as appropriate for operator.
explicit Regexp(RegexpOp op, ParseFlags parse_flags);
Expand Down
8 changes: 4 additions & 4 deletions re2/testing/dfa_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ TEST(SingleThreaded, SearchDFA) {
// NFA implementation instead. (The DFA loses its speed advantage
// if it can't get a good cache hit rate.)
// Tell the DFA to trudge along instead.
Prog::TEST_dfa_should_bail_when_slow(false);
Prog::TESTING_ONLY_set_dfa_should_bail_when_slow(false);
state_cache_resets = 0;
search_failures = 0;

Expand Down Expand Up @@ -194,7 +194,7 @@ TEST(SingleThreaded, SearchDFA) {
re->Decref();

// Reset to original behaviour.
Prog::TEST_dfa_should_bail_when_slow(true);
Prog::TESTING_ONLY_set_dfa_should_bail_when_slow(true);
ASSERT_GT(state_cache_resets, 0);
ASSERT_EQ(search_failures, 0);
}
Expand All @@ -218,7 +218,7 @@ static void DoSearch(Prog* prog, const StringPiece& match,
}

TEST(Multithreaded, SearchDFA) {
Prog::TEST_dfa_should_bail_when_slow(false);
Prog::TESTING_ONLY_set_dfa_should_bail_when_slow(false);
state_cache_resets = 0;
search_failures = 0;

Expand Down Expand Up @@ -259,7 +259,7 @@ TEST(Multithreaded, SearchDFA) {
re->Decref();

// Reset to original behaviour.
Prog::TEST_dfa_should_bail_when_slow(true);
Prog::TESTING_ONLY_set_dfa_should_bail_when_slow(true);
ASSERT_GT(state_cache_resets, 0);
ASSERT_EQ(search_failures, 0);
}
Expand Down

0 comments on commit 3a95199

Please sign in to comment.