author | Chengsong |
Fri, 14 Jul 2023 00:32:41 +0100 | |
changeset 665 | 3bedbdce3a3b |
parent 664 | ba44144875b1 |
child 666 | 6da4516ea87d |
permissions | -rwxr-xr-x |
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} |
596 | 23 |
\newcommand{\rderssimp}[2]{#1 \backslash_{rsimps} #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)} |
|
620 | 42 |
\def\cbn{\textit{createdByNtimes}} |
43 |
\def\hpa{\textit{highestPowerAux}} |
|
44 |
\def\hpower{\textit{highestPower}} |
|
45 |
\def\ntset{\textit{ntset}} |
|
46 |
\def\optermsimp{\textit{optermsimp}} |
|
47 |
\def\optermOsimp{\textit{optermOsimp}} |
|
48 |
\def\optermosimp{\textit{optermosimp}} |
|
49 |
\def\opterm{\textit{opterm}} |
|
50 |
\def\nString{\textit{nonemptyString}} |
|
532 | 51 |
|
52 |
\newcommand\myequiv{\mathrel{\stackrel{\makebox[0pt]{\mbox{\normalfont\tiny equiv}}}{=}}} |
|
53 |
||
600 | 54 |
\def\SEQ{\textit{SEQ}} |
55 |
\def\SEQs{\textit{SEQs}} |
|
564 | 56 |
\def\case{\textit{case}} |
554 | 57 |
\def\sequal{\stackrel{\mbox{\scriptsize rsimp}}{=}} |
58 |
\def\rsimpalts{\textit{rsimp}_{ALTS}} |
|
59 |
\def\good{\textit{good}} |
|
60 |
\def\btrue{\textit{true}} |
|
61 |
\def\bfalse{\textit{false}} |
|
542 | 62 |
\def\bnullable{\textit{bnullable}} |
543 | 63 |
\def\bnullables{\textit{bnullables}} |
538 | 64 |
\def\Some{\textit{Some}} |
65 |
\def\None{\textit{None}} |
|
537 | 66 |
\def\code{\textit{code}} |
532 | 67 |
\def\decode{\textit{decode}} |
68 |
\def\internalise{\textit{internalise}} |
|
69 |
\def\lexer{\mathit{lexer}} |
|
70 |
\def\mkeps{\textit{mkeps}} |
|
557 | 71 |
\newcommand{\rder}[2]{#2 \backslash_r #1} |
532 | 72 |
|
585 | 73 |
\def\rerases{\textit{rerase}} |
74 |
||
554 | 75 |
\def\nonnested{\textit{nonnested}} |
532 | 76 |
\def\AZERO{\textit{AZERO}} |
558 | 77 |
\def\sizeNregex{\textit{sizeNregex}} |
532 | 78 |
\def\AONE{\textit{AONE}} |
79 |
\def\ACHAR{\textit{ACHAR}} |
|
80 |
||
585 | 81 |
\def\simpsulz{\textit{simp}_{Sulz}} |
82 |
||
557 | 83 |
\def\scfrewrites{\stackrel{*}{\rightsquigarrow_{scf}}} |
555 | 84 |
\def\frewrite{\rightsquigarrow_f} |
85 |
\def\hrewrite{\rightsquigarrow_h} |
|
86 |
\def\grewrite{\rightsquigarrow_g} |
|
87 |
\def\frewrites{\stackrel{*}{\rightsquigarrow_f}} |
|
88 |
\def\hrewrites{\stackrel{*}{\rightsquigarrow_h}} |
|
89 |
\def\grewrites{\stackrel{*}{\rightsquigarrow_g}} |
|
538 | 90 |
\def\fuse{\textit{fuse}} |
91 |
\def\bder{\textit{bder}} |
|
542 | 92 |
\def\der{\textit{der}} |
532 | 93 |
\def\POSIX{\textit{POSIX}} |
94 |
\def\ALTS{\textit{ALTS}} |
|
95 |
\def\ASTAR{\textit{ASTAR}} |
|
96 |
\def\DFA{\textit{DFA}} |
|
538 | 97 |
\def\NFA{\textit{NFA}} |
532 | 98 |
\def\bmkeps{\textit{bmkeps}} |
543 | 99 |
\def\bmkepss{\textit{bmkepss}} |
532 | 100 |
\def\retrieve{\textit{retrieve}} |
101 |
\def\blexer{\textit{blexer}} |
|
102 |
\def\flex{\textit{flex}} |
|
573 | 103 |
\def\inj{\textit{inj}} |
564 | 104 |
\def\Empty{\textit{Empty}} |
567 | 105 |
\def\Left{\textit{Left}} |
106 |
\def\Right{\textit{Right}} |
|
573 | 107 |
\def\Stars{\textit{Stars}} |
108 |
\def\Char{\textit{Char}} |
|
109 |
\def\Seq{\textit{Seq}} |
|
653
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
110 |
\def\Alt{\textit{Alt}} |
532 | 111 |
\def\Der{\textit{Der}} |
112 |
\def\Ders{\textit{Ders}} |
|
113 |
\def\nullable{\mathit{nullable}} |
|
114 |
\def\Z{\mathit{Z}} |
|
115 |
\def\S{\mathit{S}} |
|
116 |
\def\rup{r^\uparrow} |
|
117 |
%\def\bderssimp{\mathit{bders}\_\mathit{simp}} |
|
118 |
\def\distinctWith{\textit{distinctWith}} |
|
119 |
\def\lf{\textit{lf}} |
|
120 |
\def\PD{\textit{PD}} |
|
121 |
\def\suffix{\textit{Suffix}} |
|
543 | 122 |
\def\distinctBy{\textit{distinctBy}} |
558 | 123 |
\def\starupdate{\textit{starUpdate}} |
124 |
\def\starupdates{\textit{starUpdates}} |
|
620 | 125 |
\def\nupdate{\textit{nupdate}} |
126 |
\def\nupdates{\textit{nupdates}} |
|
558 | 127 |
|
532 | 128 |
|
129 |
\def\size{\mathit{size}} |
|
130 |
\def\rexp{\mathbf{rexp}} |
|
131 |
\def\simp{\mathit{simp}} |
|
132 |
\def\simpALTs{\mathit{simp}\_\mathit{ALTs}} |
|
133 |
\def\map{\mathit{map}} |
|
134 |
\def\distinct{\mathit{distinct}} |
|
135 |
\def\blexersimp{\mathit{blexer}\_\mathit{simp}} |
|
590 | 136 |
\def\blexerStrong{\textit{blexerStrong}} |
137 |
\def\bsimpStrong{\textit{bsimpStrong}} |
|
591 | 138 |
\def\bdersStrongs{\textit{bdersStrong}} |
590 | 139 |
\newcommand{\bdersStrong}[2]{#1 \backslash_{bsimpStrongs} #2} |
140 |
||
532 | 141 |
\def\map{\textit{map}} |
142 |
\def\rrexp{\textit{rrexp}} |
|
554 | 143 |
\newcommand\rnullable[1]{\textit{rnullable} \; #1 } |
532 | 144 |
\newcommand\rsize[1]{\llbracket #1 \rrbracket_r} |
145 |
\newcommand\asize[1]{\llbracket #1 \rrbracket} |
|
543 | 146 |
\newcommand\rerase[1]{ (#1)_{\downarrow_r}} |
147 |
||
538 | 148 |
\newcommand\ChristianComment[1]{\textcolor{blue}{#1}\\} |
532 | 149 |
|
543 | 150 |
|
151 |
\def\rflts{\textit{rflts}} |
|
152 |
\def\rrewrite{\textit{rrewrite}} |
|
153 |
\def\bsimpalts{\textit{bsimp}_{ALTS}} |
|
596 | 154 |
\def\bsimpaseq{\textit{bsimp}_{ASEQ}} |
155 |
\def\rsimlalts{\textit{rsimp}_{ALTs}} |
|
156 |
\def\rsimpseq{\textit{rsimp}_{SEQ}} |
|
543 | 157 |
|
532 | 158 |
\def\erase{\textit{erase}} |
159 |
\def\STAR{\textit{STAR}} |
|
160 |
\def\flts{\textit{flts}} |
|
161 |
||
162 |
||
579 | 163 |
\def\zeroable{\textit{zeroable}} |
164 |
\def\nub{\textit{nub}} |
|
165 |
\def\filter{\textit{filter}} |
|
601 | 166 |
%\def\not{\textit{not}} |
579 | 167 |
|
168 |
||
169 |
||
532 | 170 |
\def\RZERO{\mathbf{0}_r } |
171 |
\def\RONE{\mathbf{1}_r} |
|
172 |
\newcommand\RCHAR[1]{\mathbf{#1}_r} |
|
173 |
\newcommand\RSEQ[2]{#1 \cdot #2} |
|
558 | 174 |
\newcommand\RALTS[1]{\sum #1} |
532 | 175 |
\newcommand\RSTAR[1]{#1^*} |
558 | 176 |
\newcommand\vsuf[2]{\textit{Suffix} \;#1\;#2} |
532 | 177 |
|
538 | 178 |
|
179 |
||
590 | 180 |
|
181 |
\lstdefinestyle{myScalastyle}{ |
|
182 |
frame=tb, |
|
183 |
language=scala, |
|
184 |
aboveskip=3mm, |
|
185 |
belowskip=3mm, |
|
186 |
showstringspaces=false, |
|
187 |
columns=flexible, |
|
188 |
basicstyle={\small\ttfamily}, |
|
189 |
numbers=none, |
|
190 |
numberstyle=\tiny\color{gray}, |
|
191 |
keywordstyle=\color{blue}, |
|
192 |
commentstyle=\color{dkgreen}, |
|
193 |
stringstyle=\color{mauve}, |
|
194 |
frame=single, |
|
195 |
breaklines=true, |
|
196 |
breakatwhitespace=true, |
|
197 |
tabsize=3, |
|
538 | 198 |
} |
199 |
||
590 | 200 |
|
532 | 201 |
%---------------------------------------------------------------------------------------- |
202 |
%This part is about regular expressions, Brzozowski derivatives, |
|
203 |
%and a bit-coded lexing algorithm with proven correctness and time bounds. |
|
204 |
||
205 |
%TODO: look up snort rules to use here--give readers idea of what regexes look like |
|
206 |
||
654 | 207 |
\marginpar{rephrasing using "imprecise words"} |
648 | 208 |
Regular expressions, since their inception in the 1940s, |
209 |
have been subject to extensive study and implementation. |
|
653
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
210 |
Their primary application lies in text processing--finding |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
211 |
matches and identifying patterns in a string. |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
212 |
%It is often used to match strings that comprises of numerous fields, |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
213 |
%where certain fields may recur or be omitted. |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
214 |
For example, a simple regular expression that tries |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
215 |
to recognise email addresses is |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
216 |
\marginpar{rephrased from "the regex for recognising" to "a simple regex that tries to match email"} |
648 | 217 |
\begin{center} |
654 | 218 |
\verb|[a-z0-9._]^+@[a-z0-9.-]^+\.\{a-z\}\{2,6\}| |
653
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
219 |
%$[a-z0-9._]^+@[a-z0-9.-]^+\.[a-z]{2,6}$. |
648 | 220 |
\end{center} |
653
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
221 |
\marginpar{Simplified example, but the distinction between . and escaped . is correct |
654 | 222 |
and therefore left unchanged. Also verbatim package does not straightforwardly support superscripts so + kept as they are.} |
653
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
223 |
%Using this, regular expression matchers and lexers are able to extract |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
224 |
%the domain names by the use of \verb|[a-zA-Z0-9.-]+|. |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
225 |
\marginpar{Rewrote explanation for the expression.} |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
226 |
The bracketed sub-expressions are used to extract specific parts of an email address. |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
227 |
The local part is recognised by the expression enclosed in |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
228 |
the first pair of brackets: $[a-z0-9._]$, and after the ``@'' sign |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
229 |
is the part that recognises the domain, where $[a-z]{2, 6}$ specifically |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
230 |
matches the top-level domain. |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
231 |
%Consequently, they are an indispensible components in text processing tools |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
232 |
%of software systems such as compilers, IDEs, and firewalls. |
601 | 233 |
|
648 | 234 |
The study of regular expressions is ongoing due to an |
235 |
issue known as catastrophic backtracking. |
|
236 |
This phenomenon refers to scenarios in which the regular expression |
|
237 |
matching or lexing engine exhibits a disproportionately long |
|
238 |
runtime despite the simplicity of the input and expression. |
|
239 |
||
653
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
240 |
One cause of catastrophic backtracking lies within the |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
241 |
ambiguities of lexing.\marginpar{rephrased "the origin of catastrophic ...} |
648 | 242 |
In the process of matching a multi-character string with |
243 |
a regular expression that encompasses several sub-expressions, |
|
244 |
different positions can be designated to mark |
|
245 |
the boundaries of corresponding substrings of the sub-expressions. |
|
246 |
For instance, in matching the string $aaa$ with the |
|
247 |
regular expression $(a+aa)^*$, the divide between |
|
248 |
the initial match and the subsequent iteration could either be |
|
249 |
set between the first and second characters ($a | aa$) or between the second and third characters ($aa | a$). As both the length of the input string and the structural complexity of the regular expression increase, the number of potential delimiter combinations can grow exponentially, leading to a corresponding increase in complexity for algorithms that do not optimally navigate these possibilities. |
|
250 |
||
251 |
Catastrophic backtracking facilitates a category of computationally inexpensive attacks known as Regular Expression Denial of Service (ReDoS) attacks. Here, an attacker merely dispatches a small attack string to a server, provoking high-complexity behaviours in the server's regular expression engine. Such attacks, whether intentional or accidental, have led to the failure of real-world systems (additional details can be found in the practical overview section in chapter \ref{Introduction}). |
|
252 |
||
653
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
253 |
Various disambiguation strategies are |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
254 |
employed to select sub-matches, notably including Greedy and POSIX strategies. POSIX, the strategy most widely adopted in practice, invariably opts for the longest possible sub-match. Kuklewicz \cite{KuklewiczHaskell}, for instance, offers a descriptive definition of the POSIX rule in section 1, last paragraph: |
601 | 255 |
|
256 |
||
648 | 257 |
%Regular expressions |
258 |
%have been extensively studied and |
|
259 |
%implemented since their invention in 1940s. |
|
260 |
%It is primarily used in lexing, where an unstructured input string is broken |
|
261 |
%down into a tree of tokens. |
|
262 |
%That tree's construction is guided by the shape of the regular expression. |
|
263 |
%This is particularly useful in expressing the shape of a string |
|
264 |
%with many fields, where certain fields might repeat or be omitted. |
|
265 |
%Regular expression matchers and Lexers allow us to |
|
266 |
%identify and delimit different subsections of a string and potentially |
|
267 |
%extract information from inputs, making them |
|
268 |
%an indispensible component in modern software systems' text processing tasks |
|
269 |
%such as compilers, IDEs, and firewalls. |
|
270 |
%Research on them is far from over due to the well-known issue called catastrophic-backtracking, |
|
271 |
%which means the regular expression matching or lexing engine takes an unproportional time to run |
|
272 |
%despite the input and the expression being relatively simple. |
|
273 |
% |
|
274 |
%Catastrophic backtracking stems from the ambiguities of lexing: |
|
275 |
%when matching a multiple-character string with a regular |
|
276 |
%exression that includes serveral sub-expressions, there might be different positions to set |
|
277 |
%the border between sub-expressions' corresponding sub-strings. |
|
278 |
%For example, matching the string $aaa$ against the regular expression |
|
279 |
%$(a+aa)^*$, the border between the initial match and the second iteration |
|
280 |
%could be between the first and second character ($a | aa$) |
|
281 |
%or between the second and third character ($aa | a$). |
|
282 |
%As the size of the input string and the structural complexity |
|
283 |
%of the regular expression increase, |
|
284 |
%the number of different combinations of delimiters can grow exponentially, and |
|
285 |
%algorithms that explore these possibilities unwisely will also see an exponential complexity. |
|
286 |
% |
|
287 |
%Catastrophic backtracking allow a class of computationally inexpensive attacks called |
|
288 |
%Regular expression Denial of Service attacks (ReDoS), in which the hacker |
|
289 |
%simply sends out a small attack string to a server, |
|
290 |
%triggering high-complexity behaviours in its regular expression engine. |
|
291 |
%These attacks, be it deliberate or not, have caused real-world systems to go down (see more |
|
292 |
%details of this in the practical overview section in chapter \ref{Introduction}). |
|
293 |
%There are different disambiguation strategies to select sub-matches, most notably Greedy and POSIX. |
|
294 |
%The widely adopted strategy in practice is POSIX, which always go for the longest sub-match possible. |
|
295 |
%There have been prose definitions like the following |
|
296 |
%by Kuklewicz \cite{KuklewiczHaskell}: |
|
297 |
%described the POSIX rule as (section 1, last paragraph): |
|
653
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
298 |
\marginpar{\em Deleted some peripheral specifications.} |
646 | 299 |
\begin{quote} |
300 |
\begin{itemize} |
|
301 |
\item |
|
302 |
regular expressions (REs) take the leftmost starting match, and the longest match starting there |
|
303 |
earlier subpatterns have leftmost-longest priority over later subpatterns\\ |
|
304 |
\item |
|
305 |
higher-level subpatterns have leftmost-longest priority over their component subpatterns\\ |
|
306 |
\item |
|
653
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
307 |
$\ldots$ |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
308 |
%REs have right associative concatenation which can be changed with parenthesis\\ |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
309 |
%\item |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
310 |
%parenthesized subexpressions return the match from their last usage\\ |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
311 |
%\item |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
312 |
%text of component subexpressions must be contained in the text of the |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
313 |
%higher-level subexpressions\\ |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
314 |
%\item |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
315 |
%if "p" and "q" can never match the same text then "p|q" and "q|p" are equivalent, up to trivial renumbering of captured subexpressions\\ |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
316 |
%\item |
bc5571c38d1f
more updates in section 4.2 and incorporating Christian comments
Chengsong
parents:
652
diff
changeset
|
317 |
%if "p" in "p*" is used to capture non-empty text then additional repetitions of "p" will not capture an empty string\\ |
646 | 318 |
\end{itemize} |
319 |
\end{quote} |
|
649 | 320 |
However, the author noted that various lexers that claim to be POSIX |
321 |
are rarely correct according to this standard. |
|
322 |
There are numerous occasions where programmers realised the subtlety and |
|
323 |
difficulty to implement correctly, one such quote from Go's regexp library author |
|
324 |
\footnote{\url{https://pkg.go.dev/regexp\#pkg-overview}} |
|
325 |
\begin{quote}\it |
|
326 |
`` |
|
327 |
The POSIX rule is computationally prohibitive |
|
328 |
and not even well-defined. |
|
329 |
`` |
|
330 |
\end{quote} |
|
331 |
Being able to formally define and capture the idea of |
|
332 |
POSIX rules and prove |
|
333 |
the correctness of regular expression matching/lexing |
|
334 |
algorithms against the POSIX semantics definitions |
|
335 |
is valuable. |
|
336 |
||
646 | 337 |
|
649 | 338 |
Formal proofs are |
339 |
machine checked programs |
|
340 |
%such as the ones written in Isabelle/HOL, is a powerful means |
|
341 |
for computer scientists to be certain |
|
342 |
about the correctness of their algorithms. |
|
343 |
This is done by |
|
344 |
recursively checking that every fact in a proof script |
|
345 |
is either an axiom or a fact that is derived from |
|
346 |
known axioms or verified facts. |
|
347 |
%The problem of regular expressions lexing and catastrophic backtracking are a suitable problem for such |
|
348 |
%methods as their implementation and complexity analysis tend to be error-prone. |
|
349 |
Formal proofs provides an unprecendented level of asssurance |
|
350 |
that an algorithm will perform as expected under all inputs. |
|
351 |
The software systems that help people interactively build and check |
|
352 |
such proofs are called theorem-provers or proof assitants. |
|
353 |
Many theorem-provers have been developed, such as Mizar, |
|
354 |
Isabelle/HOL, HOL-Light, HOL4, |
|
355 |
Coq, Agda, Idris, Lean and so on. |
|
356 |
Isabelle/HOL is a theorem prover with a simple type theory |
|
357 |
and powerful automated proof generators like sledgehammer. |
|
358 |
We chose to use Isabelle/HOL for its powerful automation |
|
359 |
and ease and simplicity in expressing regular expressions and |
|
360 |
regular languages. |
|
361 |
%Some of those employ |
|
362 |
%dependent types like Mizar, Coq, Agda, Lean and Idris. |
|
363 |
%Some support a constructivism approach, such as Coq. |
|
646 | 364 |
|
365 |
||
649 | 366 |
Formal proofs on regular expression matching and lexing |
367 |
complements the efforts in |
|
368 |
industry which tend to focus on overall speed |
|
369 |
with techniques like parallelization (FPGA paper), tackling |
|
370 |
the problem of catastrophic backtracking |
|
371 |
in an ad-hoc manner (cloudflare and stackexchange article). |
|
372 |
||
373 |
There have been many interesting steps in the theorem-proving community |
|
374 |
about formalising regular expressions and lexing. |
|
375 |
One flavour is to build from the regular expression an automaton, and determine |
|
376 |
acceptance in terms of the resulting |
|
377 |
state after executing the input string on that automaton. |
|
378 |
Automata formalisations are in general harder and more cumbersome to deal |
|
379 |
with for theorem provers than working directly on regular expressions. |
|
380 |
One such example is by Nipkow \cite{Nipkow1998}. |
|
381 |
%They |
|
382 |
%made everything recursive (for example the next state function), |
|
383 |
As a core idea, they |
|
384 |
used a list of booleans to name each state so that |
|
385 |
after composing sub-automata together, renaming the states to maintain |
|
386 |
the distinctness of each state is recursive and simple. |
|
387 |
The result was the obvious lemmas incorporating |
|
388 |
``a painful amount of detail'' in their formalisation. |
|
389 |
Sometimes the automata are represented as graphs. |
|
390 |
But graphs are not inductive datatypes. |
|
391 |
Having to set the induction principle on the number of nodes |
|
392 |
in a graph makes formal reasoning non-intuitive and convoluted, |
|
393 |
resulting in large formalisations \cite{Lammich2012}. |
|
394 |
When combining two graphs, one also needs to make sure that the nodes in |
|
395 |
both graphs are distinct, which almost always involve |
|
396 |
renaming of the nodes. |
|
397 |
A theorem-prover which provides dependent types such as Coq |
|
398 |
can alleviate the issue of representing graph nodes |
|
399 |
\cite{Doczkal2013}. There the representation of nodes is made |
|
400 |
easier by the use of $\textit{FinType}$. |
|
401 |
Another representation for automata are matrices. |
|
402 |
But the induction for them is not as straightforward either. |
|
403 |
There are some more clever representations, for example one by Paulson |
|
404 |
using hereditarily finite sets \cite{Paulson2015}. |
|
405 |
There the problem with combining graphs can be solved better. |
|
406 |
%but we believe that such clever tricks are not very obvious for |
|
407 |
%the John-average-Isabelle-user. |
|
408 |
||
409 |
The approach that operates directly on regular expressions circumvents the problem of |
|
410 |
conversion between a regular expression and an automaton, thereby avoiding representation |
|
411 |
problems altogether, despite that regular expressions may be seen as a variant of a |
|
412 |
non-deterministic finite automaton (ref Laurikari tagged NFA paper). |
|
413 |
To matching a string, a sequence of algebraic transformations called |
|
414 |
(Brzozowski) $\textit{derivatives}$ (ref Brzozowski) is carried out on that regular expression. |
|
415 |
Each derivative takes a character and a regular expression, |
|
416 |
and returns a new regular expression whose language is closely related to |
|
417 |
the original regular expression's language: |
|
418 |
strings prefixed with that input character will have their head removed |
|
419 |
and strings not prefixed |
|
420 |
with that character will be eliminated. |
|
421 |
After taking derivatives with respect to all the characters the string is |
|
422 |
exhausted. Then an algorithm checks whether the empty string is in that final |
|
423 |
regular expression's language. |
|
424 |
If so, a match exists and the string is in the language of the input regular expression. |
|
425 |
||
426 |
Again this process can be seen as the simulation of an NFA running on a string, |
|
427 |
but the recursive nature of the datatypes and functions involved makes |
|
428 |
derivatives a perfect object of study for theorem provers. |
|
429 |
That is why there has been numerous formalisations of regular expressions |
|
430 |
and Brzozowski derivatives in the functional programming and |
|
431 |
theorem proving community (a large list of refs to derivatives formalisation publications). |
|
432 |
Ribeiro and Du Bois \cite{RibeiroAgda2017} have |
|
433 |
formalised the notion of bit-coded regular expressions |
|
434 |
and proved their relations with simple regular expressions in |
|
435 |
the dependently-typed proof assistant Agda. |
|
436 |
They also proved the soundness and completeness of a matching algorithm |
|
437 |
based on the bit-coded regular expressions. Their algorithm is a decision procedure |
|
438 |
that gives a Yes/No answer, which does not produce |
|
439 |
lexical values. |
|
440 |
%X also formalised derivatives and regular expressions, producing "parse trees". |
|
441 |
%(Some person who's a big name in formal methods) |
|
442 |
||
443 |
||
444 |
The variant of the problem we are looking at centers around |
|
445 |
an algorithm (which we call $\blexer$) developed by Sulzmann and Lu \ref{Sulzmann2014}. |
|
446 |
The reason we chose to look at $\blexer$ and its simplifications |
|
447 |
is because it allows a lexical tree to be generated |
|
448 |
by some elegant and subtle procedure based on Brzozowski derivatives. |
|
449 |
The procedures are made of recursive functions and inductive datatypes just like derivatives, |
|
450 |
allowing intuitive and concise formal reasoning with theorem provers. |
|
451 |
Most importantly, $\blexer$ opens up a path to an optimized version |
|
452 |
of $\blexersimp$ possibilities to improve |
|
453 |
performance with simplifications that aggressively change the structure of regular expressions. |
|
454 |
While most derivative-based methods |
|
455 |
rely on structures to be maintained to allow induction to |
|
456 |
go through. |
|
457 |
For example, Egolf et al. \ref{Verbatim} have developed a verified lexer |
|
458 |
with derivatives, but as soon as they started introducing |
|
459 |
optimizations such as memoization, they reverted to constructing |
|
460 |
DFAs first. |
|
461 |
Edelmann \ref{Edelmann2020} explored similar optimizations in his |
|
462 |
work on verified LL(1) parsing, with additional enhancements with data structures like |
|
463 |
zippers. |
|
464 |
||
465 |
%Sulzmann and Lu have worked out an algorithm |
|
466 |
%that is especially suited for verification |
|
467 |
%which utilized the fact |
|
468 |
%that whenever ambiguity occurs one may duplicate a sub-expression and use |
|
469 |
%different copies to describe different matching choices. |
|
470 |
The idea behind the correctness of $\blexer$ is simple: during a derivative, |
|
471 |
multiple matches might be possible, where an alternative with multiple children |
|
472 |
each corresponding to a |
|
473 |
different match is created. In the end of |
|
474 |
a lexing process one always picks up the leftmost alternative, which is guarnateed |
|
475 |
to be a POSIX value. |
|
476 |
This is done by consistently keeping sub-regular expressions in an alternative |
|
477 |
with longer submatches |
|
478 |
to the left of other copies ( |
|
479 |
Remember that POSIX values are roughly the values with the longest inital |
|
480 |
submatch). |
|
481 |
The idea behind the optimized version of $\blexer$, which we call $\blexersimp$, |
|
482 |
is that since we only take the leftmost copy, then all re-occurring copies can be |
|
483 |
eliminated without losing the POSIX property, and this can be done with |
|
484 |
children of alternatives at different levels by merging them together. |
|
485 |
Proving $\blexersimp$ requires a different |
|
486 |
proof strategy compared to that by Ausaf \cite{FahadThesis}. |
|
487 |
We invent a rewriting relation as an |
|
488 |
inductive predicate which captures |
|
489 |
a strong enough invariance that ensures correctness, |
|
490 |
which commutes with the derivative operation. |
|
491 |
This predicate allows a simple |
|
492 |
induction on the input string to go through. |
|
493 |
||
494 |
%This idea has been repeatedly used in different variants of lexing |
|
495 |
%algorithms in their paper, one of which involves bit-codes. The bit-coded |
|
496 |
%derivative-based algorithm even allows relatively aggressive |
|
497 |
%%simplification rules which cause |
|
498 |
%structural changes that render the induction used in the correctness |
|
499 |
%proofs unusable. |
|
500 |
%More details will be given in \ref{Bitcoded2} including the |
|
501 |
%new correctness proof which involves a new inductive predicate which allows |
|
502 |
%rule induction to go through. |
|
503 |
||
504 |
||
505 |
||
506 |
||
507 |
||
508 |
||
509 |
||
510 |
%first character is removed |
|
511 |
%state of the automaton after matching that character |
|
512 |
%where nodes are represented as |
|
513 |
%a sub-expression (for example tagged NFA |
|
514 |
%Working on regular expressions |
|
515 |
%Because of these problems with automata, we prefer regular expressions |
|
516 |
%and derivatives rather than an automata (or graph-based) approach which explicitly converts between |
|
517 |
%the regular expression and a different data structure. |
|
518 |
% |
|
519 |
% |
|
520 |
%The key idea |
|
646 | 521 |
|
648 | 522 |
(ends) |
646 | 523 |
|
524 |
%Regular expressions are widely used in computer science: |
|
525 |
%be it in text-editors \parencite{atomEditor} with syntax highlighting and auto-completion; |
|
526 |
%command-line tools like $\mathit{grep}$ that facilitate easy |
|
527 |
%text-processing \cite{grep}; network intrusion |
|
528 |
%detection systems that inspect suspicious traffic; or compiler |
|
529 |
%front ends. |
|
530 |
%Given their usefulness and ubiquity, one would assume that |
|
531 |
%modern regular expression matching implementations |
|
532 |
%are mature and fully studied. |
|
533 |
%Indeed, in many popular programming languages' regular expression engines, |
|
534 |
%one can supply a regular expression and a string, and in most cases get |
|
535 |
%get the matching information in a very short time. |
|
536 |
%Those engines can sometimes be blindingly fast--some |
|
537 |
%network intrusion detection systems |
|
538 |
%use regular expression engines that are able to process |
|
539 |
%hundreds of megabytes or even gigabytes of data per second \parencite{Turo_ov__2020}. |
|
540 |
%However, those engines can sometimes also exhibit a surprising security vulnerability |
|
541 |
%under a certain class of inputs. |
|
602 | 542 |
%However, , this is not the case for $\mathbf{all}$ inputs. |
601 | 543 |
%TODO: get source for SNORT/BRO's regex matching engine/speed |
544 |
||
603 | 545 |
|
532 | 546 |
%---------------------------------------------------------------------------------------- |
547 |
\section{Contribution} |
|
664 | 548 |
%{\color{red} \rule{\linewidth}{0.5mm}} |
549 |
%\textbf{issue with this part: way too short, not enough details of what I have done.} |
|
550 |
%{\color{red} \rule{\linewidth}{0.5mm}} |
|
551 |
\marginpar{\em Gerog: No sufficient context on related work making contribution hard to digest.} |
|
552 |
||
532 | 553 |
|
664 | 554 |
%In this thesis, |
555 |
%we propose a solution to catastrophic |
|
556 |
%backtracking and error-prone matchers: a formally verified |
|
557 |
%regular expression lexing algorithm |
|
558 |
%that is both fast |
|
559 |
%and correct. |
|
560 |
%%{\color{red} \rule{\linewidth}{0.5mm}} |
|
561 |
%\HandRight Added content: |
|
646 | 562 |
%Package \verb`pifont`: \ding{43} |
563 |
%Package \verb`utfsym`: \usym{261E} |
|
564 |
%Package \verb`dingbat`: \leftpointright |
|
565 |
%Package \verb`dingbat`: \rightpointright |
|
664 | 566 |
We have made mainly two contributions in this thesis: %the |
567 |
%lexer we developed based on Brzozowski derivatives and |
|
568 |
%Sulzmanna and Lu's developments called |
|
665
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
569 |
proving the lexer $\blexersimp$ is both i) correctness and ii)fast. |
664 | 570 |
It is correct w.r.t a formalisation of POSIX lexing by Ausaf et al.\ref{AusafDyckhoffUrban2016}. |
571 |
It is fast compared with un-optimised implementations like Sulzmann and Lu's orignal |
|
572 |
development by our metric of internal data structures not growing unbounded. |
|
573 |
||
574 |
Our formalisation of complexity is unique among similar works in the sense that |
|
575 |
%is about the size of internal data structures. |
|
576 |
to our knowledge %we don't know of a |
|
665
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
577 |
there are not other certified |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
578 |
lexing/parsing algorithms with similar data structure size bound theorems. |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
579 |
Common practices involve making empirical analysis of the complexity of the algorithm |
664 | 580 |
in question (\ref{Verbatim}, \ref{Verbatimpp}), or relying |
581 |
on prior (unformalised) complexity analysis of well-known algorithms (\ref{ValiantParsing}), |
|
582 |
making them prone to complexity bugs. |
|
583 |
%TODO: add citation |
|
584 |
%, for example in the Verbatim work \ref{Verbatim} |
|
585 |
||
586 |
%While formalised proofs have not included |
|
587 |
%Issues known as "performance bugs" can |
|
588 |
Whilst formalised complexity theorems |
|
589 |
have not yet appeared in other certified lexers/parsers, |
|
590 |
%while this work is done, |
|
591 |
they do find themselves in the broader theorem-proving literature: |
|
592 |
\emph{time credits} have been formalised for separation logic in Coq |
|
593 |
\ref{atkey2010amortised}%not used in |
|
594 |
to characterise the runtime complexity of an algorithm, |
|
595 |
where integer values are recorded %from the beginning of an execution |
|
596 |
as part of the program state |
|
597 |
and decremented in each step. |
|
598 |
The idea is that the total number of decrements |
|
599 |
from the time credits during execution represents the complexity of an algorithm. |
|
600 |
%each time a basic step is executed. |
|
601 |
%The way to use a time credit Time credit is an integer |
|
602 |
%is assigned to a program prior to execution, and each step in the program consumes |
|
603 |
%one credit. |
|
665
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
604 |
Arma{\"e}l et al. have extended the framework to allow expressing time |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
605 |
credits using big-O notations |
664 | 606 |
so one can prove both the functional correctness and asymptotic complexity |
607 |
of higher-order imperative algorithms \ref{bigOImperative}. |
|
608 |
%for example the complexity of |
|
609 |
%the Quicksort algorithm |
|
610 |
%is $\Theta n\ln n$ |
|
665
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
611 |
\marginpar{more work on formalising complexity}. |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
612 |
%Our next step is to leverage these frameworks |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
613 |
%It is a precursor to our |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
614 |
Our work focuses on the space complexity of the algorithm under our notion of the size of |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
615 |
a regular expression. |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
616 |
Despite not being a direct asymptotic time complexity proof, |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
617 |
our result is an important stepping leading towards one. |
664 | 618 |
|
665
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
619 |
|
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
620 |
Brzozowski showed that there are finitely many similar deriviatives, |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
621 |
where similarity is defined in terms of ACI equations. |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
622 |
This allows him to use derivatives as a basis for DFAs where each state is |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
623 |
labelled with a derivative. |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
624 |
However, Brzozowski did not show anything about |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
625 |
the size of the derivatives. |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
626 |
Antimirov showed that there can only be finitely |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
627 |
many partial derivatives for a regular expression and any set of |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
628 |
strings. He showed that the number is actually the |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
629 |
``alphabetical width'' plus 1. |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
630 |
From this result one can relatively easily establish |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
631 |
that the size of the partial derivatives is |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
632 |
no bigger than $(\textit{size} \; r)^3$ for every string. |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
633 |
Unfortunately this result does not seem to carry over to our |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
634 |
setting because partial derivatives have the simplification |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
635 |
\begin{equation}\label{eq:headSplitRule} |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
636 |
(r_1 + r_2) \cdot r_3 \rightarrow (r_1 \cdot r_3) + (r_2 \cdot r_3) |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
637 |
\end{equation} |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
638 |
built in. We cannot have this because otherwise we would |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
639 |
lose the POSIX property. For instance, the lexing result of |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
640 |
regular expression |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
641 |
\[ |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
642 |
(a+ab)\cdot(bc+c) |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
643 |
\] |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
644 |
with respect to string $abc$ using our lexer with the simplification rule \ref{eq:headSplitRule} |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
645 |
would be |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
646 |
\[ |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
647 |
\Left (\Seq \; (\Char \; a), \Seq (\Char \; b) \; (\Char \; c) ) |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
648 |
\] |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
649 |
instead of the correct POSIX value |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
650 |
\[ |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
651 |
\Seq \; (\Right \; (\Seq \; (\Char \; a) \; (\Char \; b)) ) \; (\Char \;) |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
652 |
\] |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
653 |
Our result about the finite bound also does not say anything about the number of derivatives. |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
654 |
In fact there are infinitely many derivatives in general |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
655 |
because in the annotated regular expression for STAR we record the |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
656 |
number of iterations. What our result shows that the size of |
3bedbdce3a3b
a bit more intro, incorporating Christian chat messages
Chengsong
parents:
664
diff
changeset
|
657 |
the derivatives is bounded, not the number. |
664 | 658 |
\marginpar{new changes up to this point.} |
659 |
||
646 | 660 |
|
661 |
In particular, the main problem we solved on top of previous work was |
|
662 |
coming up with a formally verified algorithm called $\blexersimp$ based |
|
663 |
on Brzozowski derivatives. It calculates a POSIX |
|
664 |
lexical value from a string and a regular expression. This algorithm was originally |
|
665 |
by Sulzmann and Lu \cite{Sulzmann2014}, but we made the key observation that its $\textit{nub}$ |
|
666 |
function does not really simplify intermediate results where it needs to and improved the |
|
667 |
algorithm accordingly. |
|
668 |
We have proven our $\blexersimp$'s internal data structure does not grow beyond a constant $N_r$ |
|
669 |
depending on the input regular expression $r$, thanks to the aggressive simplifications of $\blexersimp$: |
|
670 |
\begin{theorem} |
|
671 |
$|\blexersimp \; r \; s | \leq N_r$ |
|
672 |
\end{theorem} |
|
673 |
The simplifications applied in each step of $\blexersimp$ |
|
674 |
||
675 |
\begin{center} |
|
676 |
$\blexersimp |
|
677 |
$ |
|
678 |
\end{center} |
|
679 |
keeps the derivatives small, but presents a |
|
680 |
challenge |
|
681 |
||
682 |
||
683 |
establishing a correctness theorem of the below form: |
|
684 |
%TODO: change this to "theorem to prove" |
|
685 |
\begin{theorem} |
|
686 |
If the POSIX lexical value of matching regular expression $r$ with string $s$ is $v$, |
|
687 |
then $\blexersimp\; r \; s = \Some \; v$. Otherwise |
|
688 |
$\blexersimp \; r \; s = \None$. |
|
689 |
\end{theorem} |
|
690 |
||
691 |
||
692 |
||
693 |
||
638 | 694 |
The result is %a regular expression lexing algorithm that comes with |
538 | 695 |
\begin{itemize} |
696 |
\item |
|
609 | 697 |
an improved version of Sulzmann and Lu's bit-coded algorithm using |
698 |
derivatives with simplifications, |
|
699 |
accompanied by |
|
700 |
a proven correctness theorem according to POSIX specification |
|
701 |
given by Ausaf et al. \cite{AusafDyckhoffUrban2016}, |
|
702 |
\item |
|
703 |
a complexity-related property for that algorithm saying that the |
|
704 |
internal data structure will |
|
638 | 705 |
remain below a finite bound, |
538 | 706 |
\item |
638 | 707 |
and an extension to |
708 |
the bounded repetition constructs with the correctness and finiteness property |
|
609 | 709 |
maintained. |
622 | 710 |
\end{itemize} |
711 |
\noindent |
|
646 | 712 |
{\color{red} \rule{\linewidth}{0.5mm}} |
609 | 713 |
With a formal finiteness bound in place, |
714 |
we can greatly reduce the attack surface of servers in terms of ReDoS attacks. |
|
631 | 715 |
The Isabelle/HOL code for our formalisation can be |
716 |
found at |
|
717 |
\begin{center} |
|
718 |
\url{https://github.com/hellotommmy/posix} |
|
719 |
\end{center} |
|
609 | 720 |
Further improvements to the algorithm with an even stronger version of |
631 | 721 |
simplification can be made. We conjecture that the resulting size of derivatives |
722 |
can be bounded by a cubic bound w.r.t. the size of the regular expression. |
|
638 | 723 |
We will give relevant code in Scala, |
724 |
but do not give a formal proof for that in Isabelle/HOL. |
|
725 |
This is still future work. |
|
532 | 726 |
|
727 |
||
728 |
\section{Structure of the thesis} |
|
652 | 729 |
\marginpar{\em This is a marginal note.} |
730 |
Before talking about the formal proof of $\blexersimp$'s |
|
731 |
correctness, which is the main contribution of this thesis, |
|
732 |
we need to introduce two formal proofs which belong |
|
733 |
to Ausafe et al. |
|
622 | 734 |
In chapter \ref{Inj} we will introduce the concepts |
532 | 735 |
and notations we |
622 | 736 |
use for describing regular expressions and derivatives, |
638 | 737 |
and the first version of Sulzmann and Lu's lexing algorithm without bitcodes (including |
622 | 738 |
its correctness proof). |
739 |
We will give their second lexing algorithm with bitcodes in \ref{Bitcoded1} |
|
740 |
together with the correctness proof by Ausaf and Urban. |
|
741 |
Then we illustrate in chapter \ref{Bitcoded2} |
|
742 |
how Sulzmann and Lu's |
|
638 | 743 |
simplifications fail to simplify correctly. We therefore introduce our own version of the |
744 |
algorithm with correct simplifications and |
|
745 |
their correctness proof. |
|
622 | 746 |
In chapter \ref{Finite} we give the second guarantee |
532 | 747 |
of our bitcoded algorithm, that is a finite bound on the size of any |
631 | 748 |
regular expression's derivatives. |
749 |
We also show how one can extend the |
|
750 |
algorithm to include bounded repetitions. |
|
622 | 751 |
In chapter \ref{Cubic} we discuss stronger simplification rules which |
638 | 752 |
improve the finite bound to a cubic bound. %and the NOT regular expression. |
637 | 753 |
Chapter \ref{RelatedWork} introduces relevant work for this thesis. |
638 | 754 |
Chapter \ref{Future} concludes and mentions avenues of future research. |
532 | 755 |
|
756 |
||
757 |
||
758 |
||
759 |
||
760 |
%---------------------------------------------------------------------------------------- |
|
761 |
||
762 |
||
763 |
%---------------------------------------------------------------------------------------- |
|
764 |
||
765 |
%---------------------------------------------------------------------------------------- |
|
766 |
||
767 |
%---------------------------------------------------------------------------------------- |
|
768 |
||
769 |