532
|
1 |
% Chapter 1
|
|
2 |
|
|
3 |
\chapter{Introduction} % Main chapter title
|
|
4 |
|
|
5 |
\label{Introduction} % For referencing the chapter elsewhere, use \ref{Chapter1}
|
|
6 |
|
|
7 |
%----------------------------------------------------------------------------------------
|
|
8 |
|
|
9 |
% Define some commands to keep the formatting separated from the content
|
|
10 |
\newcommand{\keyword}[1]{\textbf{#1}}
|
|
11 |
\newcommand{\tabhead}[1]{\textbf{#1}}
|
|
12 |
\newcommand{\code}[1]{\texttt{#1}}
|
|
13 |
\newcommand{\file}[1]{\texttt{\bfseries#1}}
|
|
14 |
\newcommand{\option}[1]{\texttt{\itshape#1}}
|
|
15 |
|
|
16 |
%boxes
|
|
17 |
\newcommand*{\mybox}[1]{\framebox{\strut #1}}
|
|
18 |
|
|
19 |
%\newcommand{\sflataux}[1]{\textit{sflat}\_\textit{aux} \, #1}
|
|
20 |
\newcommand\sflat[1]{\llparenthesis #1 \rrparenthesis }
|
|
21 |
\newcommand{\ASEQ}[3]{\textit{ASEQ}_{#1} \, #2 \, #3}
|
543
|
22 |
\newcommand{\bderssimp}[2]{#1 \backslash_{bsimps} #2}
|
532
|
23 |
\newcommand{\rderssimp}[2]{#1 \backslash_{rsimp} #2}
|
564
|
24 |
\def\derssimp{\textit{ders}\_\textit{simp}}
|
557
|
25 |
\def\rders{\textit{rders}}
|
532
|
26 |
\newcommand{\bders}[2]{#1 \backslash #2}
|
|
27 |
\newcommand{\bsimp}[1]{\textit{bsimp}(#1)}
|
591
|
28 |
\def\bsimps{\textit{bsimp}}
|
554
|
29 |
\newcommand{\rsimp}[1]{\textit{rsimp}\; #1}
|
532
|
30 |
\newcommand{\sflataux}[1]{\llparenthesis #1 \rrparenthesis'}
|
|
31 |
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}%
|
|
32 |
\newcommand{\denote}{\stackrel{\mbox{\scriptsize denote}}{=}}%
|
|
33 |
\newcommand{\ZERO}{\mbox{\bf 0}}
|
|
34 |
\newcommand{\ONE}{\mbox{\bf 1}}
|
|
35 |
\newcommand{\AALTS}[2]{\oplus {\scriptstyle #1}\, #2}
|
555
|
36 |
\newcommand{\rdistinct}[2]{\textit{rdistinct} \;\; #1 \;\; #2}
|
594
|
37 |
\def\rdistincts{\textit{rdistinct}}
|
556
|
38 |
\def\rDistinct{\textit{rdistinct}}
|
532
|
39 |
\newcommand\hflat[1]{\llparenthesis #1 \rrparenthesis_*}
|
|
40 |
\newcommand\hflataux[1]{\llparenthesis #1 \rrparenthesis_*'}
|
|
41 |
\newcommand\createdByStar[1]{\textit{createdByStar}(#1)}
|
|
42 |
|
|
43 |
\newcommand\myequiv{\mathrel{\stackrel{\makebox[0pt]{\mbox{\normalfont\tiny equiv}}}{=}}}
|
|
44 |
|
564
|
45 |
\def\case{\textit{case}}
|
554
|
46 |
\def\sequal{\stackrel{\mbox{\scriptsize rsimp}}{=}}
|
|
47 |
\def\rsimpalts{\textit{rsimp}_{ALTS}}
|
|
48 |
\def\good{\textit{good}}
|
|
49 |
\def\btrue{\textit{true}}
|
|
50 |
\def\bfalse{\textit{false}}
|
542
|
51 |
\def\bnullable{\textit{bnullable}}
|
543
|
52 |
\def\bnullables{\textit{bnullables}}
|
538
|
53 |
\def\Some{\textit{Some}}
|
|
54 |
\def\None{\textit{None}}
|
537
|
55 |
\def\code{\textit{code}}
|
532
|
56 |
\def\decode{\textit{decode}}
|
|
57 |
\def\internalise{\textit{internalise}}
|
|
58 |
\def\lexer{\mathit{lexer}}
|
|
59 |
\def\mkeps{\textit{mkeps}}
|
557
|
60 |
\newcommand{\rder}[2]{#2 \backslash_r #1}
|
532
|
61 |
|
585
|
62 |
\def\rerases{\textit{rerase}}
|
|
63 |
|
554
|
64 |
\def\nonnested{\textit{nonnested}}
|
532
|
65 |
\def\AZERO{\textit{AZERO}}
|
558
|
66 |
\def\sizeNregex{\textit{sizeNregex}}
|
532
|
67 |
\def\AONE{\textit{AONE}}
|
|
68 |
\def\ACHAR{\textit{ACHAR}}
|
|
69 |
|
585
|
70 |
\def\simpsulz{\textit{simp}_{Sulz}}
|
|
71 |
|
557
|
72 |
\def\scfrewrites{\stackrel{*}{\rightsquigarrow_{scf}}}
|
555
|
73 |
\def\frewrite{\rightsquigarrow_f}
|
|
74 |
\def\hrewrite{\rightsquigarrow_h}
|
|
75 |
\def\grewrite{\rightsquigarrow_g}
|
|
76 |
\def\frewrites{\stackrel{*}{\rightsquigarrow_f}}
|
|
77 |
\def\hrewrites{\stackrel{*}{\rightsquigarrow_h}}
|
|
78 |
\def\grewrites{\stackrel{*}{\rightsquigarrow_g}}
|
538
|
79 |
\def\fuse{\textit{fuse}}
|
|
80 |
\def\bder{\textit{bder}}
|
542
|
81 |
\def\der{\textit{der}}
|
532
|
82 |
\def\POSIX{\textit{POSIX}}
|
|
83 |
\def\ALTS{\textit{ALTS}}
|
|
84 |
\def\ASTAR{\textit{ASTAR}}
|
|
85 |
\def\DFA{\textit{DFA}}
|
538
|
86 |
\def\NFA{\textit{NFA}}
|
532
|
87 |
\def\bmkeps{\textit{bmkeps}}
|
543
|
88 |
\def\bmkepss{\textit{bmkepss}}
|
532
|
89 |
\def\retrieve{\textit{retrieve}}
|
|
90 |
\def\blexer{\textit{blexer}}
|
|
91 |
\def\flex{\textit{flex}}
|
573
|
92 |
\def\inj{\textit{inj}}
|
564
|
93 |
\def\Empty{\textit{Empty}}
|
567
|
94 |
\def\Left{\textit{Left}}
|
|
95 |
\def\Right{\textit{Right}}
|
573
|
96 |
\def\Stars{\textit{Stars}}
|
|
97 |
\def\Char{\textit{Char}}
|
|
98 |
\def\Seq{\textit{Seq}}
|
532
|
99 |
\def\Der{\textit{Der}}
|
|
100 |
\def\Ders{\textit{Ders}}
|
|
101 |
\def\nullable{\mathit{nullable}}
|
|
102 |
\def\Z{\mathit{Z}}
|
|
103 |
\def\S{\mathit{S}}
|
|
104 |
\def\rup{r^\uparrow}
|
|
105 |
%\def\bderssimp{\mathit{bders}\_\mathit{simp}}
|
|
106 |
\def\distinctWith{\textit{distinctWith}}
|
|
107 |
\def\lf{\textit{lf}}
|
|
108 |
\def\PD{\textit{PD}}
|
|
109 |
\def\suffix{\textit{Suffix}}
|
543
|
110 |
\def\distinctBy{\textit{distinctBy}}
|
558
|
111 |
\def\starupdate{\textit{starUpdate}}
|
|
112 |
\def\starupdates{\textit{starUpdates}}
|
|
113 |
|
532
|
114 |
|
|
115 |
\def\size{\mathit{size}}
|
|
116 |
\def\rexp{\mathbf{rexp}}
|
|
117 |
\def\simp{\mathit{simp}}
|
|
118 |
\def\simpALTs{\mathit{simp}\_\mathit{ALTs}}
|
|
119 |
\def\map{\mathit{map}}
|
|
120 |
\def\distinct{\mathit{distinct}}
|
|
121 |
\def\blexersimp{\mathit{blexer}\_\mathit{simp}}
|
590
|
122 |
\def\blexerStrong{\textit{blexerStrong}}
|
|
123 |
\def\bsimpStrong{\textit{bsimpStrong}}
|
591
|
124 |
\def\bdersStrongs{\textit{bdersStrong}}
|
590
|
125 |
\newcommand{\bdersStrong}[2]{#1 \backslash_{bsimpStrongs} #2}
|
|
126 |
|
532
|
127 |
\def\map{\textit{map}}
|
|
128 |
\def\rrexp{\textit{rrexp}}
|
554
|
129 |
\newcommand\rnullable[1]{\textit{rnullable} \; #1 }
|
532
|
130 |
\newcommand\rsize[1]{\llbracket #1 \rrbracket_r}
|
|
131 |
\newcommand\asize[1]{\llbracket #1 \rrbracket}
|
543
|
132 |
\newcommand\rerase[1]{ (#1)_{\downarrow_r}}
|
|
133 |
|
538
|
134 |
\newcommand\ChristianComment[1]{\textcolor{blue}{#1}\\}
|
532
|
135 |
|
543
|
136 |
|
|
137 |
\def\rflts{\textit{rflts}}
|
|
138 |
\def\rrewrite{\textit{rrewrite}}
|
|
139 |
\def\bsimpalts{\textit{bsimp}_{ALTS}}
|
|
140 |
|
532
|
141 |
\def\erase{\textit{erase}}
|
|
142 |
\def\STAR{\textit{STAR}}
|
|
143 |
\def\flts{\textit{flts}}
|
|
144 |
|
|
145 |
|
579
|
146 |
\def\zeroable{\textit{zeroable}}
|
|
147 |
\def\nub{\textit{nub}}
|
|
148 |
\def\filter{\textit{filter}}
|
|
149 |
\def\not{\textit{not}}
|
|
150 |
|
|
151 |
|
|
152 |
|
532
|
153 |
\def\RZERO{\mathbf{0}_r }
|
|
154 |
\def\RONE{\mathbf{1}_r}
|
|
155 |
\newcommand\RCHAR[1]{\mathbf{#1}_r}
|
|
156 |
\newcommand\RSEQ[2]{#1 \cdot #2}
|
558
|
157 |
\newcommand\RALTS[1]{\sum #1}
|
532
|
158 |
\newcommand\RSTAR[1]{#1^*}
|
558
|
159 |
\newcommand\vsuf[2]{\textit{Suffix} \;#1\;#2}
|
532
|
160 |
|
538
|
161 |
|
|
162 |
|
590
|
163 |
|
|
164 |
\lstdefinestyle{myScalastyle}{
|
|
165 |
frame=tb,
|
|
166 |
language=scala,
|
|
167 |
aboveskip=3mm,
|
|
168 |
belowskip=3mm,
|
|
169 |
showstringspaces=false,
|
|
170 |
columns=flexible,
|
|
171 |
basicstyle={\small\ttfamily},
|
|
172 |
numbers=none,
|
|
173 |
numberstyle=\tiny\color{gray},
|
|
174 |
keywordstyle=\color{blue},
|
|
175 |
commentstyle=\color{dkgreen},
|
|
176 |
stringstyle=\color{mauve},
|
|
177 |
frame=single,
|
|
178 |
breaklines=true,
|
|
179 |
breakatwhitespace=true,
|
|
180 |
tabsize=3,
|
538
|
181 |
}
|
|
182 |
|
590
|
183 |
|
532
|
184 |
%----------------------------------------------------------------------------------------
|
|
185 |
%This part is about regular expressions, Brzozowski derivatives,
|
|
186 |
%and a bit-coded lexing algorithm with proven correctness and time bounds.
|
|
187 |
|
|
188 |
%TODO: look up snort rules to use here--give readers idea of what regexes look like
|
|
189 |
|
|
190 |
\begin{figure}
|
|
191 |
\centering
|
|
192 |
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
|
|
193 |
\begin{tikzpicture}
|
|
194 |
\begin{axis}[
|
|
195 |
xlabel={$n$},
|
|
196 |
x label style={at={(1.05,-0.05)}},
|
|
197 |
ylabel={time in secs},
|
|
198 |
enlargelimits=false,
|
|
199 |
xtick={0,5,...,30},
|
|
200 |
xmax=33,
|
|
201 |
ymax=35,
|
|
202 |
ytick={0,5,...,30},
|
|
203 |
scaled ticks=false,
|
|
204 |
axis lines=left,
|
|
205 |
width=5cm,
|
|
206 |
height=4cm,
|
|
207 |
legend entries={JavaScript},
|
|
208 |
legend pos=north west,
|
|
209 |
legend cell align=left]
|
|
210 |
\addplot[red,mark=*, mark options={fill=white}] table {re-js.data};
|
|
211 |
\end{axis}
|
|
212 |
\end{tikzpicture}
|
|
213 |
&
|
|
214 |
\begin{tikzpicture}
|
|
215 |
\begin{axis}[
|
|
216 |
xlabel={$n$},
|
|
217 |
x label style={at={(1.05,-0.05)}},
|
|
218 |
%ylabel={time in secs},
|
|
219 |
enlargelimits=false,
|
|
220 |
xtick={0,5,...,30},
|
|
221 |
xmax=33,
|
|
222 |
ymax=35,
|
|
223 |
ytick={0,5,...,30},
|
|
224 |
scaled ticks=false,
|
|
225 |
axis lines=left,
|
|
226 |
width=5cm,
|
|
227 |
height=4cm,
|
|
228 |
legend entries={Python},
|
|
229 |
legend pos=north west,
|
|
230 |
legend cell align=left]
|
|
231 |
\addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data};
|
|
232 |
\end{axis}
|
|
233 |
\end{tikzpicture}
|
|
234 |
&
|
|
235 |
\begin{tikzpicture}
|
|
236 |
\begin{axis}[
|
|
237 |
xlabel={$n$},
|
|
238 |
x label style={at={(1.05,-0.05)}},
|
|
239 |
%ylabel={time in secs},
|
|
240 |
enlargelimits=false,
|
|
241 |
xtick={0,5,...,30},
|
|
242 |
xmax=33,
|
|
243 |
ymax=35,
|
|
244 |
ytick={0,5,...,30},
|
|
245 |
scaled ticks=false,
|
|
246 |
axis lines=left,
|
|
247 |
width=5cm,
|
|
248 |
height=4cm,
|
|
249 |
legend entries={Java 8},
|
|
250 |
legend pos=north west,
|
|
251 |
legend cell align=left]
|
|
252 |
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data};
|
|
253 |
\end{axis}
|
|
254 |
\end{tikzpicture}\\
|
|
255 |
\multicolumn{3}{c}{Graphs: Runtime for matching $(a^*)^*\,b$ with strings
|
|
256 |
of the form $\underbrace{aa..a}_{n}$.}
|
|
257 |
\end{tabular}
|
|
258 |
\caption{aStarStarb} \label{fig:aStarStarb}
|
|
259 |
\end{figure}
|
|
260 |
|
|
261 |
|
|
262 |
|
|
263 |
|
538
|
264 |
|
|
265 |
Regular expressions are widely used in computer science:
|
|
266 |
be it in text-editors \parencite{atomEditor} with syntax highlighting and auto-completion;
|
|
267 |
command-line tools like $\mathit{grep}$ that facilitate easy
|
|
268 |
text-processing; network intrusion
|
|
269 |
detection systems that reject suspicious traffic; or compiler
|
|
270 |
front ends--the majority of the solutions to these tasks
|
|
271 |
involve lexing with regular
|
|
272 |
expressions.
|
|
273 |
Given its usefulness and ubiquity, one would imagine that
|
|
274 |
modern regular expression matching implementations
|
|
275 |
are mature and fully studied.
|
|
276 |
Indeed, in a popular programming language' regex engine,
|
|
277 |
supplying it with regular expressions and strings, one can
|
|
278 |
get rich matching information in a very short time.
|
|
279 |
Some network intrusion detection systems
|
|
280 |
use regex engines that are able to process
|
|
281 |
megabytes or even gigabytes of data per second \parencite{Turo_ov__2020}.
|
|
282 |
Unfortunately, this is not the case for $\mathbf{all}$ inputs.
|
|
283 |
%TODO: get source for SNORT/BRO's regex matching engine/speed
|
|
284 |
|
|
285 |
|
|
286 |
Take $(a^*)^*\,b$ and ask whether
|
|
287 |
strings of the form $aa..a$ match this regular
|
|
288 |
expression. Obviously this is not the case---the expected $b$ in the last
|
|
289 |
position is missing. One would expect that modern regular expression
|
|
290 |
matching engines can find this out very quickly. Alas, if one tries
|
|
291 |
this example in JavaScript, Python or Java 8, even with strings of a small
|
|
292 |
length, say around 30 $a$'s, one discovers that
|
|
293 |
this decision takes crazy time to finish given the simplicity of the problem.
|
|
294 |
This is clearly exponential behaviour, and
|
|
295 |
is triggered by some relatively simple regex patterns, as the graphs
|
|
296 |
in \ref{fig:aStarStarb} show.
|
|
297 |
|
|
298 |
|
|
299 |
|
|
300 |
|
|
301 |
\ChristianComment{Superlinear I just leave out the explanation
|
|
302 |
which I find once used would distract the flow. Plus if i just say exponential
|
|
303 |
here the 2016 event in StackExchange was not exponential, but just quardratic so would be
|
|
304 |
in accurate}
|
|
305 |
|
|
306 |
This superlinear blowup in regular expression engines
|
|
307 |
had repeatedly caused grief in real life.
|
|
308 |
For example, on 20 July 2016 one evil
|
532
|
309 |
regular expression brought the webpage
|
|
310 |
\href{http://stackexchange.com}{Stack Exchange} to its
|
538
|
311 |
knees.\footnote{\url{https://stackstatus.net/post/147710624694/outage-postmortem-july-20-2016}(Last accessed in 2019)}
|
532
|
312 |
In this instance, a regular expression intended to just trim white
|
|
313 |
spaces from the beginning and the end of a line actually consumed
|
538
|
314 |
massive amounts of CPU resources---causing web servers to grind to a
|
|
315 |
halt. In this example, the time needed to process
|
532
|
316 |
the string was $O(n^2)$ with respect to the string length. This
|
|
317 |
quadratic overhead was enough for the homepage of Stack Exchange to
|
|
318 |
respond so slowly that the load balancer assumed a $\mathit{DoS}$
|
|
319 |
attack and therefore stopped the servers from responding to any
|
|
320 |
requests. This made the whole site become unavailable.
|
538
|
321 |
|
532
|
322 |
A more recent example is a global outage of all Cloudflare servers on 2 July
|
|
323 |
2019. A poorly written regular expression exhibited exponential
|
|
324 |
behaviour and exhausted CPUs that serve HTTP traffic. Although the outage
|
|
325 |
had several causes, at the heart was a regular expression that
|
|
326 |
was used to monitor network
|
538
|
327 |
traffic.\footnote{\url{https://blog.cloudflare.com/details-of-the-cloudflare-outage-on-july-2-2019/}(Last accessed in 2022)}
|
532
|
328 |
%TODO: data points for some new versions of languages
|
|
329 |
These problems with regular expressions
|
|
330 |
are not isolated events that happen
|
|
331 |
very occasionally, but actually widespread.
|
|
332 |
They occur so often that they get a
|
|
333 |
name--Regular-Expression-Denial-Of-Service (ReDoS)
|
|
334 |
attack.
|
538
|
335 |
\citeauthor{Davis18} detected more
|
532
|
336 |
than 1000 super-linear (SL) regular expressions
|
|
337 |
in Node.js, Python core libraries, and npm and pypi.
|
|
338 |
They therefore concluded that evil regular expressions
|
538
|
339 |
are problems "more than a parlour trick", but one that
|
532
|
340 |
requires
|
|
341 |
more research attention.
|
|
342 |
|
538
|
343 |
|
|
344 |
But the problems are not limited to slowness on certain
|
|
345 |
cases.
|
|
346 |
Another thing about these libraries is that there
|
|
347 |
is no correctness guarantee.
|
|
348 |
In some cases, they either fail to generate a lexing result when there exists a match,
|
|
349 |
or give results that are inconsistent with the $\POSIX$ standard.
|
|
350 |
A concrete example would be
|
|
351 |
the regex
|
|
352 |
\begin{verbatim}
|
|
353 |
(aba|ab|a)*
|
|
354 |
\end{verbatim}
|
|
355 |
and the string
|
|
356 |
\begin{verbatim}
|
|
357 |
ababa
|
|
358 |
\end{verbatim}
|
|
359 |
The correct $\POSIX$ match for the above would be
|
|
360 |
with the entire string $ababa$,
|
|
361 |
split into two Kleene star iterations, $[ab] [aba]$ at positions
|
|
362 |
$[0, 2), [2, 5)$
|
|
363 |
respectively.
|
|
364 |
But trying this out in regex101\parencite{regex101}
|
|
365 |
with different language engines would yield
|
|
366 |
the same two fragmented matches: $[aba]$ at $[0, 3)$
|
|
367 |
and $a$ at $[4, 5)$.
|
|
368 |
|
|
369 |
Kuklewicz\parencite{KuklewiczHaskell} commented that most regex libraries are not
|
|
370 |
correctly implementing the POSIX (maximum-munch)
|
|
371 |
rule of regular expression matching.
|
|
372 |
|
|
373 |
As Grathwohl\parencite{grathwohl2014crash} commented,
|
|
374 |
\begin{center}
|
|
375 |
``The POSIX strategy is more complicated than the greedy because of the dependence on information about the length of matched strings in the various subexpressions.''
|
|
376 |
\end{center}
|
|
377 |
|
|
378 |
|
|
379 |
To summarise the above, regular expressions are important.
|
|
380 |
They are popular and programming languages' library functions
|
|
381 |
for them are very fast on non-catastrophic cases.
|
|
382 |
But there are problems with current practical implementations.
|
|
383 |
First thing is that the running time might blow up.
|
|
384 |
The second problem is that they might be error-prone on certain
|
543
|
385 |
very simple cases.
|
538
|
386 |
In the next part of the chapter, we will look into reasons why
|
|
387 |
certain regex engines are running horribly slow on the "catastrophic"
|
|
388 |
cases and propose a solution that addresses both of these problems
|
|
389 |
based on Brzozowski and Sulzmann and Lu's work.
|
|
390 |
|
|
391 |
|
|
392 |
\section{Why are current regex engines slow?}
|
532
|
393 |
|
|
394 |
%find literature/find out for yourself that REGEX->DFA on basic regexes
|
|
395 |
%does not blow up the size
|
|
396 |
Shouldn't regular expression matching be linear?
|
|
397 |
How can one explain the super-linear behaviour of the
|
|
398 |
regex matching engines we have?
|
|
399 |
The time cost of regex matching algorithms in general
|
538
|
400 |
involve two different phases, and different things can go differently wrong on
|
|
401 |
these phases.
|
|
402 |
$\DFA$s usually have problems in the first (construction) phase
|
|
403 |
, whereas $\NFA$s usually run into trouble
|
|
404 |
on the second phase.
|
|
405 |
|
|
406 |
\subsection{Different Phases of a Matching/Lexing Algorithm}
|
|
407 |
|
|
408 |
|
|
409 |
Most lexing algorithms can be roughly divided into
|
|
410 |
two phases during its run.
|
|
411 |
The first phase is the "construction" phase,
|
|
412 |
in which the algorithm builds some
|
|
413 |
suitable data structure from the input regex $r$, so that
|
|
414 |
it can be easily operated on later.
|
|
415 |
We denote
|
|
416 |
the time cost for such a phase by $P_1(r)$.
|
|
417 |
The second phase is the lexing phase, when the input string
|
|
418 |
$s$ is read and the data structure
|
|
419 |
representing that regex $r$ is being operated on.
|
|
420 |
We represent the time
|
532
|
421 |
it takes by $P_2(r, s)$.\\
|
538
|
422 |
|
|
423 |
For $\mathit{DFA}$,
|
532
|
424 |
we have $P_2(r, s) = O( |s| )$,
|
|
425 |
because we take at most $|s|$ steps,
|
|
426 |
and each step takes
|
|
427 |
at most one transition--
|
|
428 |
a deterministic-finite-automata
|
|
429 |
by definition has at most one state active and at most one
|
|
430 |
transition upon receiving an input symbol.
|
|
431 |
But unfortunately in the worst case
|
538
|
432 |
$P_1(r) = O(exp^{|r|})$. An example will be given later.
|
|
433 |
|
|
434 |
|
532
|
435 |
For $\mathit{NFA}$s, we have $P_1(r) = O(|r|)$ if we do not unfold
|
|
436 |
expressions like $r^n$ into $\underbrace{r \cdots r}_{\text{n copies of r}}$.
|
|
437 |
The $P_2(r, s)$ is bounded by $|r|\cdot|s|$, if we do not backtrack.
|
|
438 |
On the other hand, if backtracking is used, the worst-case time bound bloats
|
538
|
439 |
to $|r| * 2^|s|$.
|
532
|
440 |
%on the input
|
|
441 |
%And when calculating the time complexity of the matching algorithm,
|
|
442 |
%we are assuming that each input reading step requires constant time.
|
|
443 |
%which translates to that the number of
|
|
444 |
%states active and transitions taken each time is bounded by a
|
|
445 |
%constant $C$.
|
|
446 |
%But modern regex libraries in popular language engines
|
|
447 |
% often want to support much richer constructs than just
|
|
448 |
% sequences and Kleene stars,
|
|
449 |
%such as negation, intersection,
|
|
450 |
%bounded repetitions and back-references.
|
|
451 |
%And de-sugaring these "extended" regular expressions
|
|
452 |
%into basic ones might bloat the size exponentially.
|
|
453 |
%TODO: more reference for exponential size blowup on desugaring.
|
538
|
454 |
|
|
455 |
\subsection{Why $\mathit{DFA}s$ can be slow in the first phase}
|
|
456 |
|
|
457 |
|
532
|
458 |
The good things about $\mathit{DFA}$s is that once
|
|
459 |
generated, they are fast and stable, unlike
|
|
460 |
backtracking algorithms.
|
538
|
461 |
However, they do not scale well with bounded repetitions.
|
532
|
462 |
|
538
|
463 |
\subsubsection{Problems with Bounded Repetitions}
|
532
|
464 |
Bounded repetitions, usually written in the form
|
|
465 |
$r^{\{c\}}$ (where $c$ is a constant natural number),
|
|
466 |
denotes a regular expression accepting strings
|
|
467 |
that can be divided into $c$ substrings, where each
|
|
468 |
substring is in $r$.
|
|
469 |
For the regular expression $(a|b)^*a(a|b)^{\{2\}}$,
|
|
470 |
an $\mathit{NFA}$ describing it would look like:
|
|
471 |
\begin{center}
|
|
472 |
\begin{tikzpicture}[shorten >=1pt,node distance=2cm,on grid,auto]
|
|
473 |
\node[state,initial] (q_0) {$q_0$};
|
|
474 |
\node[state, red] (q_1) [right=of q_0] {$q_1$};
|
|
475 |
\node[state, red] (q_2) [right=of q_1] {$q_2$};
|
|
476 |
\node[state, accepting, red](q_3) [right=of q_2] {$q_3$};
|
|
477 |
\path[->]
|
|
478 |
(q_0) edge node {a} (q_1)
|
|
479 |
edge [loop below] node {a,b} ()
|
|
480 |
(q_1) edge node {a,b} (q_2)
|
|
481 |
(q_2) edge node {a,b} (q_3);
|
|
482 |
\end{tikzpicture}
|
|
483 |
\end{center}
|
|
484 |
The red states are "countdown states" which counts down
|
|
485 |
the number of characters needed in addition to the current
|
|
486 |
string to make a successful match.
|
|
487 |
For example, state $q_1$ indicates a match that has
|
|
488 |
gone past the $(a|b)^*$ part of $(a|b)^*a(a|b)^{\{2\}}$,
|
|
489 |
and just consumed the "delimiter" $a$ in the middle, and
|
|
490 |
need to match 2 more iterations of $(a|b)$ to complete.
|
|
491 |
State $q_2$ on the other hand, can be viewed as a state
|
|
492 |
after $q_1$ has consumed 1 character, and just waits
|
|
493 |
for 1 more character to complete.
|
|
494 |
$q_3$ is the last state, requiring 0 more character and is accepting.
|
|
495 |
Depending on the suffix of the
|
|
496 |
input string up to the current read location,
|
|
497 |
the states $q_1$ and $q_2$, $q_3$
|
|
498 |
may or may
|
|
499 |
not be active, independent from each other.
|
|
500 |
A $\mathit{DFA}$ for such an $\mathit{NFA}$ would
|
|
501 |
contain at least $2^3$ non-equivalent states that cannot be merged,
|
|
502 |
because the subset construction during determinisation will generate
|
|
503 |
all the elements in the power set $\mathit{Pow}\{q_1, q_2, q_3\}$.
|
|
504 |
Generalizing this to regular expressions with larger
|
|
505 |
bounded repetitions number, we have that
|
|
506 |
regexes shaped like $r^*ar^{\{n\}}$ when converted to $\mathit{DFA}$s
|
|
507 |
would require at least $2^{n+1}$ states, if $r$ contains
|
|
508 |
more than 1 string.
|
|
509 |
This is to represent all different
|
|
510 |
scenarios which "countdown" states are active.
|
538
|
511 |
For those regexes, tools that uses $\DFA$s will get
|
532
|
512 |
out of memory errors.
|
538
|
513 |
|
|
514 |
\subsubsection{Tools that uses $\mathit{DFA}$s}
|
|
515 |
%TODO:more tools that use DFAs?
|
|
516 |
$\mathit{LEX}$ and $\mathit{JFLEX}$ are tools
|
|
517 |
in $C$ and $\mathit{JAVA}$ that generates $\mathit{DFA}$-based
|
|
518 |
lexers. The user provides a set of regular expressions
|
|
519 |
and configurations to such lexer generators, and then
|
|
520 |
gets an output program encoding a minimized $\mathit{DFA}$
|
|
521 |
that can be compiled and run.
|
|
522 |
When given the above countdown regular expression,
|
|
523 |
a small number $n$ would result in a determinised automata
|
|
524 |
with millions of states.
|
|
525 |
|
532
|
526 |
For this reason, regex libraries that support
|
|
527 |
bounded repetitions often choose to use the $\mathit{NFA}$
|
|
528 |
approach.
|
538
|
529 |
|
|
530 |
|
|
531 |
|
|
532 |
|
|
533 |
|
|
534 |
|
|
535 |
|
|
536 |
|
|
537 |
\subsection{Why $\mathit{NFA}$s can be slow in the second phase}
|
|
538 |
When one constructs an $\NFA$ out of a regular expression
|
|
539 |
there is often very little to be done in the first phase, one simply
|
|
540 |
construct the $\NFA$ states based on the structure of the input regular expression.
|
|
541 |
|
|
542 |
In the lexing phase, one can simulate the $\mathit{NFA}$ running in two ways:
|
532
|
543 |
one by keeping track of all active states after consuming
|
|
544 |
a character, and update that set of states iteratively.
|
|
545 |
This can be viewed as a breadth-first-search of the $\mathit{NFA}$
|
|
546 |
for a path terminating
|
|
547 |
at an accepting state.
|
|
548 |
Languages like $\mathit{Go}$ and $\mathit{Rust}$ use this
|
538
|
549 |
type of $\mathit{NFA}$ simulation and guarantees a linear runtime
|
532
|
550 |
in terms of input string length.
|
|
551 |
%TODO:try out these lexers
|
|
552 |
The other way to use $\mathit{NFA}$ for matching is choosing
|
|
553 |
a single transition each time, keeping all the other options in
|
|
554 |
a queue or stack, and backtracking if that choice eventually
|
|
555 |
fails. This method, often called a "depth-first-search",
|
|
556 |
is efficient in a lot of cases, but could end up
|
|
557 |
with exponential run time.\\
|
|
558 |
%TODO:COMPARE java python lexer speed with Rust and Go
|
|
559 |
The reason behind backtracking algorithms in languages like
|
|
560 |
Java and Python is that they support back-references.
|
538
|
561 |
\subsubsection{Back References}
|
532
|
562 |
If we have a regular expression like this (the sequence
|
|
563 |
operator is omitted for brevity):
|
|
564 |
\begin{center}
|
|
565 |
$r_1(r_2(r_3r_4))$
|
|
566 |
\end{center}
|
|
567 |
We could label sub-expressions of interest
|
|
568 |
by parenthesizing them and giving
|
|
569 |
them a number by the order in which their opening parentheses appear.
|
|
570 |
One possible way of parenthesizing and labelling is given below:
|
|
571 |
\begin{center}
|
|
572 |
$\underset{1}{(}r_1\underset{2}{(}r_2\underset{3}{(}r_3)\underset{4}{(}r_4)))$
|
|
573 |
\end{center}
|
|
574 |
$r_1r_2r_3r_4$, $r_1r_2r_3$, $r_3$, $r_4$ are labelled
|
|
575 |
by 1 to 4. $1$ would refer to the entire expression
|
|
576 |
$(r_1(r_2(r_3)(r_4)))$, $2$ referring to $r_2(r_3)(r_4)$, etc.
|
|
577 |
These sub-expressions are called "capturing groups".
|
|
578 |
We can use the following syntax to denote that we want a string just matched by a
|
|
579 |
sub-expression (capturing group) to appear at a certain location again,
|
|
580 |
exactly as it was:
|
|
581 |
\begin{center}
|
|
582 |
$\ldots\underset{\text{i-th lparen}}{(}{r_i})\ldots
|
|
583 |
\underset{s_i \text{ which just matched} \;r_i}{\backslash i}$
|
|
584 |
\end{center}
|
|
585 |
The backslash and number $i$ are used to denote such
|
|
586 |
so-called "back-references".
|
|
587 |
Let $e$ be an expression made of regular expressions
|
|
588 |
and back-references. $e$ contains the expression $e_i$
|
|
589 |
as its $i$-th capturing group.
|
|
590 |
The semantics of back-reference can be recursively
|
|
591 |
written as:
|
|
592 |
\begin{center}
|
|
593 |
\begin{tabular}{c}
|
|
594 |
$L ( e \cdot \backslash i) = \{s @ s_i \mid s \in L (e)\quad s_i \in L(r_i)$\\
|
|
595 |
$s_i\; \text{match of ($e$, $s$)'s $i$-th capturing group string}\}$
|
|
596 |
\end{tabular}
|
|
597 |
\end{center}
|
|
598 |
The concrete example
|
|
599 |
$((a|b|c|\ldots|z)^*)\backslash 1$
|
|
600 |
would match the string like $\mathit{bobo}$, $\mathit{weewee}$ and etc.\\
|
|
601 |
Back-reference is a construct in the "regex" standard
|
|
602 |
that programmers found useful, but not exactly
|
|
603 |
regular any more.
|
|
604 |
In fact, that allows the regex construct to express
|
|
605 |
languages that cannot be contained in context-free
|
|
606 |
languages either.
|
|
607 |
For example, the back-reference $((a^*)b\backslash1 b \backslash 1$
|
|
608 |
expresses the language $\{a^n b a^n b a^n\mid n \in \mathbb{N}\}$,
|
|
609 |
which cannot be expressed by context-free grammars\parencite{campeanu2003formal}.
|
|
610 |
Such a language is contained in the context-sensitive hierarchy
|
|
611 |
of formal languages.
|
|
612 |
Solving the back-reference expressions matching problem
|
|
613 |
is NP-complete\parencite{alfred2014algorithms} and a non-bactracking,
|
|
614 |
efficient solution is not known to exist.
|
|
615 |
%TODO:read a bit more about back reference algorithms
|
538
|
616 |
|
532
|
617 |
It seems that languages like Java and Python made the trade-off
|
|
618 |
to support back-references at the expense of having to backtrack,
|
|
619 |
even in the case of regexes not involving back-references.\\
|
|
620 |
Summing these up, we can categorise existing
|
|
621 |
practical regex libraries into the ones with linear
|
|
622 |
time guarantees like Go and Rust, which impose restrictions
|
|
623 |
on the user input (not allowing back-references,
|
538
|
624 |
bounded repetitions cannot exceed 1000 etc.), and ones
|
532
|
625 |
that allows the programmer much freedom, but grinds to a halt
|
|
626 |
in some non-negligible portion of cases.
|
|
627 |
%TODO: give examples such as RE2 GOLANG 1000 restriction, rust no repetitions
|
|
628 |
% For example, the Rust regex engine claims to be linear,
|
|
629 |
% but does not support lookarounds and back-references.
|
|
630 |
% The GoLang regex library does not support over 1000 repetitions.
|
|
631 |
% Java and Python both support back-references, but shows
|
|
632 |
%catastrophic backtracking behaviours on inputs without back-references(
|
|
633 |
%when the language is still regular).
|
|
634 |
%TODO: test performance of Rust on (((((a*a*)b*)b){20})*)c baabaabababaabaaaaaaaaababaaaababababaaaabaaabaaaaaabaabaabababaababaaaaaaaaababaaaababababaaaaaaaaaaaaac
|
|
635 |
%TODO: verify the fact Rust does not allow 1000+ reps
|
538
|
636 |
\ChristianComment{Comment required: Java 17 updated graphs? Is it ok to still use Java 8 graphs?}
|
532
|
637 |
|
|
638 |
|
538
|
639 |
So we have practical implementations
|
|
640 |
on regular expression matching/lexing which are fast
|
|
641 |
but do not come with any guarantees that it will not grind to a halt
|
|
642 |
or give wrong answers.
|
|
643 |
Our goal is to have a regex lexing algorithm that comes with
|
|
644 |
\begin{itemize}
|
|
645 |
\item
|
|
646 |
proven correctness
|
|
647 |
\item
|
|
648 |
proven non-catastrophic properties
|
|
649 |
\item
|
|
650 |
easy extensions to
|
|
651 |
constructs like
|
|
652 |
bounded repetitions, negation, lookarounds, and even back-references.
|
|
653 |
\end{itemize}
|
532
|
654 |
|
538
|
655 |
\section{Our Solution--Formal Specification of POSIX and Brzozowski Derivatives}
|
|
656 |
We propose Brzozowski derivatives on regular expressions as
|
|
657 |
a solution to this.
|
|
658 |
In the last fifteen or so years, Brzozowski's derivatives of regular
|
|
659 |
expressions have sparked quite a bit of interest in the functional
|
|
660 |
programming and theorem prover communities.
|
532
|
661 |
|
538
|
662 |
\subsection{Motivation}
|
|
663 |
|
|
664 |
Derivatives give a simple solution
|
|
665 |
to the problem of matching a string $s$ with a regular
|
|
666 |
expression $r$: if the derivative of $r$ w.r.t.\ (in
|
|
667 |
succession) all the characters of the string matches the empty string,
|
|
668 |
then $r$ matches $s$ (and {\em vice versa}).
|
532
|
669 |
|
538
|
670 |
The beauty of
|
532
|
671 |
Brzozowski's derivatives \parencite{Brzozowski1964} is that they are neatly
|
|
672 |
expressible in any functional language, and easily definable and
|
|
673 |
reasoned about in theorem provers---the definitions just consist of
|
|
674 |
inductive datatypes and simple recursive functions.
|
|
675 |
And an algorithms based on it by
|
|
676 |
Suzmann and Lu \parencite{Sulzmann2014} allows easy extension
|
|
677 |
to include extended regular expressions and
|
|
678 |
simplification of internal data structures
|
|
679 |
eliminating the exponential behaviours.
|
|
680 |
|
|
681 |
However, two difficulties with derivative-based matchers exist:
|
538
|
682 |
\subsubsection{Problems with Current Brzozowski Matchers}
|
532
|
683 |
First, Brzozowski's original matcher only generates a yes/no answer
|
|
684 |
for whether a regular expression matches a string or not. This is too
|
|
685 |
little information in the context of lexing where separate tokens must
|
|
686 |
be identified and also classified (for example as keywords
|
|
687 |
or identifiers). Sulzmann and Lu~\cite{Sulzmann2014} overcome this
|
|
688 |
difficulty by cleverly extending Brzozowski's matching
|
|
689 |
algorithm. Their extended version generates additional information on
|
|
690 |
\emph{how} a regular expression matches a string following the POSIX
|
|
691 |
rules for regular expression matching. They achieve this by adding a
|
|
692 |
second ``phase'' to Brzozowski's algorithm involving an injection
|
538
|
693 |
function. In our own earlier work, we provided the formal
|
532
|
694 |
specification of what POSIX matching means and proved in Isabelle/HOL
|
|
695 |
the correctness
|
|
696 |
of Sulzmann and Lu's extended algorithm accordingly
|
|
697 |
\cite{AusafDyckhoffUrban2016}.
|
|
698 |
|
|
699 |
The second difficulty is that Brzozowski's derivatives can
|
|
700 |
grow to arbitrarily big sizes. For example if we start with the
|
|
701 |
regular expression $(a+aa)^*$ and take
|
|
702 |
successive derivatives according to the character $a$, we end up with
|
|
703 |
a sequence of ever-growing derivatives like
|
|
704 |
|
|
705 |
\def\ll{\stackrel{\_\backslash{} a}{\longrightarrow}}
|
|
706 |
\begin{center}
|
|
707 |
\begin{tabular}{rll}
|
|
708 |
$(a + aa)^*$ & $\ll$ & $(\ONE + \ONE{}a) \cdot (a + aa)^*$\\
|
|
709 |
& $\ll$ & $(\ZERO + \ZERO{}a + \ONE) \cdot (a + aa)^* \;+\; (\ONE + \ONE{}a) \cdot (a + aa)^*$\\
|
|
710 |
& $\ll$ & $(\ZERO + \ZERO{}a + \ZERO) \cdot (a + aa)^* + (\ONE + \ONE{}a) \cdot (a + aa)^* \;+\; $\\
|
|
711 |
& & $\qquad(\ZERO + \ZERO{}a + \ONE) \cdot (a + aa)^* + (\ONE + \ONE{}a) \cdot (a + aa)^*$\\
|
|
712 |
& $\ll$ & \ldots \hspace{15mm}(regular expressions of sizes 98, 169, 283, 468, 767, \ldots)
|
|
713 |
\end{tabular}
|
|
714 |
\end{center}
|
|
715 |
|
|
716 |
\noindent where after around 35 steps we run out of memory on a
|
|
717 |
typical computer (we shall define shortly the precise details of our
|
|
718 |
regular expressions and the derivative operation). Clearly, the
|
|
719 |
notation involving $\ZERO$s and $\ONE$s already suggests
|
|
720 |
simplification rules that can be applied to regular regular
|
|
721 |
expressions, for example $\ZERO{}\,r \Rightarrow \ZERO$, $\ONE{}\,r
|
|
722 |
\Rightarrow r$, $\ZERO{} + r \Rightarrow r$ and $r + r \Rightarrow
|
|
723 |
r$. While such simple-minded simplifications have been proved in our
|
|
724 |
earlier work to preserve the correctness of Sulzmann and Lu's
|
|
725 |
algorithm \cite{AusafDyckhoffUrban2016}, they unfortunately do
|
|
726 |
\emph{not} help with limiting the growth of the derivatives shown
|
|
727 |
above: the growth is slowed, but the derivatives can still grow rather
|
|
728 |
quickly beyond any finite bound.
|
|
729 |
|
|
730 |
|
|
731 |
Sulzmann and Lu overcome this ``growth problem'' in a second algorithm
|
538
|
732 |
\cite{Sulzmann2014} where they introduce bit-coded
|
532
|
733 |
regular expressions. In this version, POSIX values are
|
538
|
734 |
represented as bit sequences and such sequences are incrementally generated
|
532
|
735 |
when derivatives are calculated. The compact representation
|
538
|
736 |
of bit sequences and regular expressions allows them to define a more
|
532
|
737 |
``aggressive'' simplification method that keeps the size of the
|
|
738 |
derivatives finite no matter what the length of the string is.
|
|
739 |
They make some informal claims about the correctness and linear behaviour
|
|
740 |
of this version, but do not provide any supporting proof arguments, not
|
538
|
741 |
even ``pencil-and-paper'' arguments. They write about their bit-coded
|
532
|
742 |
\emph{incremental parsing method} (that is the algorithm to be formalised
|
538
|
743 |
in this dissertation)
|
532
|
744 |
|
|
745 |
|
|
746 |
|
|
747 |
\begin{quote}\it
|
|
748 |
``Correctness Claim: We further claim that the incremental parsing
|
|
749 |
method [..] in combination with the simplification steps [..]
|
|
750 |
yields POSIX parse trees. We have tested this claim
|
|
751 |
extensively [..] but yet
|
|
752 |
have to work out all proof details.'' \cite[Page 14]{Sulzmann2014}
|
|
753 |
\end{quote}
|
|
754 |
|
|
755 |
Ausaf and Urban were able to back this correctness claim with
|
|
756 |
a formal proof.
|
|
757 |
|
|
758 |
But as they stated,
|
|
759 |
\begin{quote}\it
|
|
760 |
The next step would be to implement a more aggressive simplification procedure on annotated regular expressions and then prove the corresponding algorithm generates the same values as blexer. Alas due to time constraints we are unable to do so here.
|
|
761 |
\end{quote}
|
|
762 |
|
|
763 |
This thesis implements the aggressive simplifications envisioned
|
|
764 |
by Ausaf and Urban,
|
|
765 |
and gives a formal proof of the correctness with those simplifications.
|
|
766 |
|
|
767 |
|
|
768 |
%----------------------------------------------------------------------------------------
|
|
769 |
\section{Contribution}
|
|
770 |
|
|
771 |
|
|
772 |
|
|
773 |
This work addresses the vulnerability of super-linear and
|
|
774 |
buggy regex implementations by the combination
|
|
775 |
of Brzozowski's derivatives and interactive theorem proving.
|
|
776 |
We give an
|
|
777 |
improved version of Sulzmann and Lu's bit-coded algorithm using
|
|
778 |
derivatives, which come with a formal guarantee in terms of correctness and
|
|
779 |
running time as an Isabelle/HOL proof.
|
538
|
780 |
Further improvements to the algorithm with an even stronger version of
|
|
781 |
simplification is made.
|
|
782 |
We have not yet come up with one, but believe that it leads to a
|
|
783 |
formalised proof with a time bound linear to input and
|
532
|
784 |
cubic to regular expression size using a technique by
|
538
|
785 |
Antimirov\cite{Antimirov}.
|
532
|
786 |
|
|
787 |
|
538
|
788 |
The main contribution of this thesis is
|
|
789 |
\begin{itemize}
|
|
790 |
\item
|
|
791 |
a proven correct lexing algorithm
|
|
792 |
\item
|
|
793 |
with formalized finite bounds on internal data structures' sizes.
|
|
794 |
\end{itemize}
|
|
795 |
|
532
|
796 |
To our best knowledge, no lexing libraries using Brzozowski derivatives
|
|
797 |
have a provable time guarantee,
|
|
798 |
and claims about running time are usually speculative and backed by thin empirical
|
|
799 |
evidence.
|
|
800 |
%TODO: give references
|
|
801 |
For example, Sulzmann and Lu had proposed an algorithm in which they
|
|
802 |
claim a linear running time.
|
|
803 |
But that was falsified by our experiments and the running time
|
|
804 |
is actually $\Omega(2^n)$ in the worst case.
|
|
805 |
A similar claim about a theoretical runtime of $O(n^2)$ is made for the Verbatim
|
|
806 |
%TODO: give references
|
|
807 |
lexer, which calculates POSIX matches and is based on derivatives.
|
|
808 |
They formalized the correctness of the lexer, but not the complexity.
|
|
809 |
In the performance evaluation section, they simply analyzed the run time
|
|
810 |
of matching $a$ with the string $\underbrace{a \ldots a}_{\text{n a's}}$
|
|
811 |
and concluded that the algorithm is quadratic in terms of input length.
|
|
812 |
When we tried out their extracted OCaml code with our example $(a+aa)^*$,
|
|
813 |
the time it took to lex only 40 $a$'s was 5 minutes.
|
|
814 |
|
|
815 |
|
|
816 |
|
|
817 |
\subsection{Related Work}
|
|
818 |
We are aware
|
|
819 |
of a mechanised correctness proof of Brzozowski's derivative-based matcher in HOL4 by
|
|
820 |
Owens and Slind~\parencite{Owens2008}. Another one in Isabelle/HOL is part
|
|
821 |
of the work by Krauss and Nipkow \parencite{Krauss2011}. And another one
|
|
822 |
in Coq is given by Coquand and Siles \parencite{Coquand2012}.
|
|
823 |
Also Ribeiro and Du Bois give one in Agda \parencite{RibeiroAgda2017}.
|
|
824 |
|
538
|
825 |
|
|
826 |
When a regular expression does not behave as intended,
|
|
827 |
people usually try to rewrite the regex to some equivalent form
|
|
828 |
or they try to avoid the possibly problematic patterns completely,
|
|
829 |
for which many false positives exist\parencite{Davis18}.
|
|
830 |
Animated tools to "debug" regular expressions such as
|
|
831 |
\parencite{regexploit2021} \parencite{regex101} are also popular.
|
|
832 |
We are also aware of static analysis work on regular expressions that
|
|
833 |
aims to detect potentially expoential regex patterns. Rathnayake and Thielecke
|
|
834 |
\parencite{Rathnayake2014StaticAF} proposed an algorithm
|
|
835 |
that detects regular expressions triggering exponential
|
|
836 |
behavious on backtracking matchers.
|
|
837 |
Weideman \parencite{Weideman2017Static} came up with
|
|
838 |
non-linear polynomial worst-time estimates
|
|
839 |
for regexes, attack string that exploit the worst-time
|
|
840 |
scenario, and "attack automata" that generates
|
|
841 |
attack strings.
|
|
842 |
|
|
843 |
|
532
|
844 |
|
|
845 |
|
|
846 |
\section{Structure of the thesis}
|
538
|
847 |
In chapter 2 \ref{Inj} we will introduce the concepts
|
532
|
848 |
and notations we
|
|
849 |
use for describing the lexing algorithm by Sulzmann and Lu,
|
538
|
850 |
and then give the lexing algorithm.
|
|
851 |
We will give its variant in \ref{Bitcoded1}.
|
|
852 |
Then we illustrate in \ref{Bitcoded2}
|
532
|
853 |
how the algorithm without bitcodes falls short for such aggressive
|
|
854 |
simplifications and therefore introduce our version of the
|
538
|
855 |
bit-coded algorithm and
|
532
|
856 |
its correctness proof .
|
538
|
857 |
In \ref{Finite} we give the second guarantee
|
532
|
858 |
of our bitcoded algorithm, that is a finite bound on the size of any
|
|
859 |
regex's derivatives.
|
538
|
860 |
In \ref{Cubic} we discuss stronger simplifications to improve the finite bound
|
|
861 |
in \ref{Finite} to a polynomial one, and demonstrate how one can extend the
|
532
|
862 |
algorithm to include constructs such as bounded repetitions and negations.
|
|
863 |
|
|
864 |
|
|
865 |
|
|
866 |
|
|
867 |
|
|
868 |
%----------------------------------------------------------------------------------------
|
|
869 |
|
|
870 |
|
|
871 |
%----------------------------------------------------------------------------------------
|
|
872 |
|
|
873 |
%----------------------------------------------------------------------------------------
|
|
874 |
|
|
875 |
%----------------------------------------------------------------------------------------
|
|
876 |
|
|
877 |
|