1 | \chapter{Introduction}
|
---|
2 |
|
---|
3 | % The highest level overview of Cforall and EHMs. Get this done right away.
|
---|
4 | This thesis covers the design and implementation of the exception handling
|
---|
5 | mechanism (EHM) of
|
---|
6 | \CFA (pronounced sea-for-all and may be written Cforall or CFA).
|
---|
7 | \CFA is a new programming language that extends C, which maintains
|
---|
8 | backwards-compatibility while introducing modern programming features.
|
---|
9 | Adding exception handling to \CFA gives it new ways to handle errors and
|
---|
10 | make large control-flow jumps.
|
---|
11 |
|
---|
12 | % Now take a step back and explain what exceptions are generally.
|
---|
13 | Exception handling provides dynamic inter-function control flow.
|
---|
14 | A language's EHM is a combination of language syntax and run-time
|
---|
15 | components that construct, raise, propagate and handle exceptions,
|
---|
16 | to provide all of that control flow.
|
---|
17 | There are two forms of exception handling covered in this thesis:
|
---|
18 | termination, which acts as a multi-level return,
|
---|
19 | and resumption, which is a dynamic function call.
|
---|
20 | % About other works:
|
---|
21 | Often, when this separation is not made, termination exceptions are assumed
|
---|
22 | as they are more common and may be the only form of handling provided in
|
---|
23 | a language.
|
---|
24 |
|
---|
25 | All types of exception handling link a raise with a handler.
|
---|
26 | Both operations are usually language primitives, although raises can be
|
---|
27 | treated as a function that takes an exception argument.
|
---|
28 | Handlers are more complex, as they are added to and removed from the stack
|
---|
29 | during execution, must specify what they can handle and must give the code to
|
---|
30 | handle the exception.
|
---|
31 |
|
---|
32 | Exceptions work with different execution models but for the descriptions
|
---|
33 | that follow a simple call stack, with functions added and removed in a
|
---|
34 | first-in-last-out order, is assumed.
|
---|
35 |
|
---|
36 | Termination exception handling searches the stack for the handler, then
|
---|
37 | unwinds the stack to where the handler was found before calling it.
|
---|
38 | The handler is run inside the function that defined it and when it finishes
|
---|
39 | it returns control to that function.
|
---|
40 | \begin{center}
|
---|
41 | \input{termination}
|
---|
42 | \end{center}
|
---|
43 | %\todo{What does the right half of termination.fig mean?}
|
---|
44 |
|
---|
45 | Resumption exception handling searches the stack for a handler and then calls
|
---|
46 | it without removing any other stack frames.
|
---|
47 | The handler is run on top of the existing stack, often as a new function or
|
---|
48 | closure capturing the context in which the handler was defined.
|
---|
49 | After the handler has finished running, it returns control to the function
|
---|
50 | that preformed the raise, usually starting after the raise.
|
---|
51 | \begin{center}
|
---|
52 | \input{resumption}
|
---|
53 | \end{center}
|
---|
54 |
|
---|
55 | Although a powerful feature, exception handling tends to be complex to set up
|
---|
56 | and expensive to use,
|
---|
57 | so it is often limited to unusual or ``exceptional" cases.
|
---|
58 | The classic example is error handling; exceptions can be used to
|
---|
59 | remove error handling logic from the main execution path, and pay
|
---|
60 | most of the cost only when the error actually occurs.
|
---|
61 |
|
---|
62 | \section{Thesis Overview}
|
---|
63 | This work describes the design and implementation of the \CFA EHM.
|
---|
64 | The \CFA EHM implements all of the common exception features (or an
|
---|
65 | equivalent) found in most other EHMs and adds some features of its own.
|
---|
66 | The design of all the features had to be adapted to \CFA's feature set, as
|
---|
67 | some of the underlying tools used to implement and express exception handling
|
---|
68 | in other languages are absent in \CFA.
|
---|
69 | Still, the resulting syntax resembles that of other languages:
|
---|
70 | \begin{cfa}
|
---|
71 | try {
|
---|
72 | ...
|
---|
73 | T * object = malloc(request_size);
|
---|
74 | if (!object) {
|
---|
75 | throw OutOfMemory{fixed_allocation, request_size};
|
---|
76 | }
|
---|
77 | ...
|
---|
78 | } catch (OutOfMemory * error) {
|
---|
79 | ...
|
---|
80 | }
|
---|
81 | \end{cfa}
|
---|
82 | % A note that yes, that was a very fast overview.
|
---|
83 | The design and implementation of all of \CFA's EHM's features are
|
---|
84 | described in detail throughout this thesis, whether they are a common feature
|
---|
85 | or one unique to \CFA.
|
---|
86 |
|
---|
87 | % The current state of the project and what it contributes.
|
---|
88 | All of these features have been implemented in \CFA,
|
---|
89 | covering both changes to the compiler and the run-time.
|
---|
90 | In addition, a suite of test cases and performance benchmarks were created
|
---|
91 | alongside the implementation.
|
---|
92 | The implementation techniques are generally applicable in other programming
|
---|
93 | languages and much of the design is as well.
|
---|
94 | Some parts of the EHM use other features unique to \CFA and would be
|
---|
95 | harder to replicate in other programming languages.
|
---|
96 |
|
---|
97 | The contributions of this work are:
|
---|
98 | \begin{enumerate}
|
---|
99 | \item Designing \CFA's exception handling mechanism, adapting designs from
|
---|
100 | other programming languages and creating new features.
|
---|
101 | \item Implementing stack unwinding and the \CFA EHM, including updating
|
---|
102 | the \CFA compiler and the run-time environment.
|
---|
103 | \item Designing and implementing a prototype virtual system.
|
---|
104 | % I think the virtual system and per-call site default handlers are the only
|
---|
105 | % "new" features, everything else is a matter of implementation.
|
---|
106 | \item Creating tests to check the behaviour of the EHM.
|
---|
107 | \item Creating benchmarks to check the performance of the EHM,
|
---|
108 | as compared to other languages.
|
---|
109 | \end{enumerate}
|
---|
110 |
|
---|
111 | The rest of this thesis is organized as follows.
|
---|
112 | The current state of exceptions is covered in \autoref{s:background}.
|
---|
113 | The existing state of \CFA is covered in \autoref{c:existing}.
|
---|
114 | New EHM features are introduced in \autoref{c:features},
|
---|
115 | covering their usage and design.
|
---|
116 | That is followed by the implementation of these features in
|
---|
117 | \autoref{c:implement}.
|
---|
118 | Performance results are examined in \autoref{c:performance}.
|
---|
119 | Possibilities to extend this project are discussed in \autoref{c:future}.
|
---|
120 | Finally, the project is summarized in \autoref{c:conclusion}.
|
---|
121 |
|
---|
122 | \section{Background}
|
---|
123 | \label{s:background}
|
---|
124 |
|
---|
125 | Exception handling has been examined before in programming languages,
|
---|
126 | with papers on the subject dating back 70s.\cite{Goodenough75}
|
---|
127 | Early exceptions were often treated as signals, which carried no information
|
---|
128 | except their identity.
|
---|
129 | Ada originally used this system\cite{Ada}, but now allows for a string
|
---|
130 | message as a payload\cite{Ada12}.
|
---|
131 |
|
---|
132 | The modern flag-ship for termination exceptions is \Cpp,
|
---|
133 | which added them in its first major wave of non-object-orientated features
|
---|
134 | in 1990.\cite{CppHistory}
|
---|
135 | Many EHMs have special exception types,
|
---|
136 | however \Cpp has the ability to use any type as an exception.
|
---|
137 | These were found to be not very useful and have been pushed aside for classes
|
---|
138 | inheriting from
|
---|
139 | \code{C++}{std::exception}.
|
---|
140 | Although there is a special catch-all syntax (@catch(...)@), there are no
|
---|
141 | operations that can be performed on the caught value, not even type inspection.
|
---|
142 | Instead, the base exception-type \code{C++}{std::exception} defines common
|
---|
143 | functionality (such as
|
---|
144 | the ability to describe the reason the exception was raised) and all
|
---|
145 | exceptions have this functionality.
|
---|
146 | That trade-off, restricting usable types to gain guaranteed functionality,
|
---|
147 | is almost universal now, as without some common functionality it is almost
|
---|
148 | impossible to actually handle any errors.
|
---|
149 |
|
---|
150 | Java was the next popular language to use exceptions.\cite{Java8}
|
---|
151 | Its exception system largely reflects that of \Cpp, except that it requires
|
---|
152 | you throw a child type of \code{Java}{java.lang.Throwable}
|
---|
153 | and it uses checked exceptions.
|
---|
154 | Checked exceptions are part of a function's interface,
|
---|
155 | the exception signature of the function.
|
---|
156 | Every exception that could be raised from a function, either directly or
|
---|
157 | because it is not handled from a called function, is given.
|
---|
158 | Using this information, it is possible to statically verify if any given
|
---|
159 | exception is handled, and guarantee that no exception will go unhandled.
|
---|
160 | Making exception information explicit improves clarity and safety,
|
---|
161 | but can slow down or restrict programming.
|
---|
162 | For example, programming high-order functions becomes much more complex
|
---|
163 | if the argument functions could raise exceptions.
|
---|
164 | However, as odd it may seem, the worst problems are rooted in the simple
|
---|
165 | inconvenience of writing and updating exception signatures.
|
---|
166 | This has caused Java programmers to develop multiple programming ``hacks''
|
---|
167 | to circumvent checked exceptions, negating their advantages.
|
---|
168 | One particularly problematic example is the ``catch-and-ignore'' pattern,
|
---|
169 | where an empty handler is used to handle an exception without doing any
|
---|
170 | recovery or repair. In theory that could be good enough to properly handle
|
---|
171 | the exception, but more often is used to ignore an exception that the
|
---|
172 | programmer does not feel is worth the effort of handling, for instance if
|
---|
173 | they do not believe it will ever be raised.
|
---|
174 | If they are incorrect, the exception will be silenced, while in a similar
|
---|
175 | situation with unchecked exceptions the exception would at least activate
|
---|
176 | the language's unhandled exception code (usually, a program abort with an
|
---|
177 | error message).
|
---|
178 |
|
---|
179 | %\subsection
|
---|
180 | Resumption exceptions are less popular,
|
---|
181 | although resumption is as old as termination; that is, few
|
---|
182 | programming languages have implemented them.
|
---|
183 | % http://bitsavers.informatik.uni-stuttgart.de/pdf/xerox/parc/techReports/
|
---|
184 | % CSL-79-3_Mesa_Language_Manual_Version_5.0.pdf
|
---|
185 | Mesa is one programming language that did.\cite{Mesa} Experience with Mesa
|
---|
186 | is quoted as being one of the reasons resumptions were not
|
---|
187 | included in the \Cpp standard.
|
---|
188 | % https://en.wikipedia.org/wiki/Exception_handling
|
---|
189 | Since then, resumptions have been ignored in main-stream programming languages.
|
---|
190 | However, resumption is being revisited in the context of decades of other
|
---|
191 | developments in programming languages.
|
---|
192 | While rejecting resumption may have been the right decision in the past,
|
---|
193 | the situation has changed since then.
|
---|
194 | Some developments, such as the functional programming equivalent to resumptions,
|
---|
195 | algebraic effects\cite{Zhang19}, are enjoying success.
|
---|
196 | A complete reexamination of resumption is beyond this thesis,
|
---|
197 | but their reemergence is enough reason to try them in \CFA.
|
---|
198 | % Especially considering how much easier they are to implement than
|
---|
199 | % termination exceptions and how much Peter likes them.
|
---|
200 |
|
---|
201 | %\subsection
|
---|
202 | Functional languages tend to use other solutions for their primary error
|
---|
203 | handling mechanism, but exception-like constructs still appear.
|
---|
204 | Termination appears in the error construct, which marks the result of an
|
---|
205 | expression as an error; then the result of any expression that tries to use
|
---|
206 | it also results in an error, and so on until an appropriate handler is reached.
|
---|
207 | Resumption appears in algebraic effects, where a function dispatches its
|
---|
208 | side-effects to its caller for handling.
|
---|
209 |
|
---|
210 | %\subsection
|
---|
211 | More recently exceptions, seem to be vanishing from newer programming
|
---|
212 | languages, replaced by ``panic".
|
---|
213 | In Rust, a panic is just a program level abort that may be implemented by
|
---|
214 | unwinding the stack like in termination exception
|
---|
215 | handling.\cite{RustPanicMacro}\cite{RustPanicModule}
|
---|
216 | Go's panic through is very similar to a termination, except it only supports
|
---|
217 | a catch-all by calling \code{Go}{recover()}, simplifying the interface at
|
---|
218 | the cost of flexibility.\cite{Go:2021}
|
---|
219 |
|
---|
220 | %\subsection
|
---|
221 | As exception handling's most common use cases are in error handling,
|
---|
222 | here are some other ways to handle errors with comparisons with exceptions.
|
---|
223 | \begin{itemize}
|
---|
224 | \item\emph{Error Codes}:
|
---|
225 | This pattern has a function return an enumeration (or just a set of fixed
|
---|
226 | values) to indicate if an error has occurred and possibly which error it was.
|
---|
227 |
|
---|
228 | Error codes mix exceptional/error and normal values, enlarging the range of
|
---|
229 | possible return values. This can be addressed with multiple return values
|
---|
230 | (or a tuple) or a tagged union.
|
---|
231 | However, the main issue with error codes is forgetting to check them,
|
---|
232 | which leads to an error being quietly and implicitly ignored.
|
---|
233 | Some new languages and tools will try to issue warnings when an error code
|
---|
234 | is discarded to avoid this problem.
|
---|
235 | Checking error codes also bloats the main execution path,
|
---|
236 | especially if the error is not handled immediately and has to be passed
|
---|
237 | through multiple functions before it is addressed.
|
---|
238 |
|
---|
239 | \item\emph{Special Return with Global Store}:
|
---|
240 | Similar to the error codes pattern but the function itself only returns
|
---|
241 | that there was an error,
|
---|
242 | and stores the reason for the error in a fixed global location.
|
---|
243 | For example, many routines in the C standard library will only return some
|
---|
244 | error value (such as -1 or a null pointer) and the error code is written into
|
---|
245 | the standard variable @errno@.
|
---|
246 |
|
---|
247 | This approach avoids the multiple results issue encountered with straight
|
---|
248 | error codes but otherwise has the same disadvantages and more.
|
---|
249 | Every function that reads or writes to the global store must agree on all
|
---|
250 | possible errors and managing it becomes more complex with concurrency.
|
---|
251 |
|
---|
252 | \item\emph{Return Union}:
|
---|
253 | This pattern replaces error codes with a tagged union.
|
---|
254 | Success is one tag and the errors are another.
|
---|
255 | It is also possible to make each possible error its own tag and carry its own
|
---|
256 | additional information, but the two-branch format is easy to make generic
|
---|
257 | so that one type can be used everywhere in error handling code.
|
---|
258 |
|
---|
259 | This pattern is very popular in any functional or semi-functional language
|
---|
260 | with primitive support for tagged unions (or algebraic data types).
|
---|
261 | % We need listing Rust/rust to format code snippets from it.
|
---|
262 | % Rust's \code{rust}{Result<T, E>}
|
---|
263 | The main advantage is that an arbitrary object can be used to represent an
|
---|
264 | error, so it can include a lot more information than a simple error code.
|
---|
265 | The disadvantages include that the it does have to be checked along the main
|
---|
266 | execution, and if there aren't primitive tagged unions proper, usage can be
|
---|
267 | hard to enforce.
|
---|
268 |
|
---|
269 | \item\emph{Handler Functions}:
|
---|
270 | This pattern associates errors with functions.
|
---|
271 | On error, the function that produced the error calls another function to
|
---|
272 | handle it.
|
---|
273 | The handler function can be provided locally (passed in as an argument,
|
---|
274 | either directly as as a field of a structure/object) or globally (a global
|
---|
275 | variable).
|
---|
276 | C++ uses this approach as its fallback system if exception handling fails,
|
---|
277 | such as \snake{std::terminate} and, for a time,
|
---|
278 | \snake{std::unexpected}.\footnote{\snake{std::unexpected} was part of the
|
---|
279 | Dynamic Exception Specification, which has been removed from the standard
|
---|
280 | as of C++20.\cite{CppExceptSpec}}
|
---|
281 |
|
---|
282 | Handler functions work a lot like resumption exceptions,
|
---|
283 | but without the dynamic search for a handler.
|
---|
284 | Since setting up the handler can be more complex/expensive,
|
---|
285 | especially when the handler has to be passed through multiple layers of
|
---|
286 | function calls, but cheaper (constant time) to call,
|
---|
287 | they are more suited to more frequent (less exceptional) situations.
|
---|
288 | \end{itemize}
|
---|
289 |
|
---|
290 | %\subsection
|
---|
291 | Because of their cost, exceptions are rarely used for hot paths of execution.
|
---|
292 | Hence, there is an element of self-fulfilling prophecy as implementation
|
---|
293 | techniques have been focused on making them cheap to set-up,
|
---|
294 | happily making them expensive to use in exchange.
|
---|
295 | This difference is less important in higher-level scripting languages,
|
---|
296 | where using exceptions for other tasks is more common.
|
---|
297 | An iconic example is Python's
|
---|
298 | \code{Python}{StopIteration}\cite{PythonExceptions} exception, that
|
---|
299 | is thrown by an iterator to indicate that it is exhausted.
|
---|
300 | When paired with Python's iterator-based for-loop, this will be thrown every
|
---|
301 | time the end of the loop is reached.\cite{PythonForLoop}
|
---|