Coverage Report

Created: 2025-11-01 07:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/duckdb/third_party/re2/re2/dfa.cc
Line
Count
Source
1
// Copyright 2008 The RE2 Authors.  All Rights Reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
5
// A DFA (deterministic finite automaton)-based regular expression search.
6
//
7
// The DFA search has two main parts: the construction of the automaton,
8
// which is represented by a graph of State structures, and the execution
9
// of the automaton over a given input string.
10
//
11
// The basic idea is that the State graph is constructed so that the
12
// execution can simply start with a state s, and then for each byte c in
13
// the input string, execute "s = s->next[c]", checking at each point whether
14
// the current s represents a matching state.
15
//
16
// The simple explanation just given does convey the essence of this code,
17
// but it omits the details of how the State graph gets constructed as well
18
// as some performance-driven optimizations to the execution of the automaton.
19
// All these details are explained in the comments for the code following
20
// the definition of class DFA.
21
//
22
// See http://swtch.com/~rsc/regexp/ for a very bare-bones equivalent.
23
24
#include <stddef.h>
25
#include <stdint.h>
26
#include <stdio.h>
27
#include <string.h>
28
#include <algorithm>
29
#include <atomic>
30
#include <deque>
31
#include <mutex>
32
#include <new>
33
#include <string>
34
#include <unordered_map>
35
#include <unordered_set>
36
#include <utility>
37
#include <vector>
38
39
#include "util/logging.h"
40
#include "util/mix.h"
41
#include "util/mutex.h"
42
#include "util/strutil.h"
43
#include "re2/pod_array.h"
44
#include "re2/prog.h"
45
#include "re2/re2.h"
46
#include "re2/sparse_set.h"
47
#include "re2/stringpiece.h"
48
49
// Silence "zero-sized array in struct/union" warning for DFA::State::next_.
50
#ifdef _MSC_VER
51
#pragma warning(disable: 4200)
52
#endif
53
54
namespace duckdb_re2 {
55
56
// Controls whether the DFA should bail out early if the NFA would be faster.
57
static bool dfa_should_bail_when_slow = true;
58
59
0
void Prog::TESTING_ONLY_set_dfa_should_bail_when_slow(bool b) {
60
0
  dfa_should_bail_when_slow = b;
61
0
}
62
63
// A DFA implementation of a regular expression program.
64
// Since this is entirely a forward declaration mandated by C++,
65
// some of the comments here are better understood after reading
66
// the comments in the sections that follow the DFA definition.
67
class DFA {
68
 public:
69
  DFA(Prog* prog, Prog::MatchKind kind, int64_t max_mem);
70
  ~DFA();
71
4.62k
  bool ok() const { return !init_failed_; }
72
0
  Prog::MatchKind kind() { return kind_; }
73
74
  // Searches for the regular expression in text, which is considered
75
  // as a subsection of context for the purposes of interpreting flags
76
  // like ^ and $ and \A and \z.
77
  // Returns whether a match was found.
78
  // If a match is found, sets *ep to the end point of the best match in text.
79
  // If "anchored", the match must begin at the start of text.
80
  // If "want_earliest_match", the match that ends first is used, not
81
  //   necessarily the best one.
82
  // If "run_forward" is true, the DFA runs from text.begin() to text.end().
83
  //   If it is false, the DFA runs from text.end() to text.begin(),
84
  //   returning the leftmost end of the match instead of the rightmost one.
85
  // If the DFA cannot complete the search (for example, if it is out of
86
  //   memory), it sets *failed and returns false.
87
  bool Search(const StringPiece& text, const StringPiece& context,
88
              bool anchored, bool want_earliest_match, bool run_forward,
89
              bool* failed, const char** ep, SparseSet* matches);
90
91
  // Builds out all states for the entire DFA.
92
  // If cb is not empty, it receives one callback per state built.
93
  // Returns the number of states built.
94
  // FOR TESTING OR EXPERIMENTAL PURPOSES ONLY.
95
  int BuildAllStates(const Prog::DFAStateCallback& cb);
96
97
  // Computes min and max for matching strings.  Won't return strings
98
  // bigger than maxlen.
99
  bool PossibleMatchRange(std::string* min, std::string* max, int maxlen);
100
101
  // These data structures are logically private, but C++ makes it too
102
  // difficult to mark them as such.
103
  class RWLocker;
104
  class StateSaver;
105
  class Workq;
106
107
  // A single DFA state.  The DFA is represented as a graph of these
108
  // States, linked by the next_ pointers.  If in state s and reading
109
  // byte c, the next state should be s->next_[c].
110
  struct State {
111
2.02k
    inline bool IsMatch() const { return (flag_ & kFlagMatch) != 0; }
112
113
    int* inst_;         // Instruction pointers in the state.
114
    int ninst_;         // # of inst_ pointers.
115
    uint32_t flag_;     // Empty string bitfield flags in effect on the way
116
                        // into this state, along with kFlagMatch if this
117
                        // is a matching state.
118
119
  // fixes from https://github.com/girishji/re2/commit/80b212f289c4ef75408b1510b9fc85e6cb9a447c
120
    std::atomic<State*> *next_;    // Outgoing arrows from State,
121
122
                        // one per input byte class
123
  };
124
125
  enum {
126
    kByteEndText = 256,         // imaginary byte at end of text
127
128
    kFlagEmptyMask = 0xFF,      // State.flag_: bits holding kEmptyXXX flags
129
    kFlagMatch = 0x0100,        // State.flag_: this is a matching state
130
    kFlagLastWord = 0x0200,     // State.flag_: last byte was a word char
131
    kFlagNeedShift = 16,        // needed kEmpty bits are or'ed in shifted left
132
  };
133
134
  struct StateHash {
135
1.44M
    size_t operator()(const State* a) const {
136
1.44M
      DCHECK(a != NULL);
137
1.44M
      HashMix mix(a->flag_);
138
2.92M
      for (int i = 0; i < a->ninst_; i++)
139
1.47M
        mix.Mix(a->inst_[i]);
140
1.44M
      mix.Mix(0);
141
1.44M
      return mix.get();
142
1.44M
    }
143
  };
144
145
  struct StateEqual {
146
158k
    bool operator()(const State* a, const State* b) const {
147
158k
      DCHECK(a != NULL);
148
158k
      DCHECK(b != NULL);
149
158k
      if (a == b)
150
0
        return true;
151
158k
      if (a->flag_ != b->flag_)
152
0
        return false;
153
158k
      if (a->ninst_ != b->ninst_)
154
0
        return false;
155
210k
      for (int i = 0; i < a->ninst_; i++)
156
51.9k
        if (a->inst_[i] != b->inst_[i])
157
0
          return false;
158
158k
      return true;
159
158k
    }
160
  };
161
162
  typedef std::unordered_set<State*, StateHash, StateEqual> StateSet;
163
164
 private:
165
  // Make it easier to swap in a scalable reader-writer mutex.
166
  using CacheMutex = Mutex;
167
168
  enum {
169
    // Indices into start_ for unanchored searches.
170
    // Add kStartAnchored for anchored searches.
171
    kStartBeginText = 0,          // text at beginning of context
172
    kStartBeginLine = 2,          // text at beginning of line
173
    kStartAfterWordChar = 4,      // text follows a word character
174
    kStartAfterNonWordChar = 6,   // text follows non-word character
175
    kMaxStart = 8,
176
177
    kStartAnchored = 1,
178
  };
179
180
  // Resets the DFA State cache, flushing all saved State* information.
181
  // Releases and reacquires cache_mutex_ via cache_lock, so any
182
  // State* existing before the call are not valid after the call.
183
  // Use a StateSaver to preserve important states across the call.
184
  // cache_mutex_.r <= L < mutex_
185
  // After: cache_mutex_.w <= L < mutex_
186
  void ResetCache(RWLocker* cache_lock);
187
188
  // Looks up and returns the State corresponding to a Workq.
189
  // L >= mutex_
190
  State* WorkqToCachedState(Workq* q, Workq* mq, uint32_t flag);
191
192
  // Looks up and returns a State matching the inst, ninst, and flag.
193
  // L >= mutex_
194
  State* CachedState(int* inst, int ninst, uint32_t flag);
195
196
  // Clear the cache entirely.
197
  // Must hold cache_mutex_.w or be in destructor.
198
  void ClearCache();
199
200
  // Converts a State into a Workq: the opposite of WorkqToCachedState.
201
  // L >= mutex_
202
  void StateToWorkq(State* s, Workq* q);
203
204
  // Runs a State on a given byte, returning the next state.
205
  State* RunStateOnByteUnlocked(State*, int);  // cache_mutex_.r <= L < mutex_
206
  State* RunStateOnByte(State*, int);          // L >= mutex_
207
208
  // Runs a Workq on a given byte followed by a set of empty-string flags,
209
  // producing a new Workq in nq.  If a match instruction is encountered,
210
  // sets *ismatch to true.
211
  // L >= mutex_
212
  void RunWorkqOnByte(Workq* q, Workq* nq,
213
                      int c, uint32_t flag, bool* ismatch);
214
215
  // Runs a Workq on a set of empty-string flags, producing a new Workq in nq.
216
  // L >= mutex_
217
  void RunWorkqOnEmptyString(Workq* q, Workq* nq, uint32_t flag);
218
219
  // Adds the instruction id to the Workq, following empty arrows
220
  // according to flag.
221
  // L >= mutex_
222
  void AddToQueue(Workq* q, int id, uint32_t flag);
223
224
  // For debugging, returns a text representation of State.
225
  static std::string DumpState(State* state);
226
227
  // For debugging, returns a text representation of a Workq.
228
  static std::string DumpWorkq(Workq* q);
229
230
  // Search parameters
231
  struct SearchParams {
232
    SearchParams(const StringPiece& text, const StringPiece& context,
233
                 RWLocker* cache_lock)
234
4.40k
      : text(text),
235
4.40k
        context(context),
236
4.40k
        anchored(false),
237
4.40k
        can_prefix_accel(false),
238
4.40k
        want_earliest_match(false),
239
4.40k
        run_forward(false),
240
4.40k
        start(NULL),
241
4.40k
        cache_lock(cache_lock),
242
4.40k
        failed(false),
243
4.40k
        ep(NULL),
244
4.40k
        matches(NULL) {}
245
246
    StringPiece text;
247
    StringPiece context;
248
    bool anchored;
249
    bool can_prefix_accel;
250
    bool want_earliest_match;
251
    bool run_forward;
252
    State* start;
253
    RWLocker* cache_lock;
254
    bool failed;     // "out" parameter: whether search gave up
255
    const char* ep;  // "out" parameter: end pointer for match
256
    SparseSet* matches;
257
258
   private:
259
    SearchParams(const SearchParams&) = delete;
260
    SearchParams& operator=(const SearchParams&) = delete;
261
  };
262
263
  // Before each search, the parameters to Search are analyzed by
264
  // AnalyzeSearch to determine the state in which to start.
265
  struct StartInfo {
266
36.9k
    StartInfo() : start(NULL) {}
267
    std::atomic<State*> start;
268
  };
269
270
  // Fills in params->start and params->can_prefix_accel using
271
  // the other search parameters.  Returns true on success,
272
  // false on failure.
273
  // cache_mutex_.r <= L < mutex_
274
  bool AnalyzeSearch(SearchParams* params);
275
  bool AnalyzeSearchHelper(SearchParams* params, StartInfo* info,
276
                           uint32_t flags);
277
278
  // The generic search loop, inlined to create specialized versions.
279
  // cache_mutex_.r <= L < mutex_
280
  // Might unlock and relock cache_mutex_ via params->cache_lock.
281
  template <bool can_prefix_accel,
282
            bool want_earliest_match,
283
            bool run_forward>
284
  inline bool InlinedSearchLoop(SearchParams* params);
285
286
  // The specialized versions of InlinedSearchLoop.  The three letters
287
  // at the ends of the name denote the true/false values used as the
288
  // last three parameters of InlinedSearchLoop.
289
  // cache_mutex_.r <= L < mutex_
290
  // Might unlock and relock cache_mutex_ via params->cache_lock.
291
  bool SearchFFF(SearchParams* params);
292
  bool SearchFFT(SearchParams* params);
293
  bool SearchFTF(SearchParams* params);
294
  bool SearchFTT(SearchParams* params);
295
  bool SearchTFF(SearchParams* params);
296
  bool SearchTFT(SearchParams* params);
297
  bool SearchTTF(SearchParams* params);
298
  bool SearchTTT(SearchParams* params);
299
300
  // The main search loop: calls an appropriate specialized version of
301
  // InlinedSearchLoop.
302
  // cache_mutex_.r <= L < mutex_
303
  // Might unlock and relock cache_mutex_ via params->cache_lock.
304
  bool FastSearchLoop(SearchParams* params);
305
306
307
  // Looks up bytes in bytemap_ but handles case c == kByteEndText too.
308
122M
  int ByteMap(int c) {
309
122M
    if (c == kByteEndText)
310
607k
      return prog_->bytemap_range();
311
122M
    return prog_->bytemap()[c];
312
122M
  }
313
314
  // Constant after initialization.
315
  Prog* prog_;              // The regular expression program to run.
316
  Prog::MatchKind kind_;    // The kind of DFA.
317
  bool init_failed_;        // initialization failed (out of memory)
318
319
  Mutex mutex_;  // mutex_ >= cache_mutex_.r
320
321
  // Scratch areas, protected by mutex_.
322
  Workq* q0_;             // Two pre-allocated work queues.
323
  Workq* q1_;
324
  PODArray<int> stack_;   // Pre-allocated stack for AddToQueue
325
326
  // State* cache.  Many threads use and add to the cache simultaneously,
327
  // holding cache_mutex_ for reading and mutex_ (above) when adding.
328
  // If the cache fills and needs to be discarded, the discarding is done
329
  // while holding cache_mutex_ for writing, to avoid interrupting other
330
  // readers.  Any State* pointers are only valid while cache_mutex_
331
  // is held.
332
  CacheMutex cache_mutex_;
333
  int64_t mem_budget_;     // Total memory budget for all States.
334
  int64_t state_budget_;   // Amount of memory remaining for new States.
335
  StateSet state_cache_;   // All States computed so far.
336
  StartInfo start_[kMaxStart];
337
338
  DFA(const DFA&) = delete;
339
  DFA& operator=(const DFA&) = delete;
340
};
341
342
// Shorthand for casting to uint8_t*.
343
0
static inline const uint8_t* BytePtr(const void* v) {
344
0
  return reinterpret_cast<const uint8_t*>(v);
345
0
}
346
347
// Work queues
348
349
// Marks separate thread groups of different priority
350
// in the work queue when in leftmost-longest matching mode.
351
//#define Mark (-1)
352
constexpr auto Mark = -1;
353
354
355
// Separates the match IDs from the instructions in inst_.
356
// Used only for "many match" DFA states.
357
//#define MatchSep (-2)
358
constexpr auto MatchSep = -2;
359
360
// Internally, the DFA uses a sparse array of
361
// program instruction pointers as a work queue.
362
// In leftmost longest mode, marks separate sections
363
// of workq that started executing at different
364
// locations in the string (earlier locations first).
365
class DFA::Workq : public SparseSet {
366
 public:
367
  // Constructor: n is number of normal slots, maxmark number of mark slots.
368
  Workq(int n, int maxmark) :
369
8.81k
    SparseSet(n+maxmark),
370
8.81k
    n_(n),
371
8.81k
    maxmark_(maxmark),
372
8.81k
    nextmark_(n),
373
8.81k
    last_was_mark_(true) {
374
8.81k
  }
375
376
38.1M
  bool is_mark(int i) { return i >= n_; }
377
378
660k
  int maxmark() { return maxmark_; }
379
380
67.5M
  void clear() {
381
67.5M
    SparseSet::clear();
382
67.5M
    nextmark_ = n_;
383
67.5M
  }
384
385
0
  void mark() {
386
0
    if (last_was_mark_)
387
0
      return;
388
0
    last_was_mark_ = false;
389
0
    SparseSet::insert_new(nextmark_++);
390
0
  }
391
392
33.7M
  int size() {
393
33.7M
    return n_ + maxmark_;
394
33.7M
  }
395
396
0
  void insert(int id) {
397
0
    if (contains(id))
398
0
      return;
399
0
    insert_new(id);
400
0
  }
401
402
38.4M
  void insert_new(int id) {
403
38.4M
    last_was_mark_ = false;
404
38.4M
    SparseSet::insert_new(id);
405
38.4M
  }
406
407
 private:
408
  int n_;                // size excluding marks
409
  int maxmark_;          // maximum number of marks
410
  int nextmark_;         // id of next mark
411
  bool last_was_mark_;   // last inserted was mark
412
413
  Workq(const Workq&) = delete;
414
  Workq& operator=(const Workq&) = delete;
415
};
416
417
DFA::DFA(Prog* prog, Prog::MatchKind kind, int64_t max_mem)
418
4.62k
  : prog_(prog),
419
4.62k
    kind_(kind),
420
4.62k
    init_failed_(false),
421
4.62k
    q0_(NULL),
422
4.62k
    q1_(NULL),
423
4.62k
    mem_budget_(max_mem) {
424
4.62k
  int nmark = 0;
425
4.62k
  if (kind_ == Prog::kLongestMatch)
426
4.62k
    nmark = prog_->size();
427
  // See DFA::AddToQueue() for why this is so.
428
4.62k
  int nstack = prog_->inst_count(kInstCapture) +
429
4.62k
               prog_->inst_count(kInstEmptyWidth) +
430
4.62k
               prog_->inst_count(kInstNop) +
431
4.62k
               nmark + 1;  // + 1 for start inst
432
433
  // Account for space needed for DFA, q0, q1, stack.
434
4.62k
  mem_budget_ -= sizeof(DFA);
435
4.62k
  mem_budget_ -= (prog_->size() + nmark) *
436
4.62k
                 (sizeof(int)+sizeof(int)) * 2;  // q0, q1
437
4.62k
  mem_budget_ -= nstack * sizeof(int);  // stack
438
4.62k
  if (mem_budget_ < 0) {
439
25
    init_failed_ = true;
440
25
    return;
441
25
  }
442
443
4.59k
  state_budget_ = mem_budget_;
444
445
  // Make sure there is a reasonable amount of working room left.
446
  // At minimum, the search requires room for two states in order
447
  // to limp along, restarting frequently.  We'll get better performance
448
  // if there is room for a larger number of states, say 20.
449
  // Note that a state stores list heads only, so we use the program
450
  // list count for the upper bound, not the program size.
451
4.59k
  int nnext = prog_->bytemap_range() + 1;  // + 1 for kByteEndText slot
452
4.59k
  int64_t one_state = sizeof(State) + nnext*sizeof(std::atomic<State*>) +
453
4.59k
                      (prog_->list_count()+nmark)*sizeof(int);
454
4.59k
  if (state_budget_ < 20*one_state) {
455
190
    init_failed_ = true;
456
190
    return;
457
190
  }
458
459
4.40k
  q0_ = new Workq(prog_->size(), nmark);
460
4.40k
  q1_ = new Workq(prog_->size(), nmark);
461
4.40k
  stack_ = PODArray<int>(nstack);
462
4.40k
}
463
464
4.62k
DFA::~DFA() {
465
4.62k
  delete q0_;
466
4.62k
  delete q1_;
467
4.62k
  ClearCache();
468
4.62k
}
469
470
// In the DFA state graph, s->next[c] == NULL means that the
471
// state has not yet been computed and needs to be.  We need
472
// a different special value to signal that s->next[c] is a
473
// state that can never lead to a match (and thus the search
474
// can be called off).  Hence DeadState.
475
33.5M
#define DeadState reinterpret_cast<State*>(1)
476
477
// Signals that the rest of the string matches no matter what it is.
478
444M
#define FullMatchState reinterpret_cast<State*>(2)
479
480
178M
#define SpecialStateMax FullMatchState
481
482
// Debugging printouts
483
484
// For debugging, returns a string representation of the work queue.
485
0
std::string DFA::DumpWorkq(Workq* q) {
486
0
  std::string s;
487
0
  const char* sep = "";
488
0
  for (Workq::iterator it = q->begin(); it != q->end(); ++it) {
489
0
    if (q->is_mark(*it)) {
490
0
      s += "|";
491
0
      sep = "";
492
0
    } else {
493
0
      s += StringPrintf("%s%d", sep, *it);
494
0
      sep = ",";
495
0
    }
496
0
  }
497
0
  return s;
498
0
}
499
500
// For debugging, returns a string representation of the state.
501
0
std::string DFA::DumpState(State* state) {
502
0
  if (state == NULL)
503
0
    return "_";
504
0
  if (state == DeadState)
505
0
    return "X";
506
0
  if (state == FullMatchState)
507
0
    return "*";
508
0
  std::string s;
509
0
  const char* sep = "";
510
0
  s += StringPrintf("(%p)", state);
511
0
  for (int i = 0; i < state->ninst_; i++) {
512
0
    if (state->inst_[i] == Mark) {
513
0
      s += "|";
514
0
      sep = "";
515
0
    } else if (state->inst_[i] == MatchSep) {
516
0
      s += "||";
517
0
      sep = "";
518
0
    } else {
519
0
      s += StringPrintf("%s%d", sep, state->inst_[i]);
520
0
      sep = ",";
521
0
    }
522
0
  }
523
0
  s += StringPrintf(" flag=%#x", state->flag_);
524
0
  return s;
525
0
}
526
527
//////////////////////////////////////////////////////////////////////
528
//
529
// DFA state graph construction.
530
//
531
// The DFA state graph is a heavily-linked collection of State* structures.
532
// The state_cache_ is a set of all the State structures ever allocated,
533
// so that if the same state is reached by two different paths,
534
// the same State structure can be used.  This reduces allocation
535
// requirements and also avoids duplication of effort across the two
536
// identical states.
537
//
538
// A State is defined by an ordered list of instruction ids and a flag word.
539
//
540
// The choice of an ordered list of instructions differs from a typical
541
// textbook DFA implementation, which would use an unordered set.
542
// Textbook descriptions, however, only care about whether
543
// the DFA matches, not where it matches in the text.  To decide where the
544
// DFA matches, we need to mimic the behavior of the dominant backtracking
545
// implementations like PCRE, which try one possible regular expression
546
// execution, then another, then another, stopping when one of them succeeds.
547
// The DFA execution tries these many executions in parallel, representing
548
// each by an instruction id.  These pointers are ordered in the State.inst_
549
// list in the same order that the executions would happen in a backtracking
550
// search: if a match is found during execution of inst_[2], inst_[i] for i>=3
551
// can be discarded.
552
//
553
// Textbooks also typically do not consider context-aware empty string operators
554
// like ^ or $.  These are handled by the flag word, which specifies the set
555
// of empty-string operators that should be matched when executing at the
556
// current text position.  These flag bits are defined in prog.h.
557
// The flag word also contains two DFA-specific bits: kFlagMatch if the state
558
// is a matching state (one that reached a kInstMatch in the program)
559
// and kFlagLastWord if the last processed byte was a word character, for the
560
// implementation of \B and \b.
561
//
562
// The flag word also contains, shifted up 16 bits, the bits looked for by
563
// any kInstEmptyWidth instructions in the state.  These provide a useful
564
// summary indicating when new flags might be useful.
565
//
566
// The permanent representation of a State's instruction ids is just an array,
567
// but while a state is being analyzed, these instruction ids are represented
568
// as a Workq, which is an array that allows iteration in insertion order.
569
570
// NOTE(rsc): The choice of State construction determines whether the DFA
571
// mimics backtracking implementations (so-called leftmost first matching) or
572
// traditional DFA implementations (so-called leftmost longest matching as
573
// prescribed by POSIX).  This implementation chooses to mimic the
574
// backtracking implementations, because we want to replace PCRE.  To get
575
// POSIX behavior, the states would need to be considered not as a simple
576
// ordered list of instruction ids, but as a list of unordered sets of instruction
577
// ids.  A match by a state in one set would inhibit the running of sets
578
// farther down the list but not other instruction ids in the same set.  Each
579
// set would correspond to matches beginning at a given point in the string.
580
// This is implemented by separating different sets with Mark pointers.
581
582
// Looks in the State cache for a State matching q, flag.
583
// If one is found, returns it.  If one is not found, allocates one,
584
// inserts it in the cache, and returns it.
585
// If mq is not null, MatchSep and the match IDs in mq will be appended
586
// to the State.
587
33.7M
DFA::State* DFA::WorkqToCachedState(Workq* q, Workq* mq, uint32_t flag) {
588
  //mutex_.AssertHeld();
589
590
  // Construct array of instruction ids for the new state.
591
  // Only ByteRange, EmptyWidth, and Match instructions are useful to keep:
592
  // those are the only operators with any effect in
593
  // RunWorkqOnEmptyString or RunWorkqOnByte.
594
33.7M
  PODArray<int> inst(q->size());
595
33.7M
  int n = 0;
596
33.7M
  uint32_t needflags = 0;  // flags needed by kInstEmptyWidth instructions
597
33.7M
  bool sawmatch = false;   // whether queue contains guaranteed kInstMatch
598
33.7M
  bool sawmark = false;    // whether queue contains a Mark
599
600
35.1M
  for (Workq::iterator it = q->begin(); it != q->end(); ++it) {
601
1.37M
    int id = *it;
602
1.37M
    if (sawmatch && (kind_ == Prog::kFirstMatch || q->is_mark(id)))
603
0
      break;
604
1.37M
    if (q->is_mark(id)) {
605
0
      if (n > 0 && inst[n-1] != Mark) {
606
0
        sawmark = true;
607
0
        inst[n++] = Mark;
608
0
      }
609
0
      continue;
610
0
    }
611
1.37M
    Prog::Inst* ip = prog_->inst(id);
612
1.37M
    switch (ip->opcode()) {
613
0
      case kInstAltMatch:
614
        // This state will continue to a match no matter what
615
        // the rest of the input is.  If it is the highest priority match
616
        // being considered, return the special FullMatchState
617
        // to indicate that it's all matches from here out.
618
0
        if (kind_ != Prog::kManyMatch &&
619
0
            (kind_ != Prog::kFirstMatch ||
620
0
             (it == q->begin() && ip->greedy(prog_))) &&
621
0
            (kind_ != Prog::kLongestMatch || !sawmark) &&
622
0
            (flag & kFlagMatch)) {
623
0
          return FullMatchState;
624
0
        }
625
0
        FALLTHROUGH_INTENDED;
626
1.37M
      default:
627
        // Record iff id is the head of its list, which must
628
        // be the case if id-1 is the last of *its* list. :)
629
1.37M
        if (prog_->inst(id-1)->last())
630
774k
          inst[n++] = *it;
631
1.37M
        if (ip->opcode() == kInstEmptyWidth)
632
23.6k
          needflags |= ip->empty();
633
1.37M
        if (ip->opcode() == kInstMatch && !prog_->anchor_end())
634
3.54k
          sawmatch = true;
635
1.37M
        break;
636
1.37M
    }
637
1.37M
  }
638
33.7M
  DCHECK_LE(n, q->size());
639
33.7M
  if (n > 0 && inst[n-1] == Mark)
640
0
    n--;
641
642
  // If there are no empty-width instructions waiting to execute,
643
  // then the extra flag bits will not be used, so there is no
644
  // point in saving them.  (Discarding them reduces the number
645
  // of distinct states.)
646
33.7M
  if (needflags == 0)
647
33.7M
    flag &= kFlagMatch;
648
649
  // NOTE(rsc): The code above cannot do flag &= needflags,
650
  // because if the right flags were present to pass the current
651
  // kInstEmptyWidth instructions, new kInstEmptyWidth instructions
652
  // might be reached that in turn need different flags.
653
  // The only sure thing is that if there are no kInstEmptyWidth
654
  // instructions at all, no flags will be needed.
655
  // We could do the extra work to figure out the full set of
656
  // possibly needed flags by exploring past the kInstEmptyWidth
657
  // instructions, but the check above -- are any flags needed
658
  // at all? -- handles the most common case.  More fine-grained
659
  // analysis can only be justified by measurements showing that
660
  // too many redundant states are being allocated.
661
662
  // If there are no Insts in the list, it's a dead state,
663
  // which is useful to signal with a special pointer so that
664
  // the execution loop can stop early.  This is only okay
665
  // if the state is *not* a matching state.
666
33.7M
  if (n == 0 && flag == 0) {
667
32.9M
    return DeadState;
668
32.9M
  }
669
670
  // If we're in longest match mode, the state is a sequence of
671
  // unordered state sets separated by Marks.  Sort each set
672
  // to canonicalize, to reduce the number of distinct sets stored.
673
805k
  if (kind_ == Prog::kLongestMatch) {
674
805k
    int* ip = inst.data();
675
805k
    int* ep = ip + n;
676
1.49M
    while (ip < ep) {
677
684k
      int* markp = ip;
678
1.45M
      while (markp < ep && *markp != Mark)
679
774k
        markp++;
680
684k
      std::sort(ip, markp);
681
684k
      if (markp < ep)
682
0
        markp++;
683
684k
      ip = markp;
684
684k
    }
685
805k
  }
686
687
  // If we're in many match mode, canonicalize for similar reasons:
688
  // we have an unordered set of states (i.e. we don't have Marks)
689
  // and sorting will reduce the number of distinct sets stored.
690
805k
  if (kind_ == Prog::kManyMatch) {
691
0
    int* ip = inst.data();
692
0
    int* ep = ip + n;
693
0
    std::sort(ip, ep);
694
0
  }
695
696
  // Append MatchSep and the match IDs in mq if necessary.
697
805k
  if (mq != NULL) {
698
0
    inst[n++] = MatchSep;
699
0
    for (Workq::iterator i = mq->begin(); i != mq->end(); ++i) {
700
0
      int id = *i;
701
0
      Prog::Inst* ip = prog_->inst(id);
702
0
      if (ip->opcode() == kInstMatch)
703
0
        inst[n++] = ip->match_id();
704
0
    }
705
0
  }
706
707
  // Save the needed empty-width flags in the top bits for use later.
708
805k
  flag |= needflags << kFlagNeedShift;
709
710
805k
  State* state = CachedState(inst.data(), n, flag);
711
805k
  return state;
712
33.7M
}
713
714
// Looks in the State cache for a State matching inst, ninst, flag.
715
// If one is found, returns it.  If one is not found, allocates one,
716
// inserts it in the cache, and returns it.
717
805k
DFA::State* DFA::CachedState(int* inst, int ninst, uint32_t flag) {
718
  //mutex_.AssertHeld();
719
720
  // Look in the cache for a pre-existing state.
721
  // We have to initialise the struct like this because otherwise
722
  // MSVC will complain about the flexible array member. :(
723
805k
  State state;
724
805k
  state.inst_ = inst;
725
805k
  state.ninst_ = ninst;
726
805k
  state.flag_ = flag;
727
805k
  StateSet::iterator it = state_cache_.find(&state);
728
805k
  if (it != state_cache_.end()) {
729
158k
    return *it;
730
158k
  }
731
732
  // Must have enough memory for new state.
733
  // In addition to what we're going to allocate,
734
  // the state cache hash table seems to incur about 40 bytes per
735
  // State*, empirically.
736
646k
  const int kStateCacheOverhead = 40;
737
646k
  int nnext = prog_->bytemap_range() + 1;  // + 1 for kByteEndText slot
738
646k
  int mem = sizeof(State) + nnext*sizeof(std::atomic<State*>) +
739
646k
            ninst*sizeof(int);
740
646k
  if (mem_budget_ < mem + kStateCacheOverhead) {
741
0
    mem_budget_ = -1;
742
0
    return NULL;
743
0
  }
744
646k
  mem_budget_ -= mem + kStateCacheOverhead;
745
746
  // Allocate new state along with room for next_ and inst_.
747
646k
  char* space = std::allocator<char>().allocate(mem);
748
646k
  State* s = new (space) State;
749
646k
  s->next_ =  new (space + sizeof(State)) std::atomic<State*>[nnext];
750
  // Work around a unfortunate bug in older versions of libstdc++.
751
  // (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64658)
752
54.1M
  for (int i = 0; i < nnext; i++)
753
53.5M
    (void) new (s->next_ + i) std::atomic<State*>(NULL);
754
646k
  s->inst_ = new (s->next_ + nnext) int[ninst];
755
646k
  memmove(s->inst_, inst, ninst*sizeof s->inst_[0]);
756
646k
  s->ninst_ = ninst;
757
646k
  s->flag_ = flag;
758
  // Put state in cache and return it.
759
646k
  state_cache_.insert(s);
760
646k
  return s;
761
646k
}
762
763
// Clear the cache.  Must hold cache_mutex_.w or be in destructor.
764
4.62k
void DFA::ClearCache() {
765
4.62k
  StateSet::iterator begin = state_cache_.begin();
766
4.62k
  StateSet::iterator end = state_cache_.end();
767
651k
  while (begin != end) {
768
646k
    StateSet::iterator tmp = begin;
769
646k
    ++begin;
770
    // Deallocate the blob of memory that we allocated in DFA::CachedState().
771
    // We recompute mem in order to benefit from sized delete where possible.
772
646k
    int ninst = (*tmp)->ninst_;
773
646k
    int nnext = prog_->bytemap_range() + 1;  // + 1 for kByteEndText slot
774
646k
    int mem = sizeof(State) + nnext*sizeof(std::atomic<State*>) +
775
646k
              ninst*sizeof(int);
776
646k
    std::allocator<char>().deallocate(reinterpret_cast<char*>(*tmp), mem);
777
646k
  }
778
4.62k
  state_cache_.clear();
779
4.62k
}
780
781
// Copies insts in state s to the work queue q.
782
33.7M
void DFA::StateToWorkq(State* s, Workq* q) {
783
33.7M
  q->clear();
784
68.7M
  for (int i = 0; i < s->ninst_; i++) {
785
34.9M
    if (s->inst_[i] == Mark) {
786
0
      q->mark();
787
34.9M
    } else if (s->inst_[i] == MatchSep) {
788
      // Nothing after this is an instruction!
789
0
      break;
790
34.9M
    } else {
791
      // Explore from the head of the list.
792
34.9M
      AddToQueue(q, s->inst_[i], s->flag_ & kFlagEmptyMask);
793
34.9M
    }
794
34.9M
  }
795
33.7M
}
796
797
// Adds ip to the work queue, following empty arrows according to flag.
798
35.6M
void DFA::AddToQueue(Workq* q, int id, uint32_t flag) {
799
800
  // Use stack_ to hold our stack of instructions yet to process.
801
  // It was preallocated as follows:
802
  //   one entry per Capture;
803
  //   one entry per EmptyWidth; and
804
  //   one entry per Nop.
805
  // This reflects the maximum number of stack pushes that each can
806
  // perform. (Each instruction can be processed at most once.)
807
  // When using marks, we also added nmark == prog_->size().
808
  // (Otherwise, nmark == 0.)
809
35.6M
  int* stk = stack_.data();
810
35.6M
  int nstk = 0;
811
812
35.6M
  stk[nstk++] = id;
813
71.6M
  while (nstk > 0) {
814
35.9M
    DCHECK_LE(nstk, stack_.size());
815
35.9M
    id = stk[--nstk];
816
817
39.4M
  Loop:
818
39.4M
    if (id == Mark) {
819
0
      q->mark();
820
0
      continue;
821
0
    }
822
823
39.4M
    if (id == 0)
824
0
      continue;
825
826
    // If ip is already on the queue, nothing to do.
827
    // Otherwise add it.  We don't actually keep all the
828
    // ones that get added, but adding all of them here
829
    // increases the likelihood of q->contains(id),
830
    // reducing the amount of duplicated work.
831
39.4M
    if (q->contains(id))
832
936k
      continue;
833
38.4M
    q->insert_new(id);
834
835
    // Process instruction.
836
38.4M
    Prog::Inst* ip = prog_->inst(id);
837
38.4M
    switch (ip->opcode()) {
838
0
      default:
839
0
        LOG(DFATAL) << "unhandled opcode: " << ip->opcode();
840
0
        break;
841
842
37.2M
      case kInstByteRange:  // just save these on the queue
843
37.3M
      case kInstMatch:
844
37.3M
        if (ip->last())
845
34.8M
          break;
846
2.49M
        id = id+1;
847
2.49M
        goto Loop;
848
849
30.9k
      case kInstCapture:    // DFA treats captures as no-ops.
850
691k
      case kInstNop:
851
691k
        if (!ip->last())
852
254k
          stk[nstk++] = id+1;
853
854
        // If this instruction is the [00-FF]* loop at the beginning of
855
        // a leftmost-longest unanchored search, separate with a Mark so
856
        // that future threads (which will start farther to the right in
857
        // the input string) are lower priority than current threads.
858
691k
        if (ip->opcode() == kInstNop && q->maxmark() > 0 &&
859
660k
            id == prog_->start_unanchored() && id != prog_->start())
860
0
          stk[nstk++] = Mark;
861
691k
        id = ip->out();
862
691k
        goto Loop;
863
864
0
      case kInstAltMatch:
865
0
        DCHECK(!ip->last());
866
0
        id = id+1;
867
0
        goto Loop;
868
869
451k
      case kInstEmptyWidth:
870
451k
        if (!ip->last())
871
3.11k
          stk[nstk++] = id+1;
872
873
        // Continue on if we have all the right flag bits.
874
451k
        if (ip->empty() & ~flag)
875
175k
          break;
876
275k
        id = ip->out();
877
275k
        goto Loop;
878
38.4M
    }
879
38.4M
  }
880
35.6M
}
881
882
// Running of work queues.  In the work queue, order matters:
883
// the queue is sorted in priority order.  If instruction i comes before j,
884
// then the instructions that i produces during the run must come before
885
// the ones that j produces.  In order to keep this invariant, all the
886
// work queue runners have to take an old queue to process and then
887
// also a new queue to fill in.  It's not acceptable to add to the end of
888
// an existing queue, because new instructions will not end up in the
889
// correct position.
890
891
// Runs the work queue, processing the empty strings indicated by flag.
892
// For example, flag == kEmptyBeginLine|kEmptyEndLine means to match
893
// both ^ and $.  It is important that callers pass all flags at once:
894
// processing both ^ and $ is not the same as first processing only ^
895
// and then processing only $.  Doing the two-step sequence won't match
896
// ^$^$^$ but processing ^ and $ simultaneously will (and is the behavior
897
// exhibited by existing implementations).
898
3.77k
void DFA::RunWorkqOnEmptyString(Workq* oldq, Workq* newq, uint32_t flag) {
899
3.77k
  newq->clear();
900
15.6k
  for (Workq::iterator i = oldq->begin(); i != oldq->end(); ++i) {
901
11.8k
    if (oldq->is_mark(*i))
902
0
      AddToQueue(newq, Mark, flag);
903
11.8k
    else
904
11.8k
      AddToQueue(newq, *i, flag);
905
11.8k
  }
906
3.77k
}
907
908
// Runs the work queue, processing the single byte c followed by any empty
909
// strings indicated by flag.  For example, c == 'a' and flag == kEmptyEndLine,
910
// means to match c$.  Sets the bool *ismatch to true if the end of the
911
// regular expression program has been reached (the regexp has matched).
912
void DFA::RunWorkqOnByte(Workq* oldq, Workq* newq,
913
33.7M
                         int c, uint32_t flag, bool* ismatch) {
914
  //mutex_.AssertHeld();
915
916
33.7M
  newq->clear();
917
70.4M
  for (Workq::iterator i = oldq->begin(); i != oldq->end(); ++i) {
918
36.7M
    if (oldq->is_mark(*i)) {
919
0
      if (*ismatch)
920
0
        return;
921
0
      newq->mark();
922
0
      continue;
923
0
    }
924
36.7M
    int id = *i;
925
36.7M
    Prog::Inst* ip = prog_->inst(id);
926
36.7M
    switch (ip->opcode()) {
927
0
      default:
928
0
        LOG(DFATAL) << "unhandled opcode: " << ip->opcode();
929
0
        break;
930
931
0
      case kInstFail:        // never succeeds
932
29.4k
      case kInstCapture:     // already followed
933
671k
      case kInstNop:         // already followed
934
671k
      case kInstAltMatch:    // already followed
935
1.09M
      case kInstEmptyWidth:  // already followed
936
1.09M
        break;
937
938
35.4M
      case kInstByteRange:   // can follow if c is in range
939
35.4M
        if (!ip->Matches(c))
940
34.7M
          break;
941
735k
        AddToQueue(newq, ip->out(), flag);
942
735k
        if (ip->hint() != 0) {
943
          // We have a hint, but we must cancel out the
944
          // increment that will occur after the break.
945
16.9k
          i += ip->hint() - 1;
946
718k
        } else {
947
          // We have no hint, so we must find the end
948
          // of the current list and then skip to it.
949
718k
          Prog::Inst* ip0 = ip;
950
1.04M
          while (!ip->last())
951
324k
            ++ip;
952
718k
          i += ip - ip0;
953
718k
        }
954
735k
        break;
955
956
122k
      case kInstMatch:
957
122k
        if (prog_->anchor_end() && c != kByteEndText &&
958
1.35k
            kind_ != Prog::kManyMatch)
959
1.35k
          break;
960
121k
        *ismatch = true;
961
121k
        if (kind_ == Prog::kFirstMatch) {
962
          // Can stop processing work queue since we found a match.
963
0
          return;
964
0
        }
965
121k
        break;
966
36.7M
    }
967
36.7M
  }
968
969
33.7M
}
970
971
// Processes input byte c in state, returning new state.
972
// Caller does not hold mutex.
973
0
DFA::State* DFA::RunStateOnByteUnlocked(State* state, int c) {
974
  // Keep only one RunStateOnByte going
975
  // even if the DFA is being run by multiple threads.
976
0
  MutexLock l(&mutex_);
977
0
  return RunStateOnByte(state, c);
978
0
}
979
980
// Processes input byte c in state, returning new state.
981
89.1M
DFA::State* DFA::RunStateOnByte(State* state, int c) {
982
  //mutex_.AssertHeld();
983
984
89.1M
  if (state <= SpecialStateMax) {
985
0
    if (state == FullMatchState) {
986
      // It is convenient for routines like PossibleMatchRange
987
      // if we implement RunStateOnByte for FullMatchState:
988
      // once you get into this state you never get out,
989
      // so it's pretty easy.
990
0
      return FullMatchState;
991
0
    }
992
0
    if (state == DeadState) {
993
0
      LOG(DFATAL) << "DeadState in RunStateOnByte";
994
0
      return NULL;
995
0
    }
996
0
    if (state == NULL) {
997
0
      LOG(DFATAL) << "NULL state in RunStateOnByte";
998
0
      return NULL;
999
0
    }
1000
0
    LOG(DFATAL) << "Unexpected special state in RunStateOnByte";
1001
0
    return NULL;
1002
0
  }
1003
1004
  // If someone else already computed this, return it.
1005
89.1M
  State* ns = state->next_[ByteMap(c)].load(std::memory_order_relaxed);
1006
89.1M
  if (ns != NULL)
1007
55.3M
    return ns;
1008
1009
  // Convert state into Workq.
1010
33.7M
  StateToWorkq(state, q0_);
1011
1012
  // Flags marking the kinds of empty-width things (^ $ etc)
1013
  // around this byte.  Before the byte we have the flags recorded
1014
  // in the State structure itself.  After the byte we have
1015
  // nothing yet (but that will change: read on).
1016
33.7M
  uint32_t needflag = state->flag_ >> kFlagNeedShift;
1017
33.7M
  uint32_t beforeflag = state->flag_ & kFlagEmptyMask;
1018
33.7M
  uint32_t oldbeforeflag = beforeflag;
1019
33.7M
  uint32_t afterflag = 0;
1020
1021
33.7M
  if (c == '\n') {
1022
    // Insert implicit $ and ^ around \n
1023
132k
    beforeflag |= kEmptyEndLine;
1024
132k
    afterflag |= kEmptyBeginLine;
1025
132k
  }
1026
1027
33.7M
  if (c == kByteEndText) {
1028
    // Insert implicit $ and \z before the fake "end text" byte.
1029
303k
    beforeflag |= kEmptyEndLine | kEmptyEndText;
1030
303k
  }
1031
1032
  // The state flag kFlagLastWord says whether the last
1033
  // byte processed was a word character.  Use that info to
1034
  // insert empty-width (non-)word boundaries.
1035
33.7M
  bool islastword = (state->flag_ & kFlagLastWord) != 0;
1036
33.7M
  bool isword = c != kByteEndText && Prog::IsWordChar(static_cast<uint8_t>(c));
1037
33.7M
  if (isword == islastword)
1038
24.5M
    beforeflag |= kEmptyNonWordBoundary;
1039
9.24M
  else
1040
9.24M
    beforeflag |= kEmptyWordBoundary;
1041
1042
  // Okay, finally ready to run.
1043
  // Only useful to rerun on empty string if there are new, useful flags.
1044
33.7M
  if (beforeflag & ~oldbeforeflag & needflag) {
1045
3.77k
    RunWorkqOnEmptyString(q0_, q1_, beforeflag);
1046
3.77k
    using std::swap;
1047
3.77k
    swap(q0_, q1_);
1048
3.77k
  }
1049
33.7M
  bool ismatch = false;
1050
33.7M
  RunWorkqOnByte(q0_, q1_, c, afterflag, &ismatch);
1051
33.7M
  using std::swap;
1052
33.7M
  swap(q0_, q1_);
1053
1054
  // Save afterflag along with ismatch and isword in new state.
1055
33.7M
  uint32_t flag = afterflag;
1056
33.7M
  if (ismatch)
1057
121k
    flag |= kFlagMatch;
1058
33.7M
  if (isword)
1059
9.21M
    flag |= kFlagLastWord;
1060
1061
33.7M
  if (ismatch && kind_ == Prog::kManyMatch)
1062
0
    ns = WorkqToCachedState(q0_, q1_, flag);
1063
33.7M
  else
1064
33.7M
    ns = WorkqToCachedState(q0_, NULL, flag);
1065
1066
  // Flush ns before linking to it.
1067
  // Write barrier before updating state->next_ so that the
1068
  // main search loop can proceed without any locking, for speed.
1069
  // (Otherwise it would need one mutex operation per input byte.)
1070
33.7M
  state->next_[ByteMap(c)].store(ns, std::memory_order_release);
1071
33.7M
  return ns;
1072
89.1M
}
1073
1074
1075
//////////////////////////////////////////////////////////////////////
1076
// DFA cache reset.
1077
1078
// Reader-writer lock helper.
1079
//
1080
// The DFA uses a reader-writer mutex to protect the state graph itself.
1081
// Traversing the state graph requires holding the mutex for reading,
1082
// and discarding the state graph and starting over requires holding the
1083
// lock for writing.  If a search needs to expand the graph but is out
1084
// of memory, it will need to drop its read lock and then acquire the
1085
// write lock.  Since it cannot then atomically downgrade from write lock
1086
// to read lock, it runs the rest of the search holding the write lock.
1087
// (This probably helps avoid repeated contention, but really the decision
1088
// is forced by the Mutex interface.)  It's a bit complicated to keep
1089
// track of whether the lock is held for reading or writing and thread
1090
// that through the search, so instead we encapsulate it in the RWLocker
1091
// and pass that around.
1092
1093
class DFA::RWLocker {
1094
 public:
1095
  explicit RWLocker(CacheMutex* mu);
1096
  ~RWLocker();
1097
1098
  // If the lock is only held for reading right now,
1099
  // drop the read lock and re-acquire for writing.
1100
  // Subsequent calls to LockForWriting are no-ops.
1101
  // Notice that the lock is *released* temporarily.
1102
  void LockForWriting();
1103
1104
 private:
1105
  CacheMutex* mu_;
1106
  bool writing_;
1107
1108
  RWLocker(const RWLocker&) = delete;
1109
  RWLocker& operator=(const RWLocker&) = delete;
1110
};
1111
1112
4.40k
DFA::RWLocker::RWLocker(CacheMutex* mu) : mu_(mu), writing_(false) {
1113
4.40k
  mu_->ReaderLock();
1114
4.40k
}
1115
1116
// This function is marked as NO_THREAD_SAFETY_ANALYSIS because
1117
// the annotations don't support lock upgrade.
1118
0
void DFA::RWLocker::LockForWriting() NO_THREAD_SAFETY_ANALYSIS {
1119
0
  if (!writing_) {
1120
0
    mu_->ReaderUnlock();
1121
0
    mu_->WriterLock();
1122
0
    writing_ = true;
1123
0
  }
1124
0
}
1125
1126
4.40k
DFA::RWLocker::~RWLocker() {
1127
4.40k
  if (!writing_)
1128
4.40k
    mu_->ReaderUnlock();
1129
0
  else
1130
0
    mu_->WriterUnlock();
1131
4.40k
}
1132
1133
1134
// When the DFA's State cache fills, we discard all the states in the
1135
// cache and start over.  Many threads can be using and adding to the
1136
// cache at the same time, so we synchronize using the cache_mutex_
1137
// to keep from stepping on other threads.  Specifically, all the
1138
// threads using the current cache hold cache_mutex_ for reading.
1139
// When a thread decides to flush the cache, it drops cache_mutex_
1140
// and then re-acquires it for writing.  That ensures there are no
1141
// other threads accessing the cache anymore.  The rest of the search
1142
// runs holding cache_mutex_ for writing, avoiding any contention
1143
// with or cache pollution caused by other threads.
1144
1145
0
void DFA::ResetCache(RWLocker* cache_lock) {
1146
  // Re-acquire the cache_mutex_ for writing (exclusive use).
1147
0
  cache_lock->LockForWriting();
1148
1149
0
  hooks::GetDFAStateCacheResetHook()({
1150
0
      state_budget_,
1151
0
      state_cache_.size(),
1152
0
  });
1153
1154
  // Clear the cache, reset the memory budget.
1155
0
  for (int i = 0; i < kMaxStart; i++)
1156
0
    start_[i].start.store(NULL, std::memory_order_relaxed);
1157
0
  ClearCache();
1158
0
  mem_budget_ = state_budget_;
1159
0
}
1160
1161
// Typically, a couple States do need to be preserved across a cache
1162
// reset, like the State at the current point in the search.
1163
// The StateSaver class helps keep States across cache resets.
1164
// It makes a copy of the state's guts outside the cache (before the reset)
1165
// and then can be asked, after the reset, to recreate the State
1166
// in the new cache.  For example, in a DFA method ("this" is a DFA):
1167
//
1168
//   StateSaver saver(this, s);
1169
//   ResetCache(cache_lock);
1170
//   s = saver.Restore();
1171
//
1172
// The saver should always have room in the cache to re-create the state,
1173
// because resetting the cache locks out all other threads, and the cache
1174
// is known to have room for at least a couple states (otherwise the DFA
1175
// constructor fails).
1176
1177
class DFA::StateSaver {
1178
 public:
1179
  explicit StateSaver(DFA* dfa, State* state);
1180
  ~StateSaver();
1181
1182
  // Recreates and returns a state equivalent to the
1183
  // original state passed to the constructor.
1184
  // Returns NULL if the cache has filled, but
1185
  // since the DFA guarantees to have room in the cache
1186
  // for a couple states, should never return NULL
1187
  // if used right after ResetCache.
1188
  State* Restore();
1189
1190
 private:
1191
  DFA* dfa_;         // the DFA to use
1192
  int* inst_;        // saved info from State
1193
  int ninst_;
1194
  uint32_t flag_;
1195
  bool is_special_;  // whether original state was special
1196
  State* special_;   // if is_special_, the original state
1197
1198
  StateSaver(const StateSaver&) = delete;
1199
  StateSaver& operator=(const StateSaver&) = delete;
1200
};
1201
1202
0
DFA::StateSaver::StateSaver(DFA* dfa, State* state) {
1203
0
  dfa_ = dfa;
1204
0
  if (state <= SpecialStateMax) {
1205
0
    inst_ = NULL;
1206
0
    ninst_ = 0;
1207
0
    flag_ = 0;
1208
0
    is_special_ = true;
1209
0
    special_ = state;
1210
0
    return;
1211
0
  }
1212
0
  is_special_ = false;
1213
0
  special_ = NULL;
1214
0
  flag_ = state->flag_;
1215
0
  ninst_ = state->ninst_;
1216
0
  inst_ = new int[ninst_];
1217
0
  memmove(inst_, state->inst_, ninst_*sizeof inst_[0]);
1218
0
}
1219
1220
0
DFA::StateSaver::~StateSaver() {
1221
0
  if (!is_special_)
1222
0
    delete[] inst_;
1223
0
}
1224
1225
0
DFA::State* DFA::StateSaver::Restore() {
1226
0
  if (is_special_)
1227
0
    return special_;
1228
0
  MutexLock l(&dfa_->mutex_);
1229
0
  State* s = dfa_->CachedState(inst_, ninst_, flag_);
1230
0
  if (s == NULL)
1231
0
    LOG(DFATAL) << "StateSaver failed to restore state.";
1232
0
  return s;
1233
0
}
1234
1235
1236
//////////////////////////////////////////////////////////////////////
1237
//
1238
// DFA execution.
1239
//
1240
// The basic search loop is easy: start in a state s and then for each
1241
// byte c in the input, s = s->next[c].
1242
//
1243
// This simple description omits a few efficiency-driven complications.
1244
//
1245
// First, the State graph is constructed incrementally: it is possible
1246
// that s->next[c] is null, indicating that that state has not been
1247
// fully explored.  In this case, RunStateOnByte must be invoked to
1248
// determine the next state, which is cached in s->next[c] to save
1249
// future effort.  An alternative reason for s->next[c] to be null is
1250
// that the DFA has reached a so-called "dead state", in which any match
1251
// is no longer possible.  In this case RunStateOnByte will return NULL
1252
// and the processing of the string can stop early.
1253
//
1254
// Second, a 256-element pointer array for s->next_ makes each State
1255
// quite large (2kB on 64-bit machines).  Instead, dfa->bytemap_[]
1256
// maps from bytes to "byte classes" and then next_ only needs to have
1257
// as many pointers as there are byte classes.  A byte class is simply a
1258
// range of bytes that the regexp never distinguishes between.
1259
// A regexp looking for a[abc] would have four byte ranges -- 0 to 'a'-1,
1260
// 'a', 'b' to 'c', and 'c' to 0xFF.  The bytemap slows us a little bit
1261
// but in exchange we typically cut the size of a State (and thus our
1262
// memory footprint) by about 5-10x.  The comments still refer to
1263
// s->next[c] for simplicity, but code should refer to s->next_[bytemap_[c]].
1264
//
1265
// Third, it is common for a DFA for an unanchored match to begin in a
1266
// state in which only one particular byte value can take the DFA to a
1267
// different state.  That is, s->next[c] != s for only one c.  In this
1268
// situation, the DFA can do better than executing the simple loop.
1269
// Instead, it can call memchr to search very quickly for the byte c.
1270
// Whether the start state has this property is determined during a
1271
// pre-compilation pass and the "can_prefix_accel" argument is set.
1272
//
1273
// Fourth, the desired behavior is to search for the leftmost-best match
1274
// (approximately, the same one that Perl would find), which is not
1275
// necessarily the match ending earliest in the string.  Each time a
1276
// match is found, it must be noted, but the DFA must continue on in
1277
// hope of finding a higher-priority match.  In some cases, the caller only
1278
// cares whether there is any match at all, not which one is found.
1279
// The "want_earliest_match" flag causes the search to stop at the first
1280
// match found.
1281
//
1282
// Fifth, one algorithm that uses the DFA needs it to run over the
1283
// input string backward, beginning at the end and ending at the beginning.
1284
// Passing false for the "run_forward" flag causes the DFA to run backward.
1285
//
1286
// The checks for these last three cases, which in a naive implementation
1287
// would be performed once per input byte, slow the general loop enough
1288
// to merit specialized versions of the search loop for each of the
1289
// eight possible settings of the three booleans.  Rather than write
1290
// eight different functions, we write one general implementation and then
1291
// inline it to create the specialized ones.
1292
//
1293
// Note that matches are delayed by one byte, to make it easier to
1294
// accomodate match conditions depending on the next input byte (like $ and \b).
1295
// When s->next[c]->IsMatch(), it means that there is a match ending just
1296
// *before* byte c.
1297
1298
// The generic search loop.  Searches text for a match, returning
1299
// the pointer to the end of the chosen match, or NULL if no match.
1300
// The bools are equal to the same-named variables in params, but
1301
// making them function arguments lets the inliner specialize
1302
// this function to each combination (see two paragraphs above).
1303
template <bool can_prefix_accel,
1304
          bool want_earliest_match,
1305
          bool run_forward>
1306
0
inline bool DFA::InlinedSearchLoop(SearchParams* params) {
1307
0
  State* start = params->start;
1308
0
  const uint8_t* bp = BytePtr(params->text.data());  // start of text
1309
0
  const uint8_t* p = bp;                             // text scanning point
1310
0
  const uint8_t* ep = BytePtr(params->text.data() +
1311
0
                              params->text.size());  // end of text
1312
0
  const uint8_t* resetp = NULL;                      // p at last cache reset
1313
0
  if (!run_forward) {
1314
0
    using std::swap;
1315
0
    swap(p, ep);
1316
0
  }
1317
1318
0
  const uint8_t* bytemap = prog_->bytemap();
1319
0
  const uint8_t* lastmatch = NULL;   // most recent matching position in text
1320
0
  bool matched = false;
1321
1322
0
  State* s = start;
1323
1324
0
  if (s->IsMatch()) {
1325
0
    matched = true;
1326
0
    lastmatch = p;
1327
0
    if (params->matches != NULL && kind_ == Prog::kManyMatch) {
1328
0
      for (int i = s->ninst_ - 1; i >= 0; i--) {
1329
0
        int id = s->inst_[i];
1330
0
        if (id == MatchSep)
1331
0
          break;
1332
0
        params->matches->insert(id);
1333
0
      }
1334
0
    }
1335
0
    if (want_earliest_match) {
1336
0
      params->ep = reinterpret_cast<const char*>(lastmatch);
1337
0
      return true;
1338
0
    }
1339
0
  }
1340
1341
0
  while (p != ep) {
1342
1343
0
    if (can_prefix_accel && s == start) {
1344
      // In start state, only way out is to find the prefix,
1345
      // so we use prefix accel (e.g. memchr) to skip ahead.
1346
      // If not found, we can skip to the end of the string.
1347
0
      p = BytePtr(prog_->PrefixAccel(p, ep - p));
1348
0
      if (p == NULL) {
1349
0
        p = ep;
1350
0
        break;
1351
0
      }
1352
0
    }
1353
1354
0
    int c;
1355
0
    if (run_forward)
1356
0
      c = *p++;
1357
0
    else
1358
0
      c = *--p;
1359
1360
    // Note that multiple threads might be consulting
1361
    // s->next_[bytemap[c]] simultaneously.
1362
    // RunStateOnByte takes care of the appropriate locking,
1363
    // including a memory barrier so that the unlocked access
1364
    // (sometimes known as "double-checked locking") is safe.
1365
    // The alternative would be either one DFA per thread
1366
    // or one mutex operation per input byte.
1367
    //
1368
    // ns == DeadState means the state is known to be dead
1369
    // (no more matches are possible).
1370
    // ns == NULL means the state has not yet been computed
1371
    // (need to call RunStateOnByteUnlocked).
1372
    // RunStateOnByte returns ns == NULL if it is out of memory.
1373
    // ns == FullMatchState means the rest of the string matches.
1374
    //
1375
    // Okay to use bytemap[] not ByteMap() here, because
1376
    // c is known to be an actual byte and not kByteEndText.
1377
1378
0
    State* ns = s->next_[bytemap[c]].load(std::memory_order_acquire);
1379
0
    if (ns == NULL) {
1380
0
      ns = RunStateOnByteUnlocked(s, c);
1381
0
      if (ns == NULL) {
1382
        // After we reset the cache, we hold cache_mutex exclusively,
1383
        // so if resetp != NULL, it means we filled the DFA state
1384
        // cache with this search alone (without any other threads).
1385
        // Benchmarks show that doing a state computation on every
1386
        // byte runs at about 0.2 MB/s, while the NFA (nfa.cc) can do the
1387
        // same at about 2 MB/s.  Unless we're processing an average
1388
        // of 10 bytes per state computation, fail so that RE2 can
1389
        // fall back to the NFA.  However, RE2::Set cannot fall back,
1390
        // so we just have to keep on keeping on in that case.
1391
0
        if (dfa_should_bail_when_slow && resetp != NULL &&
1392
0
            static_cast<size_t>(p - resetp) < 10*state_cache_.size() &&
1393
0
            kind_ != Prog::kManyMatch) {
1394
0
          params->failed = true;
1395
0
          return false;
1396
0
        }
1397
0
        resetp = p;
1398
1399
        // Prepare to save start and s across the reset.
1400
0
        StateSaver save_start(this, start);
1401
0
        StateSaver save_s(this, s);
1402
1403
        // Discard all the States in the cache.
1404
0
        ResetCache(params->cache_lock);
1405
1406
        // Restore start and s so we can continue.
1407
0
        if ((start = save_start.Restore()) == NULL ||
1408
0
            (s = save_s.Restore()) == NULL) {
1409
          // Restore already did LOG(DFATAL).
1410
0
          params->failed = true;
1411
0
          return false;
1412
0
        }
1413
0
        ns = RunStateOnByteUnlocked(s, c);
1414
0
        if (ns == NULL) {
1415
0
          LOG(DFATAL) << "RunStateOnByteUnlocked failed after ResetCache";
1416
0
          params->failed = true;
1417
0
          return false;
1418
0
        }
1419
0
      }
1420
0
    }
1421
0
    if (ns <= SpecialStateMax) {
1422
0
      if (ns == DeadState) {
1423
0
        params->ep = reinterpret_cast<const char*>(lastmatch);
1424
0
        return matched;
1425
0
      }
1426
      // FullMatchState
1427
0
      params->ep = reinterpret_cast<const char*>(ep);
1428
0
      return true;
1429
0
    }
1430
1431
0
    s = ns;
1432
0
    if (s->IsMatch()) {
1433
0
      matched = true;
1434
      // The DFA notices the match one byte late,
1435
      // so adjust p before using it in the match.
1436
0
      if (run_forward)
1437
0
        lastmatch = p - 1;
1438
0
      else
1439
0
        lastmatch = p + 1;
1440
0
      if (params->matches != NULL && kind_ == Prog::kManyMatch) {
1441
0
        for (int i = s->ninst_ - 1; i >= 0; i--) {
1442
0
          int id = s->inst_[i];
1443
0
          if (id == MatchSep)
1444
0
            break;
1445
0
          params->matches->insert(id);
1446
0
        }
1447
0
      }
1448
0
      if (want_earliest_match) {
1449
0
        params->ep = reinterpret_cast<const char*>(lastmatch);
1450
0
        return true;
1451
0
      }
1452
0
    }
1453
0
  }
1454
1455
  // Process one more byte to see if it triggers a match.
1456
  // (Remember, matches are delayed one byte.)
1457
1458
0
  int lastbyte;
1459
0
  if (run_forward) {
1460
0
    if (EndPtr(params->text) == EndPtr(params->context))
1461
0
      lastbyte = kByteEndText;
1462
0
    else
1463
0
      lastbyte = EndPtr(params->text)[0] & 0xFF;
1464
0
  } else {
1465
0
    if (BeginPtr(params->text) == BeginPtr(params->context))
1466
0
      lastbyte = kByteEndText;
1467
0
    else
1468
0
      lastbyte = BeginPtr(params->text)[-1] & 0xFF;
1469
0
  }
1470
1471
0
  State* ns = s->next_[ByteMap(lastbyte)].load(std::memory_order_acquire);
1472
0
  if (ns == NULL) {
1473
0
    ns = RunStateOnByteUnlocked(s, lastbyte);
1474
0
    if (ns == NULL) {
1475
0
      StateSaver save_s(this, s);
1476
0
      ResetCache(params->cache_lock);
1477
0
      if ((s = save_s.Restore()) == NULL) {
1478
0
        params->failed = true;
1479
0
        return false;
1480
0
      }
1481
0
      ns = RunStateOnByteUnlocked(s, lastbyte);
1482
0
      if (ns == NULL) {
1483
0
        LOG(DFATAL) << "RunStateOnByteUnlocked failed after Reset";
1484
0
        params->failed = true;
1485
0
        return false;
1486
0
      }
1487
0
    }
1488
0
  }
1489
0
  if (ns <= SpecialStateMax) {
1490
0
    if (ns == DeadState) {
1491
0
      params->ep = reinterpret_cast<const char*>(lastmatch);
1492
0
      return matched;
1493
0
    }
1494
    // FullMatchState
1495
0
    params->ep = reinterpret_cast<const char*>(ep);
1496
0
    return true;
1497
0
  }
1498
1499
0
  s = ns;
1500
0
  if (s->IsMatch()) {
1501
0
    matched = true;
1502
0
    lastmatch = p;
1503
0
    if (params->matches != NULL && kind_ == Prog::kManyMatch) {
1504
0
      for (int i = s->ninst_ - 1; i >= 0; i--) {
1505
0
        int id = s->inst_[i];
1506
0
        if (id == MatchSep)
1507
0
          break;
1508
0
        params->matches->insert(id);
1509
0
      }
1510
0
    }
1511
0
  }
1512
1513
0
  params->ep = reinterpret_cast<const char*>(lastmatch);
1514
0
  return matched;
1515
0
}
Unexecuted instantiation: bool duckdb_re2::DFA::InlinedSearchLoop<false, false, false>(duckdb_re2::DFA::SearchParams*)
Unexecuted instantiation: bool duckdb_re2::DFA::InlinedSearchLoop<false, false, true>(duckdb_re2::DFA::SearchParams*)
Unexecuted instantiation: bool duckdb_re2::DFA::InlinedSearchLoop<false, true, false>(duckdb_re2::DFA::SearchParams*)
Unexecuted instantiation: bool duckdb_re2::DFA::InlinedSearchLoop<false, true, true>(duckdb_re2::DFA::SearchParams*)
Unexecuted instantiation: bool duckdb_re2::DFA::InlinedSearchLoop<true, false, false>(duckdb_re2::DFA::SearchParams*)
Unexecuted instantiation: bool duckdb_re2::DFA::InlinedSearchLoop<true, false, true>(duckdb_re2::DFA::SearchParams*)
Unexecuted instantiation: bool duckdb_re2::DFA::InlinedSearchLoop<true, true, false>(duckdb_re2::DFA::SearchParams*)
Unexecuted instantiation: bool duckdb_re2::DFA::InlinedSearchLoop<true, true, true>(duckdb_re2::DFA::SearchParams*)
1516
1517
// Inline specializations of the general loop.
1518
0
bool DFA::SearchFFF(SearchParams* params) {
1519
0
  return InlinedSearchLoop<false, false, false>(params);
1520
0
}
1521
0
bool DFA::SearchFFT(SearchParams* params) {
1522
0
  return InlinedSearchLoop<false, false, true>(params);
1523
0
}
1524
0
bool DFA::SearchFTF(SearchParams* params) {
1525
0
  return InlinedSearchLoop<false, true, false>(params);
1526
0
}
1527
0
bool DFA::SearchFTT(SearchParams* params) {
1528
0
  return InlinedSearchLoop<false, true, true>(params);
1529
0
}
1530
0
bool DFA::SearchTFF(SearchParams* params) {
1531
0
  return InlinedSearchLoop<true, false, false>(params);
1532
0
}
1533
0
bool DFA::SearchTFT(SearchParams* params) {
1534
0
  return InlinedSearchLoop<true, false, true>(params);
1535
0
}
1536
0
bool DFA::SearchTTF(SearchParams* params) {
1537
0
  return InlinedSearchLoop<true, true, false>(params);
1538
0
}
1539
0
bool DFA::SearchTTT(SearchParams* params) {
1540
0
  return InlinedSearchLoop<true, true, true>(params);
1541
0
}
1542
1543
// For performance, calls the appropriate specialized version
1544
// of InlinedSearchLoop.
1545
0
bool DFA::FastSearchLoop(SearchParams* params) {
1546
  // Because the methods are private, the Searches array
1547
  // cannot be declared at top level.
1548
0
  static bool (DFA::*Searches[])(SearchParams*) = {
1549
0
    &DFA::SearchFFF,
1550
0
    &DFA::SearchFFT,
1551
0
    &DFA::SearchFTF,
1552
0
    &DFA::SearchFTT,
1553
0
    &DFA::SearchTFF,
1554
0
    &DFA::SearchTFT,
1555
0
    &DFA::SearchTTF,
1556
0
    &DFA::SearchTTT,
1557
0
  };
1558
1559
0
  int index = 4 * params->can_prefix_accel +
1560
0
              2 * params->want_earliest_match +
1561
0
              1 * params->run_forward;
1562
0
  return (this->*Searches[index])(params);
1563
0
}
1564
1565
1566
// The discussion of DFA execution above ignored the question of how
1567
// to determine the initial state for the search loop.  There are two
1568
// factors that influence the choice of start state.
1569
//
1570
// The first factor is whether the search is anchored or not.
1571
// The regexp program (Prog*) itself has
1572
// two different entry points: one for anchored searches and one for
1573
// unanchored searches.  (The unanchored version starts with a leading ".*?"
1574
// and then jumps to the anchored one.)
1575
//
1576
// The second factor is where text appears in the larger context, which
1577
// determines which empty-string operators can be matched at the beginning
1578
// of execution.  If text is at the very beginning of context, \A and ^ match.
1579
// Otherwise if text is at the beginning of a line, then ^ matches.
1580
// Otherwise it matters whether the character before text is a word character
1581
// or a non-word character.
1582
//
1583
// The two cases (unanchored vs not) and four cases (empty-string flags)
1584
// combine to make the eight cases recorded in the DFA's begin_text_[2],
1585
// begin_line_[2], after_wordchar_[2], and after_nonwordchar_[2] cached
1586
// StartInfos.  The start state for each is filled in the first time it
1587
// is used for an actual search.
1588
1589
// Examines text, context, and anchored to determine the right start
1590
// state for the DFA search loop.  Fills in params and returns true on success.
1591
// Returns false on failure.
1592
4.40k
bool DFA::AnalyzeSearch(SearchParams* params) {
1593
4.40k
  const StringPiece& text = params->text;
1594
4.40k
  const StringPiece& context = params->context;
1595
1596
  // Sanity check: make sure that text lies within context.
1597
4.40k
  if (BeginPtr(text) < BeginPtr(context) || EndPtr(text) > EndPtr(context)) {
1598
0
    LOG(DFATAL) << "context does not contain text";
1599
0
    params->start = DeadState;
1600
0
    return true;
1601
0
  }
1602
1603
  // Determine correct search type.
1604
4.40k
  int start;
1605
4.40k
  uint32_t flags;
1606
4.40k
  if (params->run_forward) {
1607
0
    if (BeginPtr(text) == BeginPtr(context)) {
1608
0
      start = kStartBeginText;
1609
0
      flags = kEmptyBeginText|kEmptyBeginLine;
1610
0
    } else if (BeginPtr(text)[-1] == '\n') {
1611
0
      start = kStartBeginLine;
1612
0
      flags = kEmptyBeginLine;
1613
0
    } else if (Prog::IsWordChar(BeginPtr(text)[-1] & 0xFF)) {
1614
0
      start = kStartAfterWordChar;
1615
0
      flags = kFlagLastWord;
1616
0
    } else {
1617
0
      start = kStartAfterNonWordChar;
1618
0
      flags = 0;
1619
0
    }
1620
4.40k
  } else {
1621
4.40k
    if (EndPtr(text) == EndPtr(context)) {
1622
4.40k
      start = kStartBeginText;
1623
4.40k
      flags = kEmptyBeginText|kEmptyBeginLine;
1624
4.40k
    } else if (EndPtr(text)[0] == '\n') {
1625
0
      start = kStartBeginLine;
1626
0
      flags = kEmptyBeginLine;
1627
0
    } else if (Prog::IsWordChar(EndPtr(text)[0] & 0xFF)) {
1628
0
      start = kStartAfterWordChar;
1629
0
      flags = kFlagLastWord;
1630
0
    } else {
1631
0
      start = kStartAfterNonWordChar;
1632
0
      flags = 0;
1633
0
    }
1634
4.40k
  }
1635
4.40k
  if (params->anchored)
1636
4.40k
    start |= kStartAnchored;
1637
4.40k
  StartInfo* info = &start_[start];
1638
1639
  // Try once without cache_lock for writing.
1640
  // Try again after resetting the cache
1641
  // (ResetCache will relock cache_lock for writing).
1642
4.40k
  if (!AnalyzeSearchHelper(params, info, flags)) {
1643
0
    ResetCache(params->cache_lock);
1644
0
    if (!AnalyzeSearchHelper(params, info, flags)) {
1645
0
      params->failed = true;
1646
0
      LOG(DFATAL) << "Failed to analyze start state.";
1647
0
      return false;
1648
0
    }
1649
0
  }
1650
1651
4.40k
  params->start = info->start.load(std::memory_order_acquire);
1652
1653
  // Even if we could prefix accel, we cannot do so when anchored and,
1654
  // less obviously, we cannot do so when we are going to need flags.
1655
  // This trick works only when there is a single byte that leads to a
1656
  // different state!
1657
4.40k
  if (prog_->can_prefix_accel() &&
1658
2.76k
      !params->anchored &&
1659
0
      params->start > SpecialStateMax &&
1660
0
      params->start->flag_ >> kFlagNeedShift == 0)
1661
0
    params->can_prefix_accel = true;
1662
1663
4.40k
  return true;
1664
4.40k
}
1665
1666
// Fills in info if needed.  Returns true on success, false on failure.
1667
bool DFA::AnalyzeSearchHelper(SearchParams* params, StartInfo* info,
1668
4.40k
                              uint32_t flags) {
1669
  // Quick check.
1670
4.40k
  State* start = info->start.load(std::memory_order_acquire);
1671
4.40k
  if (start != NULL)
1672
0
    return true;
1673
1674
4.40k
  MutexLock l(&mutex_);
1675
4.40k
  start = info->start.load(std::memory_order_relaxed);
1676
4.40k
  if (start != NULL)
1677
0
    return true;
1678
1679
4.40k
  q0_->clear();
1680
4.40k
  AddToQueue(q0_,
1681
4.40k
             params->anchored ? prog_->start() : prog_->start_unanchored(),
1682
4.40k
             flags);
1683
4.40k
  start = WorkqToCachedState(q0_, NULL, flags);
1684
4.40k
  if (start == NULL)
1685
0
    return false;
1686
1687
  // Synchronize with "quick check" above.
1688
4.40k
  info->start.store(start, std::memory_order_release);
1689
4.40k
  return true;
1690
4.40k
}
1691
1692
// The actual DFA search: calls AnalyzeSearch and then FastSearchLoop.
1693
bool DFA::Search(const StringPiece& text,
1694
                 const StringPiece& context,
1695
                 bool anchored,
1696
                 bool want_earliest_match,
1697
                 bool run_forward,
1698
                 bool* failed,
1699
                 const char** epp,
1700
0
                 SparseSet* matches) {
1701
0
  *epp = NULL;
1702
0
  if (!ok()) {
1703
0
    *failed = true;
1704
0
    return false;
1705
0
  }
1706
0
  *failed = false;
1707
1708
0
  RWLocker l(&cache_mutex_);
1709
0
  SearchParams params(text, context, &l);
1710
0
  params.anchored = anchored;
1711
0
  params.want_earliest_match = want_earliest_match;
1712
0
  params.run_forward = run_forward;
1713
0
  params.matches = matches;
1714
1715
0
  if (!AnalyzeSearch(&params)) {
1716
0
    *failed = true;
1717
0
    return false;
1718
0
  }
1719
0
  if (params.start == DeadState)
1720
0
    return false;
1721
0
  if (params.start == FullMatchState) {
1722
0
    if (run_forward == want_earliest_match)
1723
0
      *epp = text.data();
1724
0
    else
1725
0
      *epp = text.data() + text.size();
1726
0
    return true;
1727
0
  }
1728
0
  bool ret = FastSearchLoop(&params);
1729
0
  if (params.failed) {
1730
0
    *failed = true;
1731
0
    return false;
1732
0
  }
1733
0
  *epp = params.ep;
1734
0
  return ret;
1735
0
}
1736
1737
4.62k
DFA* Prog::GetDFA(MatchKind kind) {
1738
  // For a forward DFA, half the memory goes to each DFA.
1739
  // However, if it is a "many match" DFA, then there is
1740
  // no counterpart with which the memory must be shared.
1741
  //
1742
  // For a reverse DFA, all the memory goes to the
1743
  // "longest match" DFA, because RE2 never does reverse
1744
  // "first match" searches.
1745
4.62k
  if (kind == kFirstMatch) {
1746
0
    std::call_once(dfa_first_once_, [](Prog* prog) {
1747
0
      prog->dfa_first_ = new DFA(prog, kFirstMatch, prog->dfa_mem_ / 2);
1748
0
    }, this);
1749
0
    return dfa_first_;
1750
4.62k
  } else if (kind == kManyMatch) {
1751
0
    std::call_once(dfa_first_once_, [](Prog* prog) {
1752
0
      prog->dfa_first_ = new DFA(prog, kManyMatch, prog->dfa_mem_);
1753
0
    }, this);
1754
0
    return dfa_first_;
1755
4.62k
  } else {
1756
4.62k
    std::call_once(dfa_longest_once_, [](Prog* prog) {
1757
4.62k
      if (!prog->reversed_)
1758
4.62k
        prog->dfa_longest_ = new DFA(prog, kLongestMatch, prog->dfa_mem_ / 2);
1759
0
      else
1760
0
        prog->dfa_longest_ = new DFA(prog, kLongestMatch, prog->dfa_mem_);
1761
4.62k
    }, this);
1762
4.62k
    return dfa_longest_;
1763
4.62k
  }
1764
4.62k
}
1765
1766
9.38k
void Prog::DeleteDFA(DFA* dfa) {
1767
9.38k
  delete dfa;
1768
9.38k
}
1769
1770
// Executes the regexp program to search in text,
1771
// which itself is inside the larger context.  (As a convenience,
1772
// passing a NULL context is equivalent to passing text.)
1773
// Returns true if a match is found, false if not.
1774
// If a match is found, fills in match0->end() to point at the end of the match
1775
// and sets match0->begin() to text.begin(), since the DFA can't track
1776
// where the match actually began.
1777
//
1778
// This is the only external interface (class DFA only exists in this file).
1779
//
1780
bool Prog::SearchDFA(const StringPiece& text, const StringPiece& const_context,
1781
                     Anchor anchor, MatchKind kind, StringPiece* match0,
1782
0
                     bool* failed, SparseSet* matches) {
1783
0
  *failed = false;
1784
1785
0
  StringPiece context = const_context;
1786
0
  if (context.data() == NULL)
1787
0
    context = text;
1788
0
  bool caret = anchor_start();
1789
0
  bool dollar = anchor_end();
1790
0
  if (reversed_) {
1791
0
    using std::swap;
1792
0
    swap(caret, dollar);
1793
0
  }
1794
0
  if (caret && BeginPtr(context) != BeginPtr(text))
1795
0
    return false;
1796
0
  if (dollar && EndPtr(context) != EndPtr(text))
1797
0
    return false;
1798
1799
  // Handle full match by running an anchored longest match
1800
  // and then checking if it covers all of text.
1801
0
  bool anchored = anchor == kAnchored || anchor_start() || kind == kFullMatch;
1802
0
  bool endmatch = false;
1803
0
  if (kind == kManyMatch) {
1804
    // This is split out in order to avoid clobbering kind.
1805
0
  } else if (kind == kFullMatch || anchor_end()) {
1806
0
    endmatch = true;
1807
0
    kind = kLongestMatch;
1808
0
  }
1809
1810
  // If the caller doesn't care where the match is (just whether one exists),
1811
  // then we can stop at the very first match we find, the so-called
1812
  // "earliest match".
1813
0
  bool want_earliest_match = false;
1814
0
  if (kind == kManyMatch) {
1815
    // This is split out in order to avoid clobbering kind.
1816
0
    if (matches == NULL) {
1817
0
      want_earliest_match = true;
1818
0
    }
1819
0
  } else if (match0 == NULL && !endmatch) {
1820
0
    want_earliest_match = true;
1821
0
    kind = kLongestMatch;
1822
0
  }
1823
1824
0
  DFA* dfa = GetDFA(kind);
1825
0
  const char* ep;
1826
0
  bool matched = dfa->Search(text, context, anchored,
1827
0
                             want_earliest_match, !reversed_,
1828
0
                             failed, &ep, matches);
1829
0
  if (*failed) {
1830
0
    hooks::GetDFASearchFailureHook()({
1831
        // Nothing yet...
1832
0
    });
1833
0
    return false;
1834
0
  }
1835
0
  if (!matched)
1836
0
    return false;
1837
0
  if (endmatch && ep != (reversed_ ? text.data() : text.data() + text.size()))
1838
0
    return false;
1839
1840
  // If caller cares, record the boundary of the match.
1841
  // We only know where it ends, so use the boundary of text
1842
  // as the beginning.
1843
0
  if (match0) {
1844
0
    if (reversed_)
1845
0
      *match0 =
1846
0
          StringPiece(ep, static_cast<size_t>(text.data() + text.size() - ep));
1847
0
    else
1848
0
      *match0 =
1849
0
          StringPiece(text.data(), static_cast<size_t>(ep - text.data()));
1850
0
  }
1851
0
  return true;
1852
0
}
1853
1854
// Build out all states in DFA.  Returns number of states.
1855
0
int DFA::BuildAllStates(const Prog::DFAStateCallback& cb) {
1856
0
  if (!ok())
1857
0
    return 0;
1858
1859
  // Pick out start state for unanchored search
1860
  // at beginning of text.
1861
0
  RWLocker l(&cache_mutex_);
1862
0
  SearchParams params(StringPiece(), StringPiece(), &l);
1863
0
  params.anchored = false;
1864
0
  if (!AnalyzeSearch(&params) ||
1865
0
      params.start == NULL ||
1866
0
      params.start == DeadState)
1867
0
    return 0;
1868
1869
  // Add start state to work queue.
1870
  // Note that any State* that we handle here must point into the cache,
1871
  // so we can simply depend on pointer-as-a-number hashing and equality.
1872
0
  std::unordered_map<State*, int> m;
1873
0
  std::deque<State*> q;
1874
0
  m.emplace(params.start, static_cast<int>(m.size()));
1875
0
  q.push_back(params.start);
1876
1877
  // Compute the input bytes needed to cover all of the next pointers.
1878
0
  int nnext = prog_->bytemap_range() + 1;  // + 1 for kByteEndText slot
1879
0
  std::vector<int> input(nnext);
1880
0
  for (int c = 0; c < 256; c++) {
1881
0
    int b = prog_->bytemap()[c];
1882
0
    while (c < 256-1 && prog_->bytemap()[c+1] == b)
1883
0
      c++;
1884
0
    input[b] = c;
1885
0
  }
1886
0
  input[prog_->bytemap_range()] = kByteEndText;
1887
1888
  // Scratch space for the output.
1889
0
  std::vector<int> output(nnext);
1890
1891
  // Flood to expand every state.
1892
0
  bool oom = false;
1893
0
  while (!q.empty()) {
1894
0
    State* s = q.front();
1895
0
    q.pop_front();
1896
0
    for (int c : input) {
1897
0
      State* ns = RunStateOnByteUnlocked(s, c);
1898
0
      if (ns == NULL) {
1899
0
        oom = true;
1900
0
        break;
1901
0
      }
1902
0
      if (ns == DeadState) {
1903
0
        output[ByteMap(c)] = -1;
1904
0
        continue;
1905
0
      }
1906
0
      if (m.find(ns) == m.end()) {
1907
0
        m.emplace(ns, static_cast<int>(m.size()));
1908
0
        q.push_back(ns);
1909
0
      }
1910
0
      output[ByteMap(c)] = m[ns];
1911
0
    }
1912
0
    if (cb)
1913
0
      cb(oom ? NULL : output.data(),
1914
0
         s == FullMatchState || s->IsMatch());
1915
0
    if (oom)
1916
0
      break;
1917
0
  }
1918
1919
0
  return static_cast<int>(m.size());
1920
0
}
1921
1922
// Build out all states in DFA for kind.  Returns number of states.
1923
0
int Prog::BuildEntireDFA(MatchKind kind, const DFAStateCallback& cb) {
1924
0
  return GetDFA(kind)->BuildAllStates(cb);
1925
0
}
1926
1927
// Computes min and max for matching string.
1928
// Won't return strings bigger than maxlen.
1929
4.62k
bool DFA::PossibleMatchRange(std::string* min, std::string* max, int maxlen) {
1930
4.62k
  if (!ok())
1931
215
    return false;
1932
1933
  // NOTE: if future users of PossibleMatchRange want more precision when
1934
  // presented with infinitely repeated elements, consider making this a
1935
  // parameter to PossibleMatchRange.
1936
4.40k
  static int kMaxEltRepetitions = 0;
1937
1938
  // Keep track of the number of times we've visited states previously. We only
1939
  // revisit a given state if it's part of a repeated group, so if the value
1940
  // portion of the map tuple exceeds kMaxEltRepetitions we bail out and set
1941
  // |*max| to |PrefixSuccessor(*max)|.
1942
  //
1943
  // Also note that previously_visited_states[UnseenStatePtr] will, in the STL
1944
  // tradition, implicitly insert a '0' value at first use. We take advantage
1945
  // of that property below.
1946
4.40k
  std::unordered_map<State*, int> previously_visited_states;
1947
1948
  // Pick out start state for anchored search at beginning of text.
1949
4.40k
  RWLocker l(&cache_mutex_);
1950
4.40k
  SearchParams params(StringPiece(), StringPiece(), &l);
1951
4.40k
  params.anchored = true;
1952
4.40k
  if (!AnalyzeSearch(&params))
1953
0
    return false;
1954
4.40k
  if (params.start == DeadState) {  // No matching strings
1955
0
    *min = "";
1956
0
    *max = "";
1957
0
    return true;
1958
0
  }
1959
4.40k
  if (params.start == FullMatchState)  // Every string matches: no max
1960
0
    return false;
1961
1962
  // The DFA is essentially a big graph rooted at params.start,
1963
  // and paths in the graph correspond to accepted strings.
1964
  // Each node in the graph has potentially 256+1 arrows
1965
  // coming out, one for each byte plus the magic end of
1966
  // text character kByteEndText.
1967
1968
  // To find the smallest possible prefix of an accepted
1969
  // string, we just walk the graph preferring to follow
1970
  // arrows with the lowest bytes possible.  To find the
1971
  // largest possible prefix, we follow the largest bytes
1972
  // possible.
1973
1974
  // The test for whether there is an arrow from s on byte j is
1975
  //    ns = RunStateOnByteUnlocked(s, j);
1976
  //    if (ns == NULL)
1977
  //      return false;
1978
  //    if (ns != DeadState && ns->ninst > 0)
1979
  // The RunStateOnByteUnlocked call asks the DFA to build out the graph.
1980
  // It returns NULL only if the DFA has run out of memory,
1981
  // in which case we can't be sure of anything.
1982
  // The second check sees whether there was graph built
1983
  // and whether it is interesting graph.  Nodes might have
1984
  // ns->ninst == 0 if they exist only to represent the fact
1985
  // that a match was found on the previous byte.
1986
1987
  // Build minimum prefix.
1988
4.40k
  State* s = params.start;
1989
4.40k
  min->clear();
1990
4.40k
  MutexLock lock(&mutex_);
1991
305k
  for (int i = 0; i < maxlen; i++) {
1992
305k
    if (previously_visited_states[s] > kMaxEltRepetitions)
1993
1.55k
      break;
1994
303k
    previously_visited_states[s]++;
1995
1996
    // Stop if min is a match.
1997
303k
    State* ns = RunStateOnByte(s, kByteEndText);
1998
303k
    if (ns == NULL)  // DFA out of memory
1999
0
      return false;
2000
303k
    if (ns != DeadState && (ns == FullMatchState || ns->IsMatch()))
2001
2.02k
      break;
2002
2003
    // Try to extend the string with low bytes.
2004
301k
    bool extended = false;
2005
17.4M
    for (int j = 0; j < 256; j++) {
2006
17.4M
      ns = RunStateOnByte(s, j);
2007
17.4M
      if (ns == NULL)  // DFA out of memory
2008
0
        return false;
2009
17.4M
      if (ns == FullMatchState ||
2010
17.4M
          (ns > SpecialStateMax && ns->ninst_ > 0)) {
2011
301k
        extended = true;
2012
301k
        min->append(1, static_cast<char>(j));
2013
301k
        s = ns;
2014
301k
        break;
2015
301k
      }
2016
17.4M
    }
2017
301k
    if (!extended)
2018
788
      break;
2019
301k
  }
2020
2021
  // Build maximum prefix.
2022
4.40k
  previously_visited_states.clear();
2023
4.40k
  s = params.start;
2024
4.40k
  max->clear();
2025
522k
  for (int i = 0; i < maxlen; i++) {
2026
522k
    if (previously_visited_states[s] > kMaxEltRepetitions)
2027
1.76k
      break;
2028
520k
    previously_visited_states[s] += 1;
2029
2030
    // Try to extend the string with high bytes.
2031
520k
    bool extended = false;
2032
71.4M
    for (int j = 255; j >= 0; j--) {
2033
71.4M
      State* ns = RunStateOnByte(s, j);
2034
71.4M
      if (ns == NULL)
2035
0
        return false;
2036
71.4M
      if (ns == FullMatchState ||
2037
71.4M
          (ns > SpecialStateMax && ns->ninst_ > 0)) {
2038
517k
        extended = true;
2039
517k
        max->append(1, static_cast<char>(j));
2040
517k
        s = ns;
2041
517k
        break;
2042
517k
      }
2043
71.4M
    }
2044
520k
    if (!extended) {
2045
      // Done, no need for PrefixSuccessor.
2046
2.58k
      return true;
2047
2.58k
    }
2048
520k
  }
2049
2050
  // Stopped while still adding to *max - round aaaaaaaaaa... to aaaa...b
2051
1.82k
  PrefixSuccessor(max);
2052
2053
  // If there are no bytes left, we have no way to say "there is no maximum
2054
  // string".  We could make the interface more complicated and be able to
2055
  // return "there is no maximum but here is a minimum", but that seems like
2056
  // overkill -- the most common no-max case is all possible strings, so not
2057
  // telling the caller that the empty string is the minimum match isn't a
2058
  // great loss.
2059
1.82k
  if (max->empty())
2060
0
    return false;
2061
2062
1.82k
  return true;
2063
1.82k
}
2064
2065
// PossibleMatchRange for a Prog.
2066
4.62k
bool Prog::PossibleMatchRange(std::string* min, std::string* max, int maxlen) {
2067
  // Have to use dfa_longest_ to get all strings for full matches.
2068
  // For example, (a|aa) never matches aa in first-match mode.
2069
4.62k
  return GetDFA(kLongestMatch)->PossibleMatchRange(min, max, maxlen);
2070
4.62k
}
2071
2072
}  // namespace re2