Fast Matching of Regular Patterns with Synchronizing Counting
Abstract
Fast matching of regular expressions with bounded repetition, aka counting, such as (){,}, i.e., matching linear in the length of the text and independent of the repetition bounds, has been an open problem for at least two decades. We show that, for a wide class of regular expressions with counting, which we call synchronizing, fast matching is possible. We empirically show that the class covers nearly all counting used in usual applications of regex matching. This complexity result is based on an improvement and analysis of a recent matching algorithm that compiles regexes to deterministic counting-set automata (automata with registers that hold sets of numbers).
Full text
Fast Matching of Regular Patterns with Synchronizing Counting Luk´ aˇ s Hol´ ık , Juraj S´ ıˇ c( ), Lenka Turoˇ nov´ a , and Tom´ aˇ s Vojnar Brno University of Technology, Brno, Czech Republic {holik,sicjuraj,ituronova,vojnar}@fit.vut.cz Abstract. Fast matching of regular expressions with bounded repetition, aka counting, such as (ab){50,100}, i.e., matching linear in the length of the text and independent of the repetition bounds, has been an open problem for at least two decades. We show that, for a wide class of regular expressions with counting, which we call synchronizing, fast matching is possible. We empirically show that the class covers nearly all counting used in usual applications of regex matching. This complexity result is based on an improvement and analysis of a recent matching algorithm that compiles regexes to deterministic counting-set automata (automata with registers that hold sets of numbers). 1 Introduction Fast matching of regular expressions with bounded repetition, aka counting, has been an open problem for at least two decades (cf., e.g., [33]). The time complexity of the standard matching algorithms run on a regex such as .*a.{100}is, at best, dominated by the length of the text multiplied by the repetition bounds. This makes matching prone to unacceptable slowdowns since the length of the text as well as the repetition bounds are often large. In this paper, we provide a theoretical basis for matching of bounded repetition with a much more reliable performance. We show that a large and practical class of regexes with counting theoretically allows fast matching—in time independent of the counter bounds and linear in the length of the text. The problem also has a strong practical motivation. Regex matching is used for searching, data validation, detection of information leakage, parsing, replacing, data scraping, syntax highlighting, etc. It is natively supported in most programming languages [6], and ubiquitous (used in 30–40 % of Java, JavaScript, and Python software [7,39,8,5]). Efficiency and predictability of regex matching is important. An extreme run-time of matching can have serious consequences, such as a failed input validation against injection attacks [41] and events like the outage of Cloudflare services [18]. Regexes vulnerabilities are also a doorway for the ReDoS (regular expression denial of service) attack, in which the attacker crafts a text to overwhelm a matcher (as, e.g., in the case of the outage of StackOverflow [13] or the websites exposed due to their use of the popular Express.js framework [3]). ReDoS has been widely recognized as a common and serious threat [7,9,11], with counting in regexes begin especially dangerous [37]. © The Author(s) 2023 O. Kupferman and P. Sobocinski (Eds.): FoSSaCS 2023, LNCS 13992, pp. 392–412, 2023. https://doi.org/10.1007/978-3-031-30829-1 19
Fast Matching of Regular Patterns with Synchronizing Counting 393 Matching algorithms and complexity. The potential instability of the pattern matchers is in line with the worst-case complexity of the matching algorithms. The most widely used approach to matching is backtracking (used, e.g., in standard matchers of .NET, Python, Perl, PHP, Java, JavaScript, Ruby) for its simplicity and ease of implementation of advanced features such as back-references or look-arounds. It is, however, at worst exponential to the length of the matched text and prone to ReDoS. Even though this can be improved, for instance by memoization [11], the fastest matchers used in performance critical applications all use automata-based algorithms instead of backtracking. The basis of these approaches is Thompson’s algorithm [35] (also referred to as online NFA-simulation). Together with many optimizations, it is implemented in Intel’s Hyperscan [40]. When combined with caching, it becomes the on-the-fly subset construction of a DFA, also called online DFA-simulation (implemented in RE2 from Google, GNU grep, SRM, or the standard matcher of Rust [17,19,30,12]). Without counting, the major factor in the worst-case complexity is O(nm2), with nbeing the length of the text and mthe size of the number of character occurrences in the regex (mis smaller than size of the regex, the length of string defining it). We say that the character cost, i.e., the cost of extending the text with one character, is m2. This is the cost of iterating through transitions of an NFA with O(m)states and O(m2)transitions compiled from the regex by some classical construction [2,16,24]. Extending the syntax of regexes with bounded quantifiers (or counters), such as (ab){50,100}, increases the character complexity dramatically. Given kcounters with the maximum bound `, the number of NFA states rises to O(m`k), the number of transitions as well as the character cost to O((m`k)2). For instance, the minimal DFA for .*a.{k}(i.e., aappears kcharacters from the end) has more than 2kstates. Moreover, note that, since kis written as a decadic numeral, its value is exponential in the size of the regex. This makes matching with already moderately high kprone to significant slowdowns and ReDoS vulnerabilities with virtually every mainstream matcher (see [36,37]). At the same time, repetition bounds easily reach thousands, in extreme tens of millions (in real-life XML [4]). Writing a dangerous counting expression is easy and it is hard to identify. Security-critical solutions may be vulnerable to counting-related ReDoS [37] despite an extra effort spent in regex design and testing, hence developers sometimes avoid counting, use workarounds and restrict functionality. The problem of matching with bounded repetition has been addressed from the theoretical as well as from the practical perspective by a number of authors [15,4,22,26,31,20,25,36]. From these, the recent work [36] is the only one offering fast matching for a practically significant class of regexes. The algorithm of [36] compiles a regex with counting to a non-deterministic counting automaton (CA), an automaton with counters that can be incremented, reset, and compared with a constant. The crux of the problem is then to convert the CA to a succinct deterministic machine that could be simulated fast in matching. The work [36] achieves this by determinizing the CA into a counting-set automaton (CSA), an automaton with registers that hold sets of numbers. Its size is independent of the counter bounds and it updates the sets by a handful of operations that are all constant time, regardless the size of the sets. However, regexes outside the supported class do appear, the class has no syntactic characterization, and it is hard to recognize (as demonstrated also by an incorrect proposal of a syntactic
394 L. Holík et al. class in [36] itself). For instance, .*a{5}or (ab){5}are handled, but .*(aa){5}or .*(ab){5}are not (the requirement is technical, see Section 4). Our contribution. In this paper, we 1. generalize the algorithm of [36] to extend the class of handled regexes and 2. derive a useful syntactic characterization of the extended class. The derived class is characterized by flat counting (counting operators are not nested) where repetitions of each counted expression Rare synchronizing (a word from Rncannot have a prefix from Rn+1). It is the first clearly delimited practical class of regexes with counting that allows fast matching. It includes the easily recognizable and frequent case where every word in Rhas exactly one occurrence of a marker, a letter or a word from a finite set of markers that unambiguously identifies each occurrence of R(note that even this simple class was not handled by any previous fast algorithms, including [36]). In a our experiment with a large set of regexes from various sources, 99.6 % of non-trivial flat counting was synchronizing and 99.2 % was letter-marked. To obtain the results (1) and (2) above, we first modify the determinization of [36] to include the entire class of regexes with flat counting. In a nutshell, this is achieved by two changes: (i) We allow copying and uniting of sets stored in registers, and (ii) in the determinization, we index counters of the CA by its states to handle CA in which nondeterministic runs that reach different states reach different counter values. These modifications come with the main technical challenge that we solve in this paper: copying and uniting sets is not constant-time but linear to the size of the sets. This would make the character cost linear in the counter bound `again. To remove the dependency on the counter bounds, we augment the determinization by optimizations that avoid the copying and uniting. First, to alleviate the cost of uniting, we store intersections of sets stored in registers in new shared registers, so that the intersection does not contribute to the cost of uniting the registers. Then, to increase the impact of intersection sharing, we synchronize register updates in order to make their intersections larger. We then show that if the CSA does not replicate registers, i.e, each register can in a transition appear on the right-hand side of only one register assignment, then it never copies registers and the cost of unions can be amortised. Finally, we define the class of regexes with synchronizing counting for which the optimized CsA do not replicate counters so their simulation in matching is fast. Related work. In the context of regex matching, counting automata were used in several forms under several names (e.g. [20,36,4,15,31,32,33,14,23]). Besides [36] discussed above, other solutions to matching of counting regexes [15,4,22,26,31,20,25] handle small classes of regexes or do not allow matching linear in the text size and independent of counter bounds. The work [20] proposes a CA-to-CA determinization producing smaller automata than the explicit CA determinization for the limited class of monadic regexes, covered by letter-marked counting, and the size of their deterministic automata is still dependent on the counter bounds. The work [4] uses a notion of automata with counters of [15]. It focuses mostly on deterministic regexes, a class much smaller than regexes with synchronizing counting, and proposes a matching algorithm still dependent on the counter bounds. The paper [25] proposes an algorithm that takes time at
Fast Matching of Regular Patterns with Synchronizing Counting 395 worst quadratic to the length of the text. Extended FA (XFA) of [31,32] augment NFA with a scratch memory of bits that can represent counters, and their determinization is exponential in counter bounds already for regexes such as .*a.{k}. The counter-1- unambiguous regexes of [22,23] can be directly compiled into deterministic automata called FACs, similar to our CA, independent of counter bounds, but the class is limited, excluding e.g., .*a.{k}. 2 Preliminaries We use Nto denote the natural numbers including 0. For a set S,P(S)denotes its powerset and Pfin(S)is the set of all finite subsets of S. Afirst order language (f.o.l.) Γ= (F,P)consists of a set of function symbols F and a set of predicate symbols P. An interpretation Iof Γwith a domain DIassigns a function fI:Dn I→DIto each n-ary f∈Fand a function pI:Dn I→ {0,1}to each n-ary p∈P. An assignment of a set of variables Xin Iis a total function ν:X→DI. The set of terms TermsΓ,Xand the set QFFΓ,Xof quantifier free formulae (boolean combinations of atomic formulae) over Γand X, as well as the interpretation of a term, tI(ν), and a formula, ϕI(ν), are defined as usual. We denote by ν|=Iϕthat the formula ϕis satisfied (interpreted as true) by the assignment ν. It is then satisfiable. We drop the sub/superscript Iwhen it is clear from the context. We write ϕ[x]and t[x]to denote a unary formula ϕor term t, respectively, with the free variable x, and we may also abuse this notation to denote the term/formula with its only free variable replaced by x. We write tI(k)and ϕI(k)to denote the values tI({x7→ k})and ϕI({x7→ k}). For a set of formulae Ψ={ψ1,...,ψn}, the set Minterms(Ψ)consists of all minterms of Ψ, satisfiable conjunctions ϕ1∧···∧ϕnwhere for each i: 1 ≤i≤n,ϕiis ψior ¬ψi. We fix a finite alphabet Σof symbols/letters for the rest of the paper. Words are sequences of letters, with the empty word ε. The concatenation of words uand vis denoted u·v,uv for short. A set of words over Σis a language, the concatenation of languages is L·L0={u·v|u∈L∧v∈L0},LL0for short. Bounded iteration xi,i∈N, of a word or a language xis defined by x0=εfor a word, x0={ε}for a language, and xi+1=xi·x. Then x∗=Si∈Nxi. We consider a usual basic syntax of regular expressions (regexes), generated by the grammar R::=ε|a|(R)|RR |R|R|R*|R{m,n}where m∈N, n∈N∪∞, 0 ≤m, 0 <n,m≤n, and a∈Σ. We use R{m}for R{m,m}. Regexes containing a sub-expression with the counter R{m,n}or R{m}are called counting regexes and m,nare counter bounds. We denote by maxRthe maximum integer occurring in the counter bounds of regex Rand we denote the number of counters by cntR. A regex with flat counting does not have nested counting, that is, in a sub-regex S{m,n},S cannot contain counting. The language of a regex Ris constructed inductively to the structure: L(ε) = {ε},L(a) = {a}for a∈Σ,L(RR0) = L(R)·L(R0),L(R*) = L(R)∗, L(R|R0) = L(R)∪L(R0), and L(R{m,n}) = Sm≤i≤nL(R)i. We understand |R|simply as the length of the defining string, e.g. |(ab){10}| =8. We define ]Ras the number of character occurrences in R, formally, ]a=1 for a∈Σ,]ε=0, ](R)=]R{m,n}=]R, and ]R·S=]R|S=]R+]S. A(nondeterministic) automaton (NA) is a tuple A= (Q,∆,I,F)where Qis a set of states,∆is a set of transitions of the form q − {a} → rwith q,r∈Qand a∈Σ,I⊆Qis the
396 L. Holík et al. set of initial states, and F⊆Qis the set of final states. A run of Aover a word w= a1. . . anfrom state p0to pn,n≥0 is a sequence of transitions p0 − {a1} → p1,p1 − {a2} → p2, . . .,pn−1 − {an} → pnfrom ∆. The empty sequence is a run with p0=pnover ε. The run is accepting if p0∈Iand pn∈F, and the language L(A)of Ais the set of all words for which Ahas an accepting run. A state qis reachable if there is a run from Ito it. The size of the NA, |A|, is defined as the number of its states plus the number of its transitions. The automaton is deterministic (DA) iff |I|=1 and for every state qand symbol a,∆has at most one transition q − {a} → r. The subset construction transforms the NA to the DA with the same language DA(A) = (Q{} ,∆{} ,I{} ,F{} )where Q{} ⊆P(Q)and ∆{} are the smallest sets of states and transitions satisfying I{} ={I},∆{} has for each a∈Σand each S∈Q{} the transition S − {a} → {s0|s∈S∧s − {a} → s0∈∆}, and F{} ={S∈Q{} |S∩F6=/ 0}. When the set of states Qis finite, we talk about (deterministic) finite state automata (NFA, DFA).1 This paper is concerned with the problem of fast pattern matching, basically a membership test: given a regex Rand a text w, decide whether w∈L(R). While wmay be very long, Ris normally small, hence the dependence on |w|is the major factor in the complexity. The offline DFA simulation takes time linear in |w|. It (1) compiles Rinto an NFA NFA(R)(2) determinizes it, and (3) follows the DFA run over w(aka simulates the DFA on w), all in time and space Θ(2|NFA(R)|+|w|). The cost of determinization, exponential in |NFA(R)|, is however too impractical. Modern matchers such as Grep or RE2 [19,17] therefore use the techniques of online DFA simulation, where only the part of the DFA used for processing wis constructed. It reduces the complexity to O(min(2|NFA(R)|+|w|,|w| · |NFA(R)|)) (the first operand of min is the explicit determinization in case the entire DFA is constructed, plus the cost of DFA-simulation; the second operand is the cost of the online-DFA simulation, coming from that every step may incur construction of a new DFA state and transition in time O(|NFA(R)|)). For counting regexes, the factor |NFA(R)|depends linearly (or more if counting is nested) on maxRand thus exponentially on |R|. This makes counting very problematic in practice [36,37,33]. We will present a matching algorithm which is fast for a specific class of regexes, meaning that its run-time is still linear in |w|but is independent of maxR. 3 Counting Automata We use a rephrased definition of counting automata and counting-set automata of [36]. We will present them as a special case of a generic notion of automata with registers. Definition 1 (Automata with registers). An automaton with registers (RA) operated through an f.o.l. Γunder an interpretation Iis a tuple A = (X,Q,∆,I,F)where X is a set of variables called registers; Q is a finite set of states;∆is a finite set of transitions of the form q − {a,ϕ,u} → p where p,q∈Q, a ∈Σ, u :X→TermsΓ,Xis an update, and ϕ∈ QFFΓ,Xis a guard; I is a set of initial configurations, where a configuration is a pair of the form (q,m)where q ∈Q and m:X→DIis a register assignment called a memory; and F :Q→QFFΓ,Xis a final condition assignment. 1We do not require finiteness in the basic definition in order to avoid artificial restrictions of the notions of automata with registers/counters/counting sets defined later.
Fast Matching of Regular Patterns with Synchronizing Counting 397 The language of A, L(A), is defined as the language of its configuration automaton Conf(A). States of Conf(A)are configurations of A that are reachable. I is the set of initial states of Conf(A). It has a transition (q,m) − {a} → (q0,m0)iff (q,m)is reachable and A has a transition δ=q − {a,ϕ,u} → q0∈∆such that (q0,m0)is the image of (q,m)under δ, denoted (q0,m0) = δ(q,m), meaning that (1) δis enabled in (q,m),m|=ϕ, and (2) m0=u(m), i.e. m0(x) = u(x)I(m)for each x ∈X. We let δ(C) = {δ(c)|c∈C}for a set of configurations C. A configuration (q,m)is a final if m|=F(q). By runs of A we mean runs of Conf(A). The RA A is deterministic if Conf(A)is deterministic. The size of the RA is |A|=|Q|+∑δ∈∆|δ|where |δ|is the sum of the sizes of the update and the guard. Definition 2 (Counting automata). Acounting automaton (CA) is an automaton with registers, called counters, operated through the counting language Γcnt that contains the unary increment function, denoted x+1, constants 0and 1, and predicates x >k and x≤k, k ∈N, with the standard interpretation over natural numbers, that we denote Icnt. q0 x:=0 a1 b1 b2[x≥3] a x:=1 b x:=1 b x:=x b x:=x a;x<8 x:=x+1 b;x<8 x:=x+1 Fig. 1: CA(R)for R=((a|b)b){3,8}. The accepting condition of all states is ⊥except for b2whose accepting condition is written in the square brackets. Regexes with counting may be translated to CA by several methods ([36,33,14,23]). We use a slightly adapted version of [14]—an extension of Glushkov’s algorithm [16] to counting. For a regex R, it produces a CA CA(R) = (X,Q,∆,{α0},F). Figure 1shows an example of such CA. The construction is discussed in detail in [21], here we only overview the important properties needed in Sections 4-6: 1. Every occurrence Sof a counted sub-expression T{minS,maxS}of Rcorresponds to a unique counter xSand a substructure ASof CA(R). Outside AS,xSis inactive (a dead variable) and its value is 0, it is assigned 1 on entering AS, and every iteration through ASincrements the value of xSwhile reading a word from L(T). Our minor modification of [14] is related to the fact that the original assigns 1 to inactive counters while we need 0. 2. CA(R)has at most ]R+1 states, cntR.]R2transitions, cntRcounters. It has at most ]R2transitions if Ris flat. 3. CA(R)has a single initial configuration α0= (q0,s0)s.t. s0(xS) = 0 for each xS∈X. 4. Guards and final conditions are conjunctions consisting of at most one conjunct of the form minS≤xSor maxS>xSper counter xS∈X. A transition update may assign to xS∈Xonly one of the terms 0, 1, xS, and xS+1. It has no guard on xSif it is assigned xS, i.e. kept unchanged, it has the guard xS≥minSiff xSis reset to 0 or 1 (a counter cannot be reset before reaching its lower bound), and it has the guard xS<maxSiff xSis assigned xS+1 (counter can never exceed its maximum value maxS). Hence, a counter can never exceed maxR. 5. Flatness of Rtranslates to the fact that configurations of CA(R)assign a non-zero value to at most one counter. This implies that Conf(CA(R)) has at most |Q|.maxR states and also that CA(R)is Cartesian, a property that will be defined in Section 4 and is crucial for correctness of our CA determinization (Theorem 3in Section 6.)
398 L. Holík et al. A DFA can be obtained by the subset construction in the form DA(Conf(CA(R))), called explicit determinization. Due to the factor maxRin the size of Conf(CA(R)), the explicit determinization is exponential to maxReven if Ris flat, meaning doubly exponential to |R|(Rhas maxRwritten as a decadic numeral). If Ris not flat, then the factor maxRis replaced by (maxR)cntR. 4 Counter-subset Construction In this section, we formulate a modified version of determinization of CA from [36] that constructs a machine of a size independent of maxR. Our version handles the entire class of Cartesian CA (defined below) and in turn also all regexes with flat counting. The main idea of the determinization remains the same as in [36]. The standard subset construction is augmented with registers, we call them counting sets, that can store sets of counter values that would be generated by non-deterministic runs of the CA. The automata with counting-sets as registers are called counting-set automata. Our first modification of [36] is indexing of counters by states. In intuitively, this allows to handle cases such as a*(ba|ab){5}, where, after reading the first ab, the counter is either incremented or not (bis the first letter of the counted sub-expression or not). This would violate the uniformity property of CA necessary in [36]—the set of values generated by the non-deterministic CA runs must be the same for every CA state. In our modified version, values at distinct states are stored separately in registers indexed by those states and may differ. Then, in order to handle the indexed counters, we have to introduce a general assignment of counters, allowing to assign the union of other counters.2Intu- itively, when a run non-deterministically branches into several states, each branch needs to continue with its own copy of the set, stored in a counter indexed by the state. The union of sets is used when the branches join again. This brings a technical challenge that we solve in this work: how to simulate the counting-set automata fast when the set union and copy are used? The solution is presented in Sections 5and 6. Definition 3 (Counting-set automata). Acounting-set automaton (CSA) is an automaton with registers operated through the counting-set language Γset under the number-set interpretation I{} cnt where the language Γset extends the counting language Γcnt with the constant / 0, binary union ∪, and set-filter functions ∇ pwhere p is a predicate symbol of Γcnt. For simplicity, we restrict terms assigned to counters by transition updates to the form t =t1∪ ··· ∪ tnwhere each tiis either (a) a term of Γcnt or / 0, (b) of the form ∇p(t0)where t0is a term of Γcnt . Each tiis called an r-term of t. The domain of Iset is sets of natural numbers,P(N). The interpretation of the predicates and functions of Γcnt under Iset is derived from the base number interpretation of the same predicates and functions: A function returns the image of the set in the argument under the base semantics, f Iset (S) = {fIcnt (n)|n∈S}. A set satisfies a predicate if some of its elements satisfy the base semantics of that predicate, pIset (S)⇐⇒ ∃e∈S:pIcnt (e). Filters then filter out values that do not satisfy the base semantics of their predicate, ∇Iset p(S) = {e∈S|pIcnt (e)}. Finally, / 0is interpreted as 2[36] could assign to a counter xonly a constant or function of the current value of x.
Fast Matching of Regular Patterns with Synchronizing Counting 399 the empty set and ∪as the union of sets. We denote memories of the CSA by sto distinguish them from memories of CA. We write DCSA to abbreviate deterministic CSA. Less formally, registers of CSA hold sets of numbers and are manipulated by the increment x+1 of all values, assignment of constant sets {0},{1}, and / 0, denoted by 0, 1, and / 0, filtering out values smaller or larger than a constant, denoted ∇x≤k(x)and ∇x<k(x), and testing on a presence of a value xsatisfying x≤kor x<k,k∈N. We will present an algorithm that determinizes a CA A= (X,Q,∆,I,F), fixed for the rest of the section, into a DCSA DCSA(A) = (X{} ,Q{} ,∆ {} ,I{} ,F{} ). We assume that guards of transitions in ∆and final conditions are of the form Vx∈Ypx[x],Y⊆X, i.e. conjunctions with a at most a single atomic predicate per counter. This is satisfied by all CA(R), for any regex R(see the list of properties of CA(R)in Section 3).3 Runs of DCSA(A)will encode runs of DA(Conf(A)) obtained from the explicit determinization of A. Recall that the states DA(Conf(A)) are sets of configurations of A, pairs (q,m)of a state and a counter assignment. DCSA(A)will represent the sets of counter values within a DA state as run-time values of its registers. Particularly, for every state qand a counter xof the CA, DCSA(A)has a register xqin which it remembers, after reading a word w, the set of all values that xreaches in runs of the base CA on wending in q. Hence, we have X{} ={xq|x∈X∧q∈Q} Definition 4 (Encoding of sets of CA configurations). A state S ={(qi,mi)}n i=1of DA(Conf(A)) is encoded as the DCSA(A)configuration enc(S)=({qi}n i=1,s)where s(xq) = {mi(x)|qi=q}n i=1. Since a set of assignments appearing with the state qis broken down to sets of values of the individual counters, it disregards relations between values of different counters. For instance, in the DA state S1={(q,{x7→ 0,y7→ 0}),(q,{x7→ 1,y7→ 1})}, the values of xand yare either both 0 or both 1, but enc(S1)=(q,{xq7→ {0,1},yq7→ {0,1}}) does not retain this information. It is identical to the encoding of another DA state S2={(q,{x7→ 1,y7→ 0}),(q,{x7→ 0,y7→ 1})}. This is the same loss of information as in the so-called Cartesian abstraction. The encoding is hence precise and unambiguous only when we assume that inside the states of DA(A), the relations between counters are always unrestricted—there is no information to be lost. We then call the CA Cartesian, as defined below. The encoding function is then unambiguous, and we call the inverse function decoding, denoted dec. Definition 5 (Cartesian CA). Assuming the set of counters of A is X ={xi}m i=1, then a set C of configurations of A is Cartesian iff, for every state q of A, there exist sets N1,...,Nm⊆Nsuch that (q,{xi7→ ni}m i=1)∈C iff (n1,...,nm)∈N1×···×Nm. The CA A is Cartesian iff all states of DA(Conf(A)) are Cartesian. For instance, the DA states S1and S2above are not Cartesian, while S1∪S2is. Similarly as the regex to CA construction of [36], our regex to CA construction discussed in Section 3returns a Cartesian CA when called on a flat regex. 3Every CA can be transformed to this form by transforming the formulae to DNF and creating clones of transitions/states for individual clauses.
400 L. Holík et al. Subset construction for Cartesian CA. The algorithm below is a generalization of the subset construction. Let us denote by indexq(t)the term that arises from tby replacing every variable x∈Xby xq, analogously indexq(ϕ)for formulas. We have Q{} ⊆P(Q), the initial configuration I{} ={enc(I)}, and the final conditions assign to R∈Q{} the disjunction of the final conditions of its elements, F{} (R) = Wq∈Rindexq(F(q)). We will construct DCSA(A)which is deterministic and its runs encode the runs of DA DA(Conf(A)).Conf(DCSA(A)) will be isomorphic to DA(Conf(A)). For that, we need for each transition δof DA(Conf(A)) one unique transition of DCSA(A)over the same letter enabled in the encoding of the source of δand generating the encoding of the target of δ. In other words, we need for each transition dec(R,s) − {a} → dec(R0,s0)of DA(Conf(A)) one unique transition δ0=R − {a,ϕ,u} → R0∈∆{} with (R0,s0) = δ0(R,s). That transition δ0will be built by summarizing the effect of all base CA a-transitions enabled in the CA configurations of dec(R,s). To construct the transition δ0, we first translate each base transition δ=q − {a,ϕδ,uδ} → r∈ ∆into its set-version δ{} , supposed to transform an encoding of a (Cartesian) set Cof configurations, enc(C), into the encoding of the set of their images under δ,enc(δ(C)), and enabled if δis enabled for at least one configuration in C. To that end, assuming ϕδ=Vx∈Xpx[x], we (1) construct the update u∇ δfrom uδby substituting in every uδ(x),x∈Xvariables y∈Xby their filtered versions ∇py(y), (2) add indices to registers that mark the current state, resulting in the transition δ{} =q − {a,ϕ{} δ,u{} δ} → rwhere ϕ{} δ=indexq(ϕδ)and u{} δassigns to every xr,x∈Xthe term indexq(u∇ δ(x)). The states Q{} and the transitions ∆{} are then constructed as the smallest sets satisfying that enc(I)∈Q{} and every R∈Q{} has for every a∈Σthe outgoing transitions constructed as follows. Let {qj − {a,ϕj,uj} → rj}j∈Jfor some index set Jbe the set of constituent a-transitions for R, all a-transitions δ{} where δ∈∆originates in R. To achieve determinism, ∆{} has the transition R − {a,ψ,u} → R0for every minterm ψ∈Minterms({ϕj}j∈J). The update uand target R0are constructed from the set {qj − {a,ϕj,uj} → rj}j∈K,K⊆J, of constituent transitions with guards ϕjcompatible with the minterm ψ, i.e., with satisfiable ψ∧ϕj.R0is the set of their target states, R0={rj}j∈K, and u(x)unites all their update terms uj(x), i.e. u(x) = Sj∈Kuj(x), for each x∈X{} . Example 1. When showing examples of transition updates, we write x:=tto denote that u(x) = tand we omit the assignments x:=/ 0in CSA. Let R={p,q}and let the a-transitions originating at Rbe q − {a,>,x:=x} → s, p − {a,x<n,x:=x+1} → r, and p − {a,x≥m,x:=1} → s. They induce three constituent transitions for Rand a,q − {a,>,xs:=xq} → s,p − {a,xp<n,xr:=∇ x<n(xp)+1} → r, and p − {a,xp≥m,xs:=1} → s. A transition R − {a,ψ,u0} → R0is constructed for each of the following minterms ψ:xp<n∧xp≥m,¬xp<n∧ xp≥m,xp<n∧¬xp≥m,¬xp<n∧¬xp≥m. For the first one, all three constituent transitions are compatible and so the update u0is xr:=∇ x<n(xp)+1;xs:=xq∪1 (update of xr is taken from the first constituent transitions leading to r, update of xsis the union of the updates of the second two transitions leading to s) and the target state is R0={r,s}.ut DCSA(A)is deterministic since it has a single initial configuration and the guards of transitions originating in the same state are minterms. The size of DCSA(A)obviously depends only on the size of Aand not on the interpretation of the language. Especially,
Fast Matching of Regular Patterns with Synchronizing Counting 407 words from L(ab|ba)kmust have exactly 2ksymbols, and words from L(a(ab)*)kcan be uniquely split at the first ain the a(ab)*. In comparison, (a|aa){2,5}does not have synchronizing counting as a·a·ais a prefix of aa ·aa. Intuitively, there is no pair of paths through CA(S{m,n})starting at the same state, over the same word, ending in the same state, where the number of increments differs by two. In such case, DCSAa(CA(S{m,n})) would have to delay two increments, which our construction does not allow. The theorem below is proved in [21]. Theorem 4. Given a regex R with flat counting, the algorithm of Section 6returns DCSAa(CA(R)) if and only if R has synchronizing counting. Corollary 1. Regexes with flat synchronizing counting have a fast matching algorithm. Proof. From Theorems 3and 4. Counting with Markers. Even though designing and recognizing synchronizing counting is usually intuitive, it may also be tricky. For instance, (\\\\d+\\\\.){3}, from the database of real-world regexes we use in our experiment, has synchronizing counting, while ICE Dims.{92}(( ?(X|\d+)){13})does not.8A vast majority of real-world regexes we examined fortunately belong to very easily recognizable subclasses of synchronizing counting. The most wide-spread and easy to recognize are regexes with letter-marked counting, where every sub-expression S{m,n}has a set of marker letters such that every word from L(S)has exactly one occurrence of a marker letter. 9 Marker letters may be generalized to marker words, though, markers that can arise by concatenation of several words from L(S)cannot be used. The condition that has to be satisfied is that any word from L(S)k,k∈N, has exactly knon-overlapping occurrences of marker words as infixes. Another sufficient property of Sis that it has words of a uniform length. The idea of markers may be generalized further until the point when the set of marker words is specified by general regexes, when we get precisely the synchronizing counting. The regexes with letter-marked counting are easily human as well as machine recognizable (see a simple O(|R|2)-time algorithm in [21]). 8 Practical Considerations Although the main point of this work is the theoretical feasibility of fast matching with synchronizing counting, we will also argue that the results are of practical relevance. To this end, we show experimentally that synchronizing counting and marked counting cover a majority of practical regexes. We also give arguments that matching with the CSA constructed in Section 6can be done efficiently. 8An automated way of identifying synchronizing counting would be running the CSA-to-DCSA determinization from Section 6, but this is exponential to |R|. 9That letter-marked counting is a strict superset of the class that is in [36] conjectured as handled by the algorithm of [36]. The conjecture of [36] is also not correct, as shown in [21].
408 L. Holík et al. 8.1 Occurrence of Synchronizing Counting in Practice To substantiate the practical relevance of synchronizing counting regexes, we examined a large sample of practical regexes using a simple checker of letter-marked counting. The benchmark consists of over 540 000 regexes collected from (1) a large scale analysis of software projects [10]; (2) regexes used by network intrusion detection systems Snort [27], Bro [29], Sagan [34], and the academic papers [42,38]; (4) the RegExLib database of regexes [28]. From the regexes that we could parse10, 31 975 contained counting. We selected those with flat counting and with the sum of upper bounds of counters larger than 20 (as was done in [36] to filter out counting with small bounds that can be handled through counter unfolding and traditional methods)11. This left us with 5 751 regexes. From these, only 46 regexes (0.8%) have counting that is not letter-marked. Furthermore, we manually checked these regexes and we identified that 22 of them have synchronizing counting. We have therefore found only 24 regexes with non-synchronizing counting, i.e., 0.4 % of the examined set of regexes with flat counting. The 24 non-synchronizing regexes are listed in [21]. Some of them may clearly be rewritten with synchronizing counting, such as (.+){25}(.*), which can be rewritten as .{25,}(.*). We speculate that some of them might in fact represent a mistake, such as (.*){1,32000}[bc] where the counter matches the empty word, or (\n\s+)(criterion .*\n)(\s.+){1,99}where the \s.+ might have been intended as \s\S+ (\sare white spaces, \Sare all the other characters). Synchronizing counting seems to capture the intuition with which counting is often written, hence reporting non-synchronizing counting might help identifying bugs. By the same methodology and from a nearly identical benchmark, [36] arrived to a sample of 5 000 regexes with flat counting with the sum of bounds larger than 20. The algorithm of [36] did not cover 571 regexes from the 5 000, which is 11 % of the examined set of regexes with flat counting (in contrast to the 0.4% with non-synchronizing counting and the 0.8% with counting that is not letter-marked, measured on a slightly larger set of regexes). The two sets of regexes with flat counting, the 5 751 of ours and the 5 000 of [36], are not perfectly identical, however. Differences are to a small degree caused by differences in the base database ([36] uses about 18 more regexes that are proprietary and excludes 26 regexes with counter bounds larger than 1 000), and to a larger degree by small differences in the parsers. 8.2 Practical Efficiency of Matching with Synchronizing Counting The size and the worst-case time of simulation of DCSAa(CA(R)) are still exponential to the number of states of CA(R)(namely, O(22mm)and O(22mmn)where m=]Requals the number of states of CA(R), cf. Theorem 3). The potential problem is that the algorithm may generate at most 2mcounters, and this potentially threatens practicality of our matching algorithm. 10 We did not parse 38 558 regexes since their syntax was broken or contained some advanced features we do not support. 11 926 regexes contain nested counting and 25297 regexes contain small upper bounds.
Fast Matching of Regular Patterns with Synchronizing Counting 409 First, it should be noted that the min the exponent can be decreased from the size of the entire regex to the size of the counted sub-expression, which is usually very small. Then, although an efficient implementation is beyond the scope of this paper and we are leaving it as a future work, we give some indirect arguments for practicality of the CA-to-CSA algorithm.12 By the standard techniques of register allocation [1], it is possible to decrease the number of counters and counter assignments other than identity dramatically. In fact, simply eliminating needless renaming of counters and reusing the same name whenever possible, our algorithm creates CSA isomorphic to those of [36] when run on regexes handled by [36]. The work [36] already shows that simulating these CSA may be done efficiently and that it brings dramatic improvements over best matchers on countingintensive examples. In our experience with hand-simulating the algorithm on practical examples, cases not handled by [36] do not behave much differently, and the numbers of CSA counters do not have a strong tendency to explode. 9 Conclusions We have extended the regex matching algorithm of [36] and shown that the extended version allows fast pattern matching of so-called synchronising regexes, a class of regexes that we have newly introduced. The class of synchronising regexes significantly extends all previously known classes of regexes that allow fast matching and covers a majority of regexes appearing in practice (wrt. our empirical study). In the future, we plan to study extensions of the presented techniques to regexes with nested counting (non-flat). This will probably require a more sophisticated alternative of the offset-list data structure for sets, capable of storing relations of numbers. An interesting question is also how and when regexes can be rewritten to a synchronizing form and for what cost. Acknowledgment This work has been supported by the Czech Ministry of Education, Youth and Sports project LL1908 of the ERC.CZ programme, the Czech Science Foundation project 23- 06506S, and the FIT BUT internal project FIT-S-23-8151. References 1. Aho, A.V., Lam, M.S., Sethi, R., Ullman, J.D.: Compilers: Principles, Techniques, and Tools (2nd Edition). Addison Wesley (August 2006), http://www.amazon.ca/exec/obidos/ redirect?tag=citeulike09-20&path=ASIN/0321486811 12 A competitive matcher that runs on real-world regexes requires an extensive infrastructure, optimized data structures for the shared registers, and ideally an on-the-fly version of the CA- to-CSA determinization (similar to the online DFA simulation).
410 L. Holík et al. 2. Antimirov, V.: Partial derivatives of regular expressions and finite automaton constructions. Theoretical Computer Science 155(2), 291 – 319 (1996). https://doi.org/10.1016/ 0304-3975(95)00182-4,https://doi.org/10.1016/0304-3975(95)00182-4 3. Baldwin, A.: Regular expression denial of service affecting express.js. https://med ium.com/node-security/regularexpression-denial-of-service-affecting- express-js-9c397c164c43 (2016) 4. Bj¨ orklund, H., Martens, W., Timm, T.: Efficient incremental evaluation of succinct regular expressions. In: CIKM’15. ACM (2015). https://doi.org/10.1145/2806416.2806434 5. Chapman, C., Stolee, K.T.: Exploring regular expression usage and context in python. In: Zeller, A., Roychoudhury, A. (eds.) Proceedings of the 25th International Symposium on Software Testing and Analysis, ISSTA 2016, Saarbr¨ ucken, Germany, July 18-20, 2016. pp. 282–293. ACM (2016). https://doi.org/10.1145/2931037.2931073,https://doi. org/10.1145/2931037.2931073 6. contributors, W.: Regular expression—wikipedia (2019), https://en.wikipedia.org/w/ index.php?title=Regular_expression&%20oldid=852858998 7. Davis, J.C.: Rethinking regex engines to address ReDoS. In: ESEC/FSE’19. pp. 1256–1258. ACM (2019) 8. Davis, J.C., Coghlan, C.A., Servant, F., Lee, D.: The impact of regular expression denial of service (redos) in practice: an empirical study at the ecosystem scale. In: Leavens, G.T., Garcia, A., Pasareanu, C.S. (eds.) Proceedings of the 2018 ACM Joint Meeting on European Software Engineering Conference and Symposium on the Foundations of Software Engineering, ESEC/SIGSOFT FSE 2018, Lake Buena Vista, FL, USA, November 04-09, 2018. pp. 246–256. ACM (2018). https://doi.org/10.1145/3236024.3236027, https://doi.org/10.1145/3236024.3236027 9. Davis, J.C., Coghlan, C.A., Servant, F., Lee, D.: The impact of regular expression denial of service (ReDoS) in practice: An empirical study at the ecosystem scale. In: ESEC/FSE’18. pp. 246–256. ACM (2018) 10. Davis, J.C., Michael IV, L.G., Coghlan, C.A., Servant, F., Lee, D.: Why aren’t regular expressions a lingua franca? An empirical study on the re-use and portability of regular expressions. In: ESEC/FSE’19. pp. 1256–1258. ACM (2019) 11. Davis, J.C., Servant, F., Lee, D.: Using selective memoization to defeat regular expression denial of service (ReDoS). In: 42nd IEEE Symposium on Security and Privacy, SP 2021, San Francisco, CA, USA, 24-27 May 2021. pp. 1–17. IEEE (2021). https://doi.org/10. 1109/SP40001.2021.00032,https://doi.org/10.1109/SP40001.2021.00032 12. docs.rs: regex - rust. https://docs.rs/regex/1.5.4/regex/ (2021) 13. Exchange, S.: Outage postmortem. http://stackstatus.net/post/147710624694/ outage-postmortem-july-20-2016 (2016) 14. Gelade, W., Gyssens, M., Martens, W.: Regular expressions with counting: Weak versus strong determinism. In: Mathematical Foundations of Computer Science 2009. pp. 369– 381. Springer Berlin Heidelberg, Berlin, Heidelberg (2009). https://doi.org/10.1007/ 978-3-642-03816-7_32 15. Gelade, W., Gyssens, M., Martens, W.: Regular expressions with counting: Weak versus strong determinism. SIAM J. Comput. 41(1), 160–190 (2012). https://doi.org/10. 1137/100814196, extended version of paper in MFCS’09 16. Glushkov, V.M.: The abstract theory of automata. Russian Math. Surveys 16, 1–53 (1961). https://doi.org/10.1070/RM1961v016n05ABEH004112 17. Google: RE2.https://github.com/google/re2 18. Graham-Cumming, J.: Details of the Cloudflare outage on july 2, 2019. https://blog. cloudflare.com/details-of-the-cloudflare-outage-on-july-2-2019/ (2019) 19. Haertel, M., et al.: GNU grep.https://www.gnu.org/software/grep/
Fast Matching of Regular Patterns with Synchronizing Counting 411 20. Hol´ ık, L., Leng´ al, O., Saarikivi, O., Turoˇ nov´ a, L., Veanes, M., Vojnar, T.: Succinct determinisation of counting automata via sphere construction. In: Proc. of APLAS’19. LNCS, vol. 11893, pp. 468–489. Springer (2019). https://doi.org/10.1007/978-3-030-34175-6_ 24 21. Hol´ ık, L., S´ ıˇ c, J., Turoˇ nov´ a, L., Vojnar, T.: Fast matching of regular patterns with synchronizing counting (technical report). Tech. rep., Brno University of Technology (2023), https://doi.org/10.48550/arXiv.2301.12851 22. Hovland, D.: Regular expressions with numerical constraints and automata with counters. In: ICTAC. LNCS, vol. 5684, pp. 231–245. Springer (2009). https://doi.org/10.1007/ 978-3-642-03466-4_15 23. Hovland, D.: The membership problem for regular expressions with unordered concatenation and numerical constraints. In: Language and Automata Theory and Applications. pp. 313– 324. Springer Berlin Heidelberg, Berlin, Heidelberg (2012). https://doi.org/10.1007/ 978-3-642-28332-1_27 24. Hromkoviˇ c, J., Seibert, S., Wilke, T.: Translating regular expressions into small ε-free nondeterministic finite automata. In: Reischuk, R., Morvan, M. (eds.) STACS 97. pp. 55–66. Springer Berlin Heidelberg, Berlin, Heidelberg (1997) 25. Kilpel¨ ainen, P., Tuhkanen, R.: Regular expressions with numerical occurrence indicators - preliminary results. In: SPLST’03. pp. 163–173. University of Kuopio, Department of Computer Science (2003) 26. Kilpel¨ ainen, P., Tuhkanen, R.: One-unambiguity of regular expressions with numeric occurrence indicators. Information and Computation 205(6), 890–916 (2007). https://doi. org/10.1016/j.ic.2006.12.003 27. M. Roesch et al.: Snort: A Network Intrusion Detection and Prevention System,. http:// www.snort.org 28. RegExLib.com: The Internet’s first Regular Expression Library. http://regexlib.com/ 29. Robin Sommer et al.: The Bro Network Security Monitor, http://www.bro.org 30. Saarikivi, O., Veanes, M., Wan, T., Xu, E.: Symbolic regex matcher. In: Vojnar, T., Zhang, L. (eds.) TACAS’2019. LNCS, vol. 11427, pp. 372–378. Springer (2019). https://doi.org/ 10.1007/978-3-030-17462-0_24,https://doi.org/10.1007/978-3-030-17462-0_ 24 31. Smith, R., Estan, C., Jha, S.: XFA: faster signature matching with extended automata. In: IEEE Symposium on Security and Privacy. IEEE (2008). https://doi.org/10.1109/SP. 2008.14 32. Smith, R., Estan, C., Jha, S., Siahaan, I.: Fast signature matching using extended finite automaton (XFA). In: ICISS’08. LNCS, vol. 5352, pp. 158–172. Springer (2008). https: //doi.org/10.1007/978-3-540-89862-7_15 33. Sperberg-McQueen, M.: Notes on finite state automata with counters. https://www. w3.org/XML/2004/05/msm-cfa.html,https://www.w3.org/XML/2004/05/msm-cfa. html, accessed: 2018-08-08 34. The Sagan team: The Sagan Log Analysis Engine, https://quadrantsec.com/sagan_ log_analysis_engine/ 35. Thompson, K.: Programming techniques: Regular expression search algorithm. Commun. ACM 11(6), 419–422 (1968) 36. Turoˇ nov´ a, L., Hol´ ık, L., Leng´ al, O., Saarikivi, O., Veanes, M., Vojnar, T.: Regex matching with counting-set automata. Proc. ACM Program. Lang. 4(OOPSLA), 218:1–218:30 (2020) 37. Turoˇ nov´ a, L., Hol´ ık, L., Leng´ al, O., Veanes, M., Vojnar, T.: Counting in regexes considered harmful (2022) 38. ˇ Ceˇ ska, M., Havlena, V., Hol´ ık, L., Leng´ al, O., Vojnar, T.: Approximate reduction of finite automata for high-speed network intrusion detection. In: Proc. of TACAS’18. LNCS, vol. 10806. Springer (2018). https://doi.org/10.1007/978-3-319-89963-3_9
412 L. Holík et al. 39. Wang, P., Stolee, K.T.: How well are regular expressions tested in the wild? In: Leavens, G.T., Garcia, A., Pasareanu, C.S. (eds.) Proceedings of the 2018 ACM Joint Meeting on European Software Engineering Conference and Symposium on the Foundations of Software Engineering, ESEC/SIGSOFT FSE 2018, Lake Buena Vista, FL, USA, November 04-09, 2018. pp. 668–678. ACM (2018). https://doi.org/10.1145/3236024.3236072,https: //doi.org/10.1145/3236024.3236072 40. Wang, X., Hong, Y., Chang, H., Park, K., Langdale, G., Hu, J., Zhu, H.: Hyperscan: A fast multi-pattern regex matcher for modern CPUs. In: 16th USENIX Symposium on Networked Systems Design and Implementation (NSDI 19). pp. 631–648. USENIX Association, Boston, MA (Feb 2019), https://www.usenix.org/conference/nsdi19/ presentation/wang-xiang 41. W¨ ubbeling, M.: Regular expression security. ADMIN 55 (2020) 42. Yang, L., Karim, R., Ganapathy, V., Smith, R.: Improving NFA-based signature matching using ordered binary decision diagrams. In: Recent Advances in Intrusion Detection. pp. 58–78. Springer Berlin Heidelberg (2010) Open Access This chapter is licensed under the terms of the Creative Commons Attribution 4.0 International License (http://creativecommons.org/licenses/by/4.0/), which permits use, sharing, adaptation, distribution and reproduction in any medium or format, as long as you give appropriate credit to the original author(s) and the source, provide a link to the Creative Commons license and indicate if changes were made. The images or other third party material in this chapter are included in the chapter’s Creative Commons license, unless indicated otherwise in a credit line to the material. If material is not included in the chapter’s Creative Commons license and your intended use is not permitted by statutory regulation or exceeds the permitted use, you will need to obtain permission directly from the copyright holder.