From ee7c3c16cd10834b4d2787bccfa58ff0a469db55 Mon Sep 17 00:00:00 2001 From: Teemu Koponen Date: Fri, 16 Oct 2020 10:40:39 -0700 Subject: [PATCH] wasm: Compile the re2 into the library. Disable all the logging in the library to avoid a dependency to C++ streams of the standard library. Signed-off-by: Teemu Koponen --- wasm/Makefile | 19 +++++++++++++++-- wasm/src/re2/re2/bitstate.cc | 4 ++++ wasm/src/re2/re2/compile.cc | 8 +++++++ wasm/src/re2/re2/dfa.cc | 20 +++++++++++++++++ wasm/src/re2/re2/nfa.cc | 12 +++++++++++ wasm/src/re2/re2/onepass.cc | 16 ++++++++++++++ wasm/src/re2/re2/parse.cc | 18 ++++++++++++++++ wasm/src/re2/re2/prog.cc | 10 +++++++++ wasm/src/re2/re2/re2.cc | 38 +++++++++++++++++++++++++++++++++ wasm/src/re2/re2/regexp.cc | 12 +++++++++++ wasm/src/re2/re2/simplify.cc | 14 ++++++++++++ wasm/src/re2/re2/stringpiece.cc | 2 ++ wasm/src/re2/re2/tostring.cc | 4 ++++ wasm/src/re2/re2/walker-inl.h | 4 ++++ wasm/src/re2/util/logging.h | 5 +++++ wasm/test.js | 2 +- 16 files changed, 185 insertions(+), 3 deletions(-) diff --git a/wasm/Makefile b/wasm/Makefile index 8346dd3044..92095405c7 100644 --- a/wasm/Makefile +++ b/wasm/Makefile @@ -35,6 +35,7 @@ CPPFLAGS += \ -I src/libc++ \ -I /usr/lib/llvm-11/include/c++/v1 \ -I /usr/lib/llvm-11/lib/clang/11.0.0/include \ + -I src/re2 \ -D_LIBCPP_HAS_NO_THREADS ifeq ($(DEBUG), 1) @@ -80,6 +81,8 @@ hack: $(shell mkdir -p $(WASM_OBJ_DIR)/src/lib) $(shell mkdir -p $(WASM_OBJ_DIR)/src/libmpdec) $(shell mkdir -p $(WASM_OBJ_DIR)/src/libc++) +$(shell mkdir -p $(WASM_OBJ_DIR)/src/re2/re2) +$(shell mkdir -p $(WASM_OBJ_DIR)/src/re2/util) $(shell mkdir -p $(WASM_OBJ_DIR)/src) $(shell mkdir -p $(WASM_OBJ_DIR)/tests) @@ -87,18 +90,24 @@ SRCS := $(sort $(wildcard src/*.c)) LIB_SRCS := $(sort $(wildcard src/lib/*.c)) LIB_MPDEC_SRCS := $(sort $(wildcard src/libmpdec/*.c)) LIB_CPP_SRCS := $(sort $(wildcard src/libc++/*.cc)) +RE2_RE2_SRCS := $(sort $(wildcard src/re2/re2/*.cc)) +RE2_UTIL_SRCS := $(sort $(wildcard src/re2/util/*.cc)) TEST_SRCS := $(sort $(wildcard tests/*.c)) -include $(patsubst %.c,$(WASM_OBJ_DIR)/%.d,$(SRCS)) -include $(patsubst %.c,$(WASM_OBJ_DIR)/%.d,$(LIB_SRCS)) -include $(patsubst %.c,$(WASM_OBJ_DIR)/%.d,$(LIB_MPDEC_SRCS)) -include $(patsubst %.cc,$(WASM_OBJ_DIR)/%.d,$(LIB_CPP_SRCS)) +-include $(patsubst %.cc,$(WASM_OBJ_DIR)/%.d,$(RE2_RE2_SRCS)) +-include $(patsubst %.cc,$(WASM_OBJ_DIR)/%.d,$(RE2_UTIL_SRCS)) -include $(patsubst %.c,$(WASM_OBJ_DIR)/%.d,$(TEST_SRCS)) OBJS := $(patsubst %.c, $(WASM_OBJ_DIR)/%.wasm, $(SRCS)) LIB_OBJS := $(patsubst %.c, $(WASM_OBJ_DIR)/%.wasm, $(LIB_SRCS)) LIB_MPDEC_OBJS := $(patsubst %.c, $(WASM_OBJ_DIR)/%.wasm, $(LIB_MPDEC_SRCS)) LIB_CPP_OBJS := $(patsubst %.cc, $(WASM_OBJ_DIR)/%.wasm, $(LIB_CPP_SRCS)) +RE2_RE2_OBJS := $(patsubst %.cc, $(WASM_OBJ_DIR)/%.wasm, $(RE2_RE2_SRCS)) +RE2_UTIL_OBJS := $(patsubst %.cc, $(WASM_OBJ_DIR)/%.wasm, $(RE2_UTIL_SRCS)) TEST_OBJS := $(patsubst %.c, $(WASM_OBJ_DIR)/%.wasm, $(TEST_SRCS)) $(OBJS): $(WASM_OBJ_DIR)/src/%.wasm: src/%.c @@ -113,10 +122,16 @@ $(LIB_MPDEC_OBJS): $(WASM_OBJ_DIR)/src/libmpdec/%.wasm: src/libmpdec/%.c $(LIB_CPP_OBJS): $(WASM_OBJ_DIR)/src/libc++/%.wasm: src/libc++/%.cc $(CXX) $(CPPFLAGS) -c -o $@ $< +$(RE2_RE2_OBJS): $(WASM_OBJ_DIR)/src/re2/re2/%.wasm: src/re2/re2/%.cc + $(CXX) $(CPPFLAGS) -c -o $@ $< + +$(RE2_UTIL_OBJS): $(WASM_OBJ_DIR)/src/re2/util/%.wasm: src/re2/util/%.cc + $(CXX) $(CPPFLAGS) -c -o $@ $< + $(TEST_OBJS): $(WASM_OBJ_DIR)/tests/%.wasm: tests/%.c $(CC) $(CFLAGS) -I src -c -o $@ $< -$(WASM_OBJ_DIR)/opa.wasm: $(OBJS) $(LIB_OBJS) $(LIB_MPDEC_OBJS) $(LIB_CPP_OBJS) +$(WASM_OBJ_DIR)/opa.wasm: $(OBJS) $(LIB_OBJS) $(LIB_MPDEC_OBJS) $(LIB_CPP_OBJS) $(RE2_RE2_OBJS) $(RE2_UTIL_OBJS) wasm-ld-11 \ --allow-undefined-file=src/undefined.symbols \ --import-memory \ @@ -125,7 +140,7 @@ $(WASM_OBJ_DIR)/opa.wasm: $(OBJS) $(LIB_OBJS) $(LIB_MPDEC_OBJS) $(LIB_CPP_OBJS) -o $@ $^ @wasm2wat $(WASM_OBJ_DIR)/opa.wasm > $(WASM_OBJ_DIR)/opa.wast -$(WASM_OBJ_DIR)/opa-test.wasm: $(OBJS) $(LIB_OBJS) $(LIB_MPDEC_OBJS) $(LIB_CPP_OBJS) $(TEST_OBJS) +$(WASM_OBJ_DIR)/opa-test.wasm: $(OBJS) $(LIB_OBJS) $(LIB_MPDEC_OBJS) $(LIB_CPP_OBJS) $(RE2_RE2_OBJS) $(RE2_UTIL_OBJS) $(TEST_OBJS) @cat src/undefined.symbols tests/undefined.symbols > _obj/undefined.symbols @wasm-ld-11 \ --allow-undefined-file=_obj/undefined.symbols \ diff --git a/wasm/src/re2/re2/bitstate.cc b/wasm/src/re2/re2/bitstate.cc index 320d1eea15..cf5815db62 100644 --- a/wasm/src/re2/re2/bitstate.cc +++ b/wasm/src/re2/re2/bitstate.cc @@ -108,9 +108,11 @@ void BitState::Push(int id, const char* p) { if (njob_ >= job_.size()) { GrowStack(); if (njob_ >= job_.size()) { +#if 0 LOG(DFATAL) << "GrowStack() failed: " << "njob_ = " << njob_ << ", " << "job_.size() = " << job_.size(); +#endif return; } } @@ -168,7 +170,9 @@ bool BitState::TrySearch(int id0, const char* p0) { Prog::Inst* ip = prog_->inst(id); switch (ip->opcode()) { default: +#if 0 LOG(DFATAL) << "Unexpected opcode: " << ip->opcode(); +#endif return false; case kInstFail: diff --git a/wasm/src/re2/re2/compile.cc b/wasm/src/re2/re2/compile.cc index 7a9de07281..fe7b1f8860 100644 --- a/wasm/src/re2/re2/compile.cc +++ b/wasm/src/re2/re2/compile.cc @@ -592,7 +592,9 @@ Frag Compiler::FindByteRange(int root, int id) { return NoMatch(); } +#if 0 LOG(DFATAL) << "should never happen"; +#endif return NoMatch(); } @@ -769,7 +771,9 @@ void Compiler::AddRuneRangeUTF8(Rune lo, Rune hi, bool foldcase) { // Should not be called. Frag Compiler::Copy(Frag arg) { // We're using WalkExponential; there should be no copying. +#if 0 LOG(DFATAL) << "Compiler::Copy called!"; +#endif failed_ = true; return NoMatch(); } @@ -896,7 +900,9 @@ Frag Compiler::PostVisit(Regexp* re, Frag, Frag, Frag* child_frags, CharClass* cc = re->cc(); if (cc->empty()) { // This can't happen. +#if 0 LOG(DFATAL) << "No ranges in char class"; +#endif failed_ = true; return NoMatch(); } @@ -954,7 +960,9 @@ Frag Compiler::PostVisit(Regexp* re, Frag, Frag, Frag* child_frags, case kRegexpNoWordBoundary: return EmptyWidth(kEmptyNonWordBoundary); } +#if 0 LOG(DFATAL) << "Missing case in Compiler: " << re->op(); +#endif failed_ = true; return NoMatch(); } diff --git a/wasm/src/re2/re2/dfa.cc b/wasm/src/re2/re2/dfa.cc index 3f6571d3a5..7ad547d27e 100644 --- a/wasm/src/re2/re2/dfa.cc +++ b/wasm/src/re2/re2/dfa.cc @@ -847,7 +847,9 @@ void DFA::AddToQueue(Workq* q, int id, uint32_t flag) { Prog::Inst* ip = prog_->inst(id); switch (ip->opcode()) { default: +#if 0 LOG(DFATAL) << "unhandled opcode: " << ip->opcode(); +#endif break; case kInstByteRange: // just save these on the queue @@ -936,7 +938,9 @@ void DFA::RunWorkqOnByte(Workq* oldq, Workq* newq, Prog::Inst* ip = prog_->inst(id); switch (ip->opcode()) { default: +#if 0 LOG(DFATAL) << "unhandled opcode: " << ip->opcode(); +#endif break; case kInstFail: // never succeeds @@ -1004,14 +1008,20 @@ DFA::State* DFA::RunStateOnByte(State* state, int c) { return FullMatchState; } if (state == DeadState) { +#if 0 LOG(DFATAL) << "DeadState in RunStateOnByte"; +#endif return NULL; } if (state == NULL) { +#if 0 LOG(DFATAL) << "NULL state in RunStateOnByte"; +#endif return NULL; } +#if 0 LOG(DFATAL) << "Unexpected special state in RunStateOnByte"; +#endif return NULL; } @@ -1241,8 +1251,10 @@ DFA::State* DFA::StateSaver::Restore() { return special_; MutexLock l(&dfa_->mutex_); State* s = dfa_->CachedState(inst_, ninst_, flag_); +#if 0 if (s == NULL) LOG(DFATAL) << "StateSaver failed to restore state."; +#endif return s; } @@ -1432,7 +1444,9 @@ inline bool DFA::InlinedSearchLoop(SearchParams* params) { } ns = RunStateOnByteUnlocked(s, c); if (ns == NULL) { +#if 0 LOG(DFATAL) << "RunStateOnByteUnlocked failed after ResetCache"; +#endif params->failed = true; return false; } @@ -1504,7 +1518,9 @@ inline bool DFA::InlinedSearchLoop(SearchParams* params) { } ns = RunStateOnByteUnlocked(s, lastbyte); if (ns == NULL) { +#if 0 LOG(DFATAL) << "RunStateOnByteUnlocked failed after Reset"; +#endif params->failed = true; return false; } @@ -1621,7 +1637,9 @@ bool DFA::AnalyzeSearch(SearchParams* params) { // Sanity check: make sure that text lies within context. if (text.begin() < context.begin() || text.end() > context.end()) { +#if 0 LOG(DFATAL) << "context does not contain text"; +#endif params->start = DeadState; return true; } @@ -1668,7 +1686,9 @@ bool DFA::AnalyzeSearch(SearchParams* params) { if (!AnalyzeSearchHelper(params, info, flags)) { ResetCache(params->cache_lock); if (!AnalyzeSearchHelper(params, info, flags)) { +#if 0 LOG(DFATAL) << "Failed to analyze start state."; +#endif params->failed = true; return false; } diff --git a/wasm/src/re2/re2/nfa.cc b/wasm/src/re2/re2/nfa.cc index 19ee08de21..2a8392f149 100644 --- a/wasm/src/re2/re2/nfa.cc +++ b/wasm/src/re2/re2/nfa.cc @@ -239,7 +239,9 @@ void NFA::AddToThreadq(Threadq* q, int id0, int c, const StringPiece& context, Prog::Inst* ip = prog_->inst(id); switch (ip->opcode()) { default: +#if 0 LOG(DFATAL) << "unhandled " << ip->opcode() << " in AddToThreadq"; +#endif break; case kInstFail: @@ -351,7 +353,9 @@ int NFA::Step(Threadq* runq, Threadq* nextq, int c, const StringPiece& context, switch (ip->opcode()) { default: // Should only see the values handled below. +#if 0 LOG(DFATAL) << "Unhandled " << ip->opcode() << " in step"; +#endif break; case kInstByteRange: @@ -457,7 +461,9 @@ bool NFA::Search(const StringPiece& text, const StringPiece& const_context, // Sanity check: make sure that text lies within context. if (text.begin() < context.begin() || text.end() > context.end()) { +#if 0 LOG(DFATAL) << "context does not contain text"; +#endif return false; } @@ -472,7 +478,9 @@ bool NFA::Search(const StringPiece& text, const StringPiece& const_context, } if (nsubmatch < 0) { +#if 0 LOG(DFATAL) << "Bad args: nsubmatch=" << nsubmatch; +#endif return false; } @@ -540,7 +548,9 @@ bool NFA::Search(const StringPiece& text, const StringPiece& const_context, Prog::Inst* ip = prog_->inst(id); switch (ip->opcode()) { default: +#if 0 LOG(DFATAL) << "Unexpected opcode in short circuit: " << ip->opcode(); +#endif break; case kInstCapture: @@ -671,7 +681,9 @@ void Prog::Fanout(SparseArray* fanout) { Prog::Inst* ip = inst(id); switch (ip->opcode()) { default: +#if 0 LOG(DFATAL) << "unhandled " << ip->opcode() << " in Prog::Fanout()"; +#endif break; case kInstByteRange: diff --git a/wasm/src/re2/re2/onepass.cc b/wasm/src/re2/re2/onepass.cc index 66a62d94b0..d82524a294 100644 --- a/wasm/src/re2/re2/onepass.cc +++ b/wasm/src/re2/re2/onepass.cc @@ -216,7 +216,9 @@ bool Prog::SearchOnePass(const StringPiece& text, Anchor anchor, MatchKind kind, StringPiece* match, int nmatch) { if (anchor != kAnchored && kind != kFullMatch) { +#if 0 LOG(DFATAL) << "Cannot use SearchOnePass for unanchored matches."; +#endif return false; } @@ -444,7 +446,9 @@ bool Prog::IsOnePass() { Prog::Inst* ip = inst(id); switch (ip->opcode()) { default: +#if 0 LOG(DFATAL) << "unhandled opcode: " << ip->opcode(); +#endif break; case kInstAltMatch: @@ -461,9 +465,11 @@ bool Prog::IsOnePass() { int nextindex = nodebyid[ip->out()]; if (nextindex == -1) { if (nalloc >= maxnodes) { +#if 0 if (ExtraDebug) LOG(ERROR) << StringPrintf( "Not OnePass: hit node limit %d >= %d", nalloc, maxnodes); +#endif goto fail; } nextindex = nalloc; @@ -486,9 +492,11 @@ bool Prog::IsOnePass() { if ((act & kImpossible) == kImpossible) { node->action[b] = newact; } else if (act != newact) { +#if 0 if (ExtraDebug) LOG(ERROR) << StringPrintf( "Not OnePass: conflict on byte %#x at state %d", c, *it); +#endif goto fail; } } @@ -507,9 +515,11 @@ bool Prog::IsOnePass() { if ((act & kImpossible) == kImpossible) { node->action[b] = newact; } else if (act != newact) { +#if 0 if (ExtraDebug) LOG(ERROR) << StringPrintf( "Not OnePass: conflict on byte %#x at state %d", c, *it); +#endif goto fail; } } @@ -548,9 +558,11 @@ bool Prog::IsOnePass() { // If already on work queue, (1) is violated: bail out. if (!AddQ(&workq, ip->out())) { +#if 0 if (ExtraDebug) LOG(ERROR) << StringPrintf( "Not OnePass: multiple paths %d -> %d", *it, ip->out()); +#endif goto fail; } id = ip->out(); @@ -559,9 +571,11 @@ bool Prog::IsOnePass() { case kInstMatch: if (matched) { // (3) is violated +#if 0 if (ExtraDebug) LOG(ERROR) << StringPrintf( "Not OnePass: multiple matches from %d", *it); +#endif goto fail; } matched = true; @@ -581,6 +595,7 @@ bool Prog::IsOnePass() { } } +#if 0 if (ExtraDebug) { // For debugging, dump one-pass NFA to LOG(ERROR). LOG(ERROR) << "bytemap:\n" << DumpByteMap(); LOG(ERROR) << "prog:\n" << Dump(); @@ -610,6 +625,7 @@ bool Prog::IsOnePass() { } LOG(ERROR) << "nodes:\n" << dump; } +#endif dfa_mem_ -= nalloc*statesize; onepass_nodes_ = PODArray(nalloc*statesize); diff --git a/wasm/src/re2/re2/parse.cc b/wasm/src/re2/re2/parse.cc index 3bba6137f4..007aba16a0 100644 --- a/wasm/src/re2/re2/parse.cc +++ b/wasm/src/re2/re2/parse.cc @@ -348,7 +348,9 @@ static void AddFoldedRange(CharClassBuilder* cc, Rune lo, Rune hi, int depth) { // current Unicode tables. make_unicode_casefold.py checks that // the cycles are not too long, and we double-check here using depth. if (depth > 10) { +#if 0 LOG(DFATAL) << "AddFoldedRange recurses too much."; +#endif return; } @@ -557,8 +559,10 @@ int RepetitionWalker::PostVisit(Regexp* re, int parent_arg, int pre_arg, int RepetitionWalker::ShortVisit(Regexp* re, int parent_arg) { // Should never be called: we use Walk(), not WalkExponential(). +#if 0 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION LOG(DFATAL) << "RepetitionWalker::ShortVisit called"; +#endif #endif return 0; } @@ -844,7 +848,9 @@ void Regexp::RemoveLeadingString(Regexp* re, int n) { case 0: case 1: // Impossible. +#if 0 LOG(DFATAL) << "Concat of " << re->nsub(); +#endif re->submany_ = NULL; re->op_ = kRegexpEmptyMatch; break; @@ -974,7 +980,9 @@ int Regexp::FactorAlternation(Regexp** sub, int nsub, ParseFlags flags) { i += iter->nsub; break; default: +#if 0 LOG(DFATAL) << "unknown round: " << round; +#endif break; } // If we are done, copy until the end of sub. @@ -1013,7 +1021,9 @@ int Regexp::FactorAlternation(Regexp** sub, int nsub, ParseFlags flags) { continue; } default: +#if 0 LOG(DFATAL) << "unknown round: " << round; +#endif break; } @@ -1180,8 +1190,10 @@ void FactorAlternationImpl::Round3(Regexp** sub, int nsub, } else if (re->op() == kRegexpLiteral) { ccb.AddRangeFlags(re->rune(), re->rune(), re->parse_flags()); } else { +#if 0 LOG(DFATAL) << "RE2: unexpected op: " << re->op() << " " << re->ToString(); +#endif } re->Decref(); } @@ -1439,7 +1451,9 @@ static int UnHex(int c) { return c - 'A' + 10; if ('a' <= c && c <= 'f') return c - 'a' + 10; +#if 0 LOG(DFATAL) << "Bad hex digit " << c; +#endif return 0; } @@ -1585,7 +1599,9 @@ static bool ParseEscape(StringPiece* s, Rune* rp, // return true; } +#if 0 LOG(DFATAL) << "Not reached in ParseEscape."; +#endif BadEscape: // Unrecognized escape sequence. @@ -2040,7 +2056,9 @@ bool Regexp::ParseState::ParsePerlFlags(StringPiece* s) { // Caller is supposed to check this. if (!(flags_ & PerlX) || t.size() < 2 || t[0] != '(' || t[1] != '?') { +#if 0 LOG(DFATAL) << "Bad call to ParseState::ParsePerlFlags"; +#endif status_->set_code(kRegexpInternalError); return false; } diff --git a/wasm/src/re2/re2/prog.cc b/wasm/src/re2/re2/prog.cc index ac9c085240..05d4b0dcd0 100644 --- a/wasm/src/re2/re2/prog.cc +++ b/wasm/src/re2/re2/prog.cc @@ -197,7 +197,9 @@ static bool IsMatch(Prog* prog, Prog::Inst* ip) { for (;;) { switch (ip->opcode()) { default: +#if 0 LOG(DFATAL) << "Unexpected opcode in IsMatch: " << ip->opcode(); +#endif return false; case kInstAlt: @@ -509,12 +511,14 @@ void Prog::ComputeByteMap() { builder.Build(bytemap_, &bytemap_range_); +#if 0 if (0) { // For debugging, use trivial bytemap. LOG(ERROR) << "Using trivial bytemap."; for (int i = 0; i < 256; i++) bytemap_[i] = static_cast(i); bytemap_range_ = 256; } +#endif } // Prog::Flatten() implements a graph rewriting algorithm. @@ -668,7 +672,9 @@ void Prog::MarkSuccessors(SparseArray* rootmap, Inst* ip = inst(id); switch (ip->opcode()) { default: +#if 0 LOG(DFATAL) << "unhandled opcode: " << ip->opcode(); +#endif break; case kInstAltMatch: @@ -728,7 +734,9 @@ void Prog::MarkDominator(int root, SparseArray* rootmap, Inst* ip = inst(id); switch (ip->opcode()) { default: +#if 0 LOG(DFATAL) << "unhandled opcode: " << ip->opcode(); +#endif break; case kInstAltMatch: @@ -795,7 +803,9 @@ void Prog::EmitList(int root, SparseArray* rootmap, Inst* ip = inst(id); switch (ip->opcode()) { default: +#if 0 LOG(DFATAL) << "unhandled opcode: " << ip->opcode(); +#endif break; case kInstAltMatch: diff --git a/wasm/src/re2/re2/re2.cc b/wasm/src/re2/re2/re2.cc index 85ba1f4ecd..5b9fd43197 100644 --- a/wasm/src/re2/re2/re2.cc +++ b/wasm/src/re2/re2/re2.cc @@ -130,8 +130,10 @@ int RE2::Options::ParseFlags() const { int flags = Regexp::ClassNL; switch (encoding()) { default: +#if 0 if (log_errors()) LOG(ERROR) << "Unknown encoding " << encoding(); +#endif break; case RE2::Options::EncodingUTF8: break; @@ -201,10 +203,12 @@ void RE2::Init(const StringPiece& pattern, const Options& options) { static_cast(options_.ParseFlags()), &status); if (entire_regexp_ == NULL) { +#if 0 if (options_.log_errors()) { LOG(ERROR) << "Error parsing '" << trunc(pattern_) << "': " << status.Text(); } +#endif error_ = new std::string(status.Text()); error_code_ = RegexpErrorToRE2(status.code()); error_arg_ = std::string(status.error_arg()); @@ -222,8 +226,10 @@ void RE2::Init(const StringPiece& pattern, const Options& options) { // Prog has two DFAs but the reverse prog has one. prog_ = suffix_regexp_->CompileToProg(options_.max_mem()*2/3); if (prog_ == NULL) { +#if 0 if (options_.log_errors()) LOG(ERROR) << "Error compiling '" << trunc(pattern_) << "'"; +#endif error_ = new std::string("pattern too large - compile failed"); error_code_ = RE2::ErrorPatternTooLarge; return; @@ -248,8 +254,10 @@ re2::Prog* RE2::ReverseProg() const { re->rprog_ = re->suffix_regexp_->CompileToReverseProg(re->options_.max_mem() / 3); if (re->rprog_ == NULL) { +#if 0 if (re->options_.log_errors()) LOG(ERROR) << "Error reverse compiling '" << trunc(re->pattern_) << "'"; +#endif // We no longer touch error_ and error_code_ because failing to compile // the reverse Prog is not a showstopper: falling back to NFA execution // is fine. More importantly, an RE2 object is supposed to be logically @@ -620,17 +628,21 @@ bool RE2::Match(const StringPiece& text, StringPiece* submatch, int nsubmatch) const { if (!ok()) { +#if 0 if (options_.log_errors()) LOG(ERROR) << "Invalid RE2: " << *error_; +#endif return false; } if (startpos > endpos || endpos > text.size()) { +#if 0 if (options_.log_errors()) LOG(ERROR) << "RE2: invalid startpos, endpos pair. [" << "startpos: " << startpos << ", " << "endpos: " << endpos << ", " << "text size: " << text.size() << "]"; +#endif return false; } @@ -707,7 +719,9 @@ bool RE2::Match(const StringPiece& text, bool skipped_test = false; switch (re_anchor) { default: +#if 0 LOG(DFATAL) << "Unexpected re_anchor value: " << re_anchor; +#endif return false; case UNANCHORED: { @@ -724,12 +738,14 @@ bool RE2::Match(const StringPiece& text, if (!prog->SearchDFA(subtext, text, Prog::kAnchored, Prog::kLongestMatch, matchp, &dfa_failed, NULL)) { if (dfa_failed) { +#if 0 if (options_.log_errors()) LOG(ERROR) << "DFA out of memory: " << "pattern length " << pattern_.size() << ", " << "program size " << prog->size() << ", " << "list count " << prog->list_count() << ", " << "bytemap range " << prog->bytemap_range(); +#endif // Fall back to NFA below. skipped_test = true; break; @@ -744,12 +760,14 @@ bool RE2::Match(const StringPiece& text, if (!prog_->SearchDFA(subtext, text, anchor, kind, matchp, &dfa_failed, NULL)) { if (dfa_failed) { +#if 0 if (options_.log_errors()) LOG(ERROR) << "DFA out of memory: " << "pattern length " << pattern_.size() << ", " << "program size " << prog_->size() << ", " << "list count " << prog_->list_count() << ", " << "bytemap range " << prog_->bytemap_range(); +#endif // Fall back to NFA below. skipped_test = true; break; @@ -770,18 +788,22 @@ bool RE2::Match(const StringPiece& text, if (!prog->SearchDFA(match, text, Prog::kAnchored, Prog::kLongestMatch, &match, &dfa_failed, NULL)) { if (dfa_failed) { +#if 0 if (options_.log_errors()) LOG(ERROR) << "DFA out of memory: " << "pattern length " << pattern_.size() << ", " << "program size " << prog->size() << ", " << "list count " << prog->list_count() << ", " << "bytemap range " << prog->bytemap_range(); +#endif // Fall back to NFA below. skipped_test = true; break; } +#if 0 if (options_.log_errors()) LOG(ERROR) << "SearchDFA inconsistency"; +#endif return false; } break; @@ -812,12 +834,14 @@ bool RE2::Match(const StringPiece& text, if (!prog_->SearchDFA(subtext, text, anchor, kind, &match, &dfa_failed, NULL)) { if (dfa_failed) { +#if 0 if (options_.log_errors()) LOG(ERROR) << "DFA out of memory: " << "pattern length " << pattern_.size() << ", " << "program size " << prog_->size() << ", " << "list count " << prog_->list_count() << ", " << "bytemap range " << prog_->bytemap_range(); +#endif // Fall back to NFA below. skipped_test = true; break; @@ -848,21 +872,27 @@ bool RE2::Match(const StringPiece& text, if (can_one_pass && anchor != Prog::kUnanchored) { if (!prog_->SearchOnePass(subtext1, text, anchor, kind, submatch, ncap)) { +#if 0 if (!skipped_test && options_.log_errors()) LOG(ERROR) << "SearchOnePass inconsistency"; +#endif return false; } } else if (can_bit_state && subtext1.size() <= bit_state_text_max) { if (!prog_->SearchBitState(subtext1, text, anchor, kind, submatch, ncap)) { +#if 0 if (!skipped_test && options_.log_errors()) LOG(ERROR) << "SearchBitState inconsistency"; +#endif return false; } } else { if (!prog_->SearchNFA(subtext1, text, anchor, kind, submatch, ncap)) { +#if 0 if (!skipped_test && options_.log_errors()) LOG(ERROR) << "SearchNFA inconsistency"; +#endif return false; } } @@ -886,8 +916,10 @@ bool RE2::DoMatch(const StringPiece& text, const Arg* const* args, int n) const { if (!ok()) { +#if 0 if (options_.log_errors()) LOG(ERROR) << "Invalid RE2: " << *error_; +#endif return false; } @@ -1018,10 +1050,12 @@ bool RE2::Rewrite(std::string* out, if (isdigit(c)) { int n = (c - '0'); if (n >= veclen) { +#if 0 if (options_.log_errors()) { LOG(ERROR) << "invalid substitution \\" << n << " from " << veclen << " groups"; } +#endif return false; } StringPiece snip = vec[n]; @@ -1030,14 +1064,17 @@ bool RE2::Rewrite(std::string* out, } else if (c == '\\') { out->push_back('\\'); } else { +#if 0 if (options_.log_errors()) LOG(ERROR) << "invalid rewrite pattern: " << rewrite.data(); +#endif return false; } } return true; } +#if 0 /***** Parsers for various types *****/ namespace re2_internal { @@ -1290,6 +1327,7 @@ bool Parse(const char* str, size_t n, unsigned long long* dest, int radix) { } } // namespace re2_internal +#endif namespace hooks { diff --git a/wasm/src/re2/re2/regexp.cc b/wasm/src/re2/re2/regexp.cc index 574780fbea..a614cbf8e5 100644 --- a/wasm/src/re2/re2/regexp.cc +++ b/wasm/src/re2/re2/regexp.cc @@ -44,8 +44,10 @@ Regexp::Regexp(RegexpOp op, ParseFlags parse_flags) // that could cause arbitrarily deep recursion, so // required Decref() to have handled them for us. Regexp::~Regexp() { +#if 0 if (nsub_ > 0) LOG(DFATAL) << "Regexp not destroyed."; +#endif switch (op_) { default: @@ -143,8 +145,10 @@ void Regexp::Destroy() { while (stack != NULL) { Regexp* re = stack; stack = re->down_; +#if 0 if (re->ref_ != 0) LOG(DFATAL) << "Bad reference count " << re->ref_; +#endif if (re->nsub_ > 0) { Regexp** subs = re->sub(); for (int i = 0; i < re->nsub_; i++) { @@ -405,7 +409,9 @@ static bool TopEqual(Regexp* a, Regexp* b) { } } +#if 0 LOG(DFATAL) << "Unexpected op in Regexp::Equal: " << a->op(); +#endif return 0; } @@ -545,8 +551,10 @@ class NumCapturesWalker : public Regexp::Walker { virtual Ignored ShortVisit(Regexp* re, Ignored ignored) { // Should never be called: we use Walk(), not WalkExponential(). +#if 0 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION LOG(DFATAL) << "NumCapturesWalker::ShortVisit called"; +#endif #endif return ignored; } @@ -593,8 +601,10 @@ class NamedCapturesWalker : public Regexp::Walker { virtual Ignored ShortVisit(Regexp* re, Ignored ignored) { // Should never be called: we use Walk(), not WalkExponential(). +#if 0 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION LOG(DFATAL) << "NamedCapturesWalker::ShortVisit called"; +#endif #endif return ignored; } @@ -637,8 +647,10 @@ class CaptureNamesWalker : public Regexp::Walker { virtual Ignored ShortVisit(Regexp* re, Ignored ignored) { // Should never be called: we use Walk(), not WalkExponential(). +#if 0 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION LOG(DFATAL) << "CaptureNamesWalker::ShortVisit called"; +#endif #endif return ignored; } diff --git a/wasm/src/re2/re2/simplify.cc b/wasm/src/re2/re2/simplify.cc index 663d5fcd45..d1798c47e2 100644 --- a/wasm/src/re2/re2/simplify.cc +++ b/wasm/src/re2/re2/simplify.cc @@ -94,7 +94,9 @@ bool Regexp::ComputeSimple() { case kRegexpRepeat: return false; } +#if 0 LOG(DFATAL) << "Case not handled in ComputeSimple: " << op_; +#endif return false; } @@ -221,8 +223,10 @@ Regexp* CoalesceWalker::Copy(Regexp* re) { Regexp* CoalesceWalker::ShortVisit(Regexp* re, Regexp* parent_arg) { // Should never be called: we use Walk(), not WalkExponential(). +#if 0 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION LOG(DFATAL) << "CoalesceWalker::ShortVisit called"; +#endif #endif return re->Incref(); } @@ -371,7 +375,9 @@ void CoalesceWalker::DoCoalesce(Regexp** r1ptr, Regexp** r2ptr) { break; default: +#if 0 LOG(DFATAL) << "DoCoalesce failed: r1->op() is " << r1->op(); +#endif nre->Decref(); return; } @@ -432,7 +438,9 @@ void CoalesceWalker::DoCoalesce(Regexp** r1ptr, Regexp** r2ptr) { } default: +#if 0 LOG(DFATAL) << "DoCoalesce failed: r2->op() is " << r2->op(); +#endif nre->Decref(); return; } @@ -447,8 +455,10 @@ Regexp* SimplifyWalker::Copy(Regexp* re) { Regexp* SimplifyWalker::ShortVisit(Regexp* re, Regexp* parent_arg) { // Should never be called: we use Walk(), not WalkExponential(). +#if 0 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION LOG(DFATAL) << "SimplifyWalker::ShortVisit called"; +#endif #endif return re->Incref(); } @@ -564,7 +574,9 @@ Regexp* SimplifyWalker::PostVisit(Regexp* re, } } +#if 0 LOG(ERROR) << "Simplify case not handled: " << re->op(); +#endif return re->Incref(); } @@ -641,7 +653,9 @@ Regexp* SimplifyWalker::SimplifyRepeat(Regexp* re, int min, int max, if (nre == NULL) { // Some degenerate case, like min > max, or min < max < 0. // This shouldn't happen, because the parser rejects such regexps. +#if 0 LOG(DFATAL) << "Malformed repeat " << re->ToString() << " " << min << " " << max; +#endif return new Regexp(kRegexpNoMatch, f); } diff --git a/wasm/src/re2/re2/stringpiece.cc b/wasm/src/re2/re2/stringpiece.cc index 65608646db..01472c9e02 100644 --- a/wasm/src/re2/re2/stringpiece.cc +++ b/wasm/src/re2/re2/stringpiece.cc @@ -57,9 +57,11 @@ StringPiece::size_type StringPiece::rfind(char c, size_type pos) const { return npos; } +#if 0 std::ostream& operator<<(std::ostream& o, const StringPiece& p) { o.write(p.data(), p.size()); return o; } +#endif } // namespace re2 diff --git a/wasm/src/re2/re2/tostring.cc b/wasm/src/re2/re2/tostring.cc index 4545a92ddb..475a8c2bbb 100644 --- a/wasm/src/re2/re2/tostring.cc +++ b/wasm/src/re2/re2/tostring.cc @@ -101,8 +101,10 @@ int ToStringWalker::PreVisit(Regexp* re, int parent_arg, bool* stop) { case kRegexpCapture: t_->append("("); +#if 0 if (re->cap() == 0) LOG(DFATAL) << "kRegexpCapture cap() == 0"; +#endif if (re->name()) { t_->append("?P<"); t_->append(*re->name()); @@ -184,8 +186,10 @@ int ToStringWalker::PostVisit(Regexp* re, int parent_arg, int pre_arg, // at the end of their strings, so just remove the last one. if ((*t_)[t_->size()-1] == '|') t_->erase(t_->size()-1); +#if 0 else LOG(DFATAL) << "Bad final char: " << t_; +#endif if (prec < PrecAlternate) t_->append(")"); break; diff --git a/wasm/src/re2/re2/walker-inl.h b/wasm/src/re2/re2/walker-inl.h index 8e0f94667c..8a37f87155 100644 --- a/wasm/src/re2/re2/walker-inl.h +++ b/wasm/src/re2/re2/walker-inl.h @@ -146,7 +146,9 @@ template Regexp::Walker::~Walker() { // Logs DFATAL if stack is not already clear. template void Regexp::Walker::Reset() { if (!stack_.empty()) { +#if 0 LOG(DFATAL) << "Stack not empty."; +#endif while (!stack_.empty()) { delete[] stack_.top().child_args; stack_.pop(); @@ -159,7 +161,9 @@ template T Regexp::Walker::WalkInternal(Regexp* re, T top_arg, Reset(); if (re == NULL) { +#if 0 LOG(DFATAL) << "Walk NULL"; +#endif return top_arg; } diff --git a/wasm/src/re2/util/logging.h b/wasm/src/re2/util/logging.h index 5b2217f29c..b389b3bb52 100644 --- a/wasm/src/re2/util/logging.h +++ b/wasm/src/re2/util/logging.h @@ -10,8 +10,10 @@ #include #include #include +#if 0 #include #include +#endif #include "util/util.h" @@ -54,6 +56,7 @@ #define VLOG(x) if((x)>0){}else LOG_INFO.stream() +#if 0 class LogMessage { public: LogMessage(const char* file, int line) @@ -106,4 +109,6 @@ class LogMessageFatal : public LogMessage { #pragma warning(pop) #endif +#endif + #endif // UTIL_LOGGING_H_ diff --git a/wasm/test.js b/wasm/test.js index e44cf4d88a..9f33dd79ff 100644 --- a/wasm/test.js +++ b/wasm/test.js @@ -49,7 +49,7 @@ function report(passed, error, msg) { async function test(executable) { - const mem = new WebAssembly.Memory({ initial: 2 }); + const mem = new WebAssembly.Memory({ initial: 3 }); const addr2string = stringDecoder(mem); let cache = {};