1 | \chapter{Implementation} |
---|
2 | % Goes over how all the features are implemented. |
---|
3 | |
---|
4 | The implementation work for this thesis covers two components: the virtual |
---|
5 | system and exceptions. Each component is discussed in detail. |
---|
6 | |
---|
7 | \section{Virtual System} |
---|
8 | \label{s:VirtualSystem} |
---|
9 | % Virtual table rules. Virtual tables, the pointer to them and the cast. |
---|
10 | While the \CFA virtual system currently has only one public feature, virtual |
---|
11 | cast (see the virtual cast feature \vpageref{p:VirtualCast}), |
---|
12 | substantial structure is required to support it, |
---|
13 | and provide features for exception handling and the standard library. |
---|
14 | |
---|
15 | \subsection{Virtual Type} |
---|
16 | Virtual types only have one change to their structure: the addition of a |
---|
17 | pointer to the virtual table, which is called the \emph{virtual-table pointer}. |
---|
18 | Internally, the field is called \snake{virtual_table}. |
---|
19 | The field is fixed after construction. It is always the first field in the |
---|
20 | structure so that its location is always known. |
---|
21 | \todo{Talk about constructors for virtual types (after they are working).} |
---|
22 | |
---|
23 | This is what binds an instance of a virtual type to a virtual table. This |
---|
24 | pointer can be used as an identity check. It can also be used to access the |
---|
25 | virtual table and the virtual members there. |
---|
26 | |
---|
27 | \subsection{Type Id} |
---|
28 | Every virtual type has a unique id. |
---|
29 | Type ids can be compared for equality (the types reperented are the same) |
---|
30 | or used to access the type's type information. |
---|
31 | The type information currently is only the parent's type id or, if the |
---|
32 | type has no parent, zero. |
---|
33 | |
---|
34 | The id's are implemented as pointers to the type's type information instance. |
---|
35 | Derefencing the pointer gets the type information. |
---|
36 | By going back-and-forth between the type id and |
---|
37 | the type info one can find every ancestor of a virtual type. |
---|
38 | It also pushes the issue of creating a unique value (for |
---|
39 | the type id) to the problem of creating a unique instance (for type |
---|
40 | information) which the linker can solve. |
---|
41 | |
---|
42 | Advanced linker support is required because there is no place that appears |
---|
43 | only once to attach the type information to. There should be one structure |
---|
44 | definition but it is included in multiple translation units. Each virtual |
---|
45 | table definition should be unique but there are an arbitrary number of thoses. |
---|
46 | So the special section prefix \texttt{.gnu.linkonce} is used. |
---|
47 | With a unique suffix (making the entire section name unique) the linker will |
---|
48 | remove multiple definition making sure only one version exists after linking. |
---|
49 | Then it is just a matter of making sure there is a unique name for each type. |
---|
50 | |
---|
51 | This is done in three phases. |
---|
52 | The first phase is to generate a new structure definition to store the type |
---|
53 | information. The layout is the same in each case, just the parent's type id, |
---|
54 | but the types are changed. |
---|
55 | The structure's name is change, it is based off the virtual type's name, and |
---|
56 | the type of the parent's type id. |
---|
57 | If the virtual type is polymorphic then the type information structure is |
---|
58 | polymorphic as well, with the same polymorphic arguments. |
---|
59 | |
---|
60 | The second phase is to generate an instance of the type information with a |
---|
61 | almost unique name, generated by mangling the virtual type name. |
---|
62 | |
---|
63 | The third phase is implicit with \CFA's overloading scheme. \CFA mangles |
---|
64 | names with type information so that all of the symbols exported to the linker |
---|
65 | are unique even if in \CFA code they are the same. Having two declarations |
---|
66 | with the same name and same type is forbidden because it is impossible for |
---|
67 | overload resolution to pick between them. This is why a unique type is |
---|
68 | generated for each virtual type. |
---|
69 | Polymorphic information is included in this mangling so polymorphic |
---|
70 | types will have seperate instances for each set of polymorphic arguments. |
---|
71 | |
---|
72 | \begin{cfa} |
---|
73 | struct TYPE_ID_TYPE { |
---|
74 | PARENT_ID_TYPE const * parent; |
---|
75 | }; |
---|
76 | |
---|
77 | __attribute__((cfa_linkonce)) |
---|
78 | TYPE_ID_TYPE const TYPE_ID_NAME = { |
---|
79 | &PARENT_ID_NAME, |
---|
80 | }; |
---|
81 | \end{cfa} |
---|
82 | |
---|
83 | \subsubsection{cfa\_linkonce Attribute} |
---|
84 | Another feature added to \CFA is a new attribute: \texttt{cfa\_linkonce}. |
---|
85 | This attribute can be put on an object or function definition |
---|
86 | (any global declaration with a name and a type). |
---|
87 | This allows you to define that object or function multiple times. |
---|
88 | All definitions should have the link-once attribute on them and all should |
---|
89 | be identical. |
---|
90 | |
---|
91 | The simplist way to use it is to put a definition in a header where the |
---|
92 | forward declaration would usually go. |
---|
93 | This is how it is used for type-id instances. There was is no unique location |
---|
94 | associated with a type except for the type definition which is in a header. |
---|
95 | This allows the unique type-id object to be generated there. |
---|
96 | |
---|
97 | Internally @cfa_linkonce@ removes all @section@ attributes |
---|
98 | from the declaration (as well as itself) and replaces them with |
---|
99 | @section(".gnu.linkonce.NAME")@ where \texttt{NAME} is replaced by the |
---|
100 | mangled name of the object. |
---|
101 | The prefix \texttt{.gnu.linkonce} in section names is recognized by the |
---|
102 | linker. If two of these sections with the same name, including everything |
---|
103 | that comes after the special prefix, then only one will be used and the other |
---|
104 | will be discarded. |
---|
105 | |
---|
106 | \subsection{Virtual Table} |
---|
107 | Each virtual type has a virtual table type that stores its type id and |
---|
108 | virtual members. |
---|
109 | Each virtual type instance is bound to a table instance that is filled with |
---|
110 | the values of virtual members. |
---|
111 | Both the layout of the fields and their value are decided by the rules given |
---|
112 | below. |
---|
113 | |
---|
114 | The layout always comes in three parts. |
---|
115 | The first section is just the type id at the head of the table. It is always |
---|
116 | there to ensure that |
---|
117 | The second section are all the virtual members of the parent, in the same |
---|
118 | order as they appear in the parent's virtual table. Note that the type may |
---|
119 | change slightly as references to the ``this" will change. This is limited to |
---|
120 | inside pointers/references and via function pointers so that the size (and |
---|
121 | hence the offsets) are the same. |
---|
122 | The third section is similar to the second except that it is the new virtual |
---|
123 | members introduced at this level in the hierarchy. |
---|
124 | |
---|
125 | \begin{figure} |
---|
126 | \input{vtable-layout} |
---|
127 | \caption{Virtual Table Layout} |
---|
128 | \label{f:VirtualTableLayout} |
---|
129 | \todo*{Improve the Virtual Table Layout diagram.} |
---|
130 | \end{figure} |
---|
131 | |
---|
132 | The first and second sections together mean that every virtual table has a |
---|
133 | prefix that has the same layout and types as its parent virtual table. |
---|
134 | This, combined with the fixed offset to the virtual table pointer, means that |
---|
135 | for any virtual type it doesn't matter if we have it or any of its |
---|
136 | descendants, it is still always safe to access the virtual table through |
---|
137 | the virtual table pointer. |
---|
138 | From there it is safe to check the type id to identify the exact type of the |
---|
139 | underlying object, access any of the virtual members and pass the object to |
---|
140 | any of the method-like virtual members. |
---|
141 | |
---|
142 | When a virtual table is declared the user decides where to declare it and its |
---|
143 | name. The initialization of the virtual table is entirely automatic based on |
---|
144 | the context of the declaration. |
---|
145 | |
---|
146 | The type id is always fixed, each virtual table type will always have one |
---|
147 | exactly one possible type id. |
---|
148 | The virtual members are usually filled in by resolution. The best match for |
---|
149 | a given name and type at the declaration site is filled in. |
---|
150 | There are two exceptions to that rule: the @size@ field is the type's size |
---|
151 | and is set to the result of a @sizeof@ expression, the @align@ field is the |
---|
152 | type's alignment and similarly uses an @alignof@ expression. |
---|
153 | |
---|
154 | \subsubsection{Concurrency Integration} |
---|
155 | Coroutines and threads need instances of @CoroutineCancelled@ and |
---|
156 | @ThreadCancelled@ respectively to use all of their functionality. When a new |
---|
157 | data type is declared with @coroutine@ or @thread@ the forward declaration for |
---|
158 | the instance is created as well. The definition of the virtual table is created |
---|
159 | at the definition of the main function. |
---|
160 | |
---|
161 | \begin{figure} |
---|
162 | \begin{cfa} |
---|
163 | coroutine Example { |
---|
164 | // fields |
---|
165 | }; |
---|
166 | \end{cfa} |
---|
167 | |
---|
168 | \begin{cfa} |
---|
169 | __attribute__((cfa_linkonce)) |
---|
170 | struct __cfatid_struct_CoroutineCancelled(Example) |
---|
171 | __cfatid_CoroutineCancelled = { |
---|
172 | &EXCEPTION_TYPE_ID, |
---|
173 | }; |
---|
174 | extern CoroutineCancelled_vtable _default_vtable_object_declaration; |
---|
175 | extern CoroutineCancelled_vtable & _default_vtable; |
---|
176 | \end{cfa} |
---|
177 | |
---|
178 | \begin{cfa} |
---|
179 | void main(Example & this) { |
---|
180 | // body |
---|
181 | } |
---|
182 | \end{cfa} |
---|
183 | |
---|
184 | \begin{cfa} |
---|
185 | CoroutineCancelled_vtable _default_vtable_object_declaration = { |
---|
186 | __cfatid_CoroutineCancelled, |
---|
187 | // Virtual member initialization. |
---|
188 | }; |
---|
189 | |
---|
190 | CoroutineCancelled_vtable & _default_vtable = |
---|
191 | &_default_vtable_object_declaration; |
---|
192 | \end{cfa} |
---|
193 | \caption{Concurrency Transformations} |
---|
194 | \label{f:ConcurrencyTransformations} |
---|
195 | \end{figure} |
---|
196 | \todo{Improve Concurrency Transformations figure.} |
---|
197 | |
---|
198 | \subsection{Virtual Cast} |
---|
199 | Virtual casts are implemented as a function call that does the subtype check |
---|
200 | and a C coercion-cast to do the type conversion. |
---|
201 | % The C-cast is just to make sure the generated code is correct so the rest of |
---|
202 | % the section is about that function. |
---|
203 | The function is implemented in the standard library and has the following |
---|
204 | signature: |
---|
205 | \begin{cfa} |
---|
206 | void * __cfa__virtual_cast( |
---|
207 | struct __cfavir_type_td parent, |
---|
208 | struct __cfavir_type_id const * child ); |
---|
209 | \end{cfa} |
---|
210 | The type id of target type of the virtual cast is passed in as @parent@ and |
---|
211 | the cast target is passed in as @child@. |
---|
212 | |
---|
213 | For C generation both arguments and the result are wrapped with type casts. |
---|
214 | There is also an internal store inside the compiler to make sure that the |
---|
215 | target type is a virtual type. |
---|
216 | % It also checks for conflicting definitions. |
---|
217 | |
---|
218 | The virtual cast either returns the original pointer as a new type or null. |
---|
219 | So the function just does the parent check and returns the approprate value. |
---|
220 | The parent check is a simple linear search of child's ancestors using the |
---|
221 | type information. |
---|
222 | |
---|
223 | \section{Exceptions} |
---|
224 | % Anything about exception construction. |
---|
225 | |
---|
226 | \section{Unwinding} |
---|
227 | % Adapt the unwind chapter, just describe the sections of libunwind used. |
---|
228 | % Mention that termination and cancellation use it. Maybe go into why |
---|
229 | % resumption doesn't as well. |
---|
230 | |
---|
231 | % Many modern languages work with an interal stack that function push and pop |
---|
232 | % their local data to. Stack unwinding removes large sections of the stack, |
---|
233 | % often across functions. |
---|
234 | |
---|
235 | Stack unwinding is the process of removing stack frames (activations) from the |
---|
236 | stack. On function entry and return, unwinding is handled directly by the |
---|
237 | call/return code embedded in the function. |
---|
238 | In many cases the position of the instruction pointer (relative to parameter |
---|
239 | and local declarations) is enough to know the current size of the stack |
---|
240 | frame. |
---|
241 | |
---|
242 | Usually, the stack-frame size is known statically based on parameter and |
---|
243 | local variable declarations. Even with dynamic stack-size the information |
---|
244 | to determain how much of the stack has to be removed is still contained |
---|
245 | within the function. |
---|
246 | Allocating/deallocating stack space is usually an $O(1)$ operation achieved by |
---|
247 | bumping the hardware stack-pointer up or down as needed. |
---|
248 | Constructing/destructing values on the stack takes longer put in terms of |
---|
249 | figuring out what needs to be done is of similar complexity. |
---|
250 | |
---|
251 | Unwinding across multiple stack frames is more complex because that |
---|
252 | information is no longer contained within the current function. |
---|
253 | With seperate compilation a function has no way of knowing what its callers |
---|
254 | are so it can't know how large those frames are. |
---|
255 | Without altering the main code path it is also hard to pass that work off |
---|
256 | to the caller. |
---|
257 | |
---|
258 | The traditional unwinding mechanism for C is implemented by saving a snap-shot |
---|
259 | of a function's state with @setjmp@ and restoring that snap-shot with |
---|
260 | @longjmp@. This approach bypasses the need to know stack details by simply |
---|
261 | reseting to a snap-shot of an arbitrary but existing function frame on the |
---|
262 | stack. It is up to the programmer to ensure the snap-shot is valid when it is |
---|
263 | reset and that all required clean-up from the unwound stacks is preformed. |
---|
264 | This approach is fragile and forces a work onto the surounding code. |
---|
265 | |
---|
266 | With respect to that work forced onto the surounding code, |
---|
267 | many languages define clean-up actions that must be taken when certain |
---|
268 | sections of the stack are removed. Such as when the storage for a variable |
---|
269 | is removed from the stack or when a try statement with a finally clause is |
---|
270 | (conceptually) popped from the stack. |
---|
271 | None of these should be handled by the user, that would contradict the |
---|
272 | intention of these features, so they need to be handled automatically. |
---|
273 | |
---|
274 | To safely remove sections of the stack the language must be able to find and |
---|
275 | run these clean-up actions even when removing multiple functions unknown at |
---|
276 | the beginning of the unwinding. |
---|
277 | |
---|
278 | One of the most popular tools for stack management is libunwind, a low-level |
---|
279 | library that provides tools for stack walking, handler execution, and |
---|
280 | unwinding. What follows is an overview of all the relevant features of |
---|
281 | libunwind needed for this work, and how \CFA uses them to implement exception |
---|
282 | handling. |
---|
283 | |
---|
284 | \subsection{libunwind Usage} |
---|
285 | Libunwind, accessed through @unwind.h@ on most platforms, is a C library that |
---|
286 | provides \Cpp-style stack-unwinding. Its operation is divided into two phases: |
---|
287 | search and cleanup. The dynamic target search -- phase 1 -- is used to scan the |
---|
288 | stack and decide where unwinding should stop (but no unwinding occurs). The |
---|
289 | cleanup -- phase 2 -- does the unwinding and also runs any cleanup code. |
---|
290 | |
---|
291 | To use libunwind, each function must have a personality function and a Language |
---|
292 | Specific Data Area (LSDA). The LSDA has the unique information for each |
---|
293 | function to tell the personality function where a function is executing, its |
---|
294 | current stack frame, and what handlers should be checked. Theoretically, the |
---|
295 | LSDA can contain any information but conventionally it is a table with entries |
---|
296 | representing regions of the function and what has to be done there during |
---|
297 | unwinding. These regions are bracketed by instruction addresses. If the |
---|
298 | instruction pointer is within a region's start/end, then execution is currently |
---|
299 | executing in that region. Regions are used to mark out the scopes of objects |
---|
300 | with destructors and try blocks. |
---|
301 | |
---|
302 | % Libunwind actually does very little, it simply moves down the stack from |
---|
303 | % function to function. Most of the actions are implemented by the personality |
---|
304 | % function which libunwind calls on every function. Since this is shared across |
---|
305 | % many functions or even every function in a language it will need a bit more |
---|
306 | % information. |
---|
307 | |
---|
308 | The GCC compilation flag @-fexceptions@ causes the generation of an LSDA and |
---|
309 | attaches a personality function to each function. |
---|
310 | In plain C (which \CFA currently compiles down to) this |
---|
311 | flag only handles the cleanup attribute: |
---|
312 | \begin{cfa} |
---|
313 | void clean_up( int * var ) { ... } |
---|
314 | int avar __attribute__(( cleanup(clean_up) )); |
---|
315 | \end{cfa} |
---|
316 | The attribue is used on a variable and specifies a function, |
---|
317 | in this case @clean_up@, run when the variable goes out of scope. |
---|
318 | This is enough to mimic destructors, but not try statements which can effect |
---|
319 | the unwinding. |
---|
320 | |
---|
321 | To get full unwinding support all of this has to be done directly with |
---|
322 | assembly and assembler directives. Partiularly the cfi directives |
---|
323 | \snake{.cfi_lsda} and \snake{.cfi_personality}. |
---|
324 | |
---|
325 | \subsection{Personality Functions} |
---|
326 | Personality functions have a complex interface specified by libunwind. This |
---|
327 | section covers some of the important parts of the interface. |
---|
328 | |
---|
329 | A personality function can preform different actions depending on how it is |
---|
330 | called. |
---|
331 | \begin{lstlisting} |
---|
332 | typedef _Unwind_Reason_Code (*_Unwind_Personality_Fn) ( |
---|
333 | _Unwind_Action action, |
---|
334 | _Unwind_Exception_Class exception_class, |
---|
335 | _Unwind_Exception * exception, |
---|
336 | struct _Unwind_Context * context); |
---|
337 | \end{lstlisting} |
---|
338 | The @action@ argument is a bitmask of possible actions: |
---|
339 | \begin{enumerate}[topsep=5pt] |
---|
340 | \item |
---|
341 | @_UA_SEARCH_PHASE@ specifies a search phase and tells the personality function |
---|
342 | to check for handlers. If there is a handler in a stack frame, as defined by |
---|
343 | the language, the personality function returns @_URC_HANDLER_FOUND@; otherwise |
---|
344 | it return @_URC_CONTINUE_UNWIND@. |
---|
345 | |
---|
346 | \item |
---|
347 | @_UA_CLEANUP_PHASE@ specifies a cleanup phase, where the entire frame is |
---|
348 | unwound and all cleanup code is run. The personality function does whatever |
---|
349 | cleanup the language defines (such as running destructors/finalizers) and then |
---|
350 | generally returns @_URC_CONTINUE_UNWIND@. |
---|
351 | |
---|
352 | \item |
---|
353 | \begin{sloppypar} |
---|
354 | @_UA_HANDLER_FRAME@ specifies a cleanup phase on a function frame that found a |
---|
355 | handler. The personality function must prepare to return to normal code |
---|
356 | execution and return @_URC_INSTALL_CONTEXT@. |
---|
357 | \end{sloppypar} |
---|
358 | |
---|
359 | \item |
---|
360 | @_UA_FORCE_UNWIND@ specifies a forced unwind call. Forced unwind only performs |
---|
361 | the cleanup phase and uses a different means to decide when to stop |
---|
362 | (see \vref{s:ForcedUnwind}). |
---|
363 | \end{enumerate} |
---|
364 | |
---|
365 | The @exception_class@ argument is a copy of the |
---|
366 | \code{C}{exception}'s @exception_class@ field. |
---|
367 | This a number that identifies the exception handling mechanism that created |
---|
368 | the |
---|
369 | |
---|
370 | The \code{C}{exception} argument is a pointer to the user |
---|
371 | provided storage object. It has two public fields: the @exception_class@, |
---|
372 | which is described above, and the @exception_cleanup@ function. |
---|
373 | The clean-up function is used by the EHM to clean-up the exception if it |
---|
374 | should need to be freed at an unusual time, it takes an argument that says |
---|
375 | why it had to be cleaned up. |
---|
376 | |
---|
377 | The @context@ argument is a pointer to an opaque type passed to helper |
---|
378 | functions called inside the personality function. |
---|
379 | |
---|
380 | The return value, @_Unwind_Reason_Code@, is an enumeration of possible messages |
---|
381 | that can be passed several places in libunwind. It includes a number of |
---|
382 | messages for special cases (some of which should never be used by the |
---|
383 | personality function) and error codes. However, unless otherwise noted, the |
---|
384 | personality function should always return @_URC_CONTINUE_UNWIND@. |
---|
385 | |
---|
386 | \subsection{Raise Exception} |
---|
387 | Raising an exception is the central function of libunwind and it performs a |
---|
388 | two-staged unwinding. |
---|
389 | \begin{cfa} |
---|
390 | _Unwind_Reason_Code _Unwind_RaiseException(_Unwind_Exception *); |
---|
391 | \end{cfa} |
---|
392 | First, the function begins the search phase, calling the personality function |
---|
393 | of the most recent stack frame. It continues to call personality functions |
---|
394 | traversing the stack from newest to oldest until a function finds a handler or |
---|
395 | the end of the stack is reached. In the latter case, raise exception returns |
---|
396 | @_URC_END_OF_STACK@. |
---|
397 | |
---|
398 | Second, when a handler is matched, raise exception moves to the clean-up |
---|
399 | phase and walks the stack a second time. |
---|
400 | Once again, it calls the personality functions of each stack frame from newest |
---|
401 | to oldest. This pass stops at the stack frame containing the matching handler. |
---|
402 | If that personality function has not install a handler, it is an error. |
---|
403 | |
---|
404 | If an error is encountered, raise exception returns either |
---|
405 | @_URC_FATAL_PHASE1_ERROR@ or @_URC_FATAL_PHASE2_ERROR@ depending on when the |
---|
406 | error occurred. |
---|
407 | |
---|
408 | \subsection{Forced Unwind} |
---|
409 | \label{s:ForcedUnwind} |
---|
410 | Forced Unwind is the other central function in libunwind. |
---|
411 | \begin{cfa} |
---|
412 | _Unwind_Reason_Code _Unwind_ForcedUnwind(_Unwind_Exception *, |
---|
413 | _Unwind_Stop_Fn, void *); |
---|
414 | \end{cfa} |
---|
415 | It also unwinds the stack but it does not use the search phase. Instead another |
---|
416 | function, the stop function, is used to stop searching. The exception is the |
---|
417 | same as the one passed to raise exception. The extra arguments are the stop |
---|
418 | function and the stop parameter. The stop function has a similar interface as a |
---|
419 | personality function, except it is also passed the stop parameter. |
---|
420 | \begin{lstlisting} |
---|
421 | typedef _Unwind_Reason_Code (*_Unwind_Stop_Fn)( |
---|
422 | _Unwind_Action action, |
---|
423 | _Unwind_Exception_Class exception_class, |
---|
424 | _Unwind_Exception * exception, |
---|
425 | struct _Unwind_Context * context, |
---|
426 | void * stop_parameter); |
---|
427 | \end{lstlisting} |
---|
428 | |
---|
429 | The stop function is called at every stack frame before the personality |
---|
430 | function is called and then once more after all frames of the stack are |
---|
431 | unwound. |
---|
432 | |
---|
433 | Each time it is called, the stop function should return @_URC_NO_REASON@ or |
---|
434 | transfer control directly to other code outside of libunwind. The framework |
---|
435 | does not provide any assistance here. |
---|
436 | |
---|
437 | \begin{sloppypar} |
---|
438 | Its arguments are the same as the paired personality function. The actions |
---|
439 | \snake{_UA_CLEANUP_PHASE} and \snake{_UA_FORCE_UNWIND} are always set when it is |
---|
440 | called. Beyond the libunwind standard, both GCC and Clang add an extra action |
---|
441 | on the last call at the end of the stack: \snake{_UA_END_OF_STACK}. |
---|
442 | \end{sloppypar} |
---|
443 | |
---|
444 | \section{Exception Context} |
---|
445 | % Should I have another independent section? |
---|
446 | % There are only two things in it, top_resume and current_exception. How it is |
---|
447 | % stored changes depending on whether or not the thread-library is linked. |
---|
448 | |
---|
449 | The exception context is global storage used to maintain data across different |
---|
450 | exception operations and to communicate among different components. |
---|
451 | |
---|
452 | Each stack must have its own exception context. In a sequential \CFA program, |
---|
453 | there is only one stack with a single global exception-context. However, when |
---|
454 | the library @libcfathread@ is linked, there are multiple stacks and each |
---|
455 | needs its own exception context. |
---|
456 | |
---|
457 | The exception context should be retrieved by calling the function |
---|
458 | \snake{this_exception_context}. |
---|
459 | For sequential execution, this function is defined as |
---|
460 | a weak symbol in the \CFA system-library, @libcfa@. When a \CFA program is |
---|
461 | concurrent, it links with @libcfathread@, where this function is defined with a |
---|
462 | strong symbol replacing the sequential version. |
---|
463 | |
---|
464 | The sequential @this_exception_context@ returns a hard-coded pointer to the |
---|
465 | global exception context. |
---|
466 | The concurrent version adds the exception context to the data stored at the |
---|
467 | base of each stack. When @this_exception_context@ is called, it retrieves the |
---|
468 | active stack and returns the address of the context saved there. |
---|
469 | |
---|
470 | \section{Termination} |
---|
471 | % Memory management & extra information, the custom function used to implement |
---|
472 | % catches. Talk about GCC nested functions. |
---|
473 | |
---|
474 | \CFA termination exceptions use libunwind heavily because they match \Cpp |
---|
475 | \Cpp exceptions closely. The main complication for \CFA is that the |
---|
476 | compiler generates C code, making it very difficult to generate the assembly to |
---|
477 | form the LSDA for try blocks or destructors. |
---|
478 | |
---|
479 | \subsection{Memory Management} |
---|
480 | The first step of a termination raise is to copy the exception into memory |
---|
481 | managed by the exception system. Currently, the system uses @malloc@, rather |
---|
482 | than reserved memory or the stack top. The exception handling mechanism manages |
---|
483 | memory for the exception as well as memory for libunwind and the system's own |
---|
484 | per-exception storage. |
---|
485 | |
---|
486 | \begin{figure} |
---|
487 | \input{exception-layout} |
---|
488 | \caption{Exception Layout} |
---|
489 | \label{f:ExceptionLayout} |
---|
490 | \end{figure} |
---|
491 | \todo*{Convert the exception layout to an actual diagram.} |
---|
492 | |
---|
493 | Exceptions are stored in variable-sized blocks (see \vref{f:ExceptionLayout}). |
---|
494 | The first component is a fixed-sized data structure that contains the |
---|
495 | information for libunwind and the exception system. The second component is an |
---|
496 | area of memory big enough to store the exception. Macros with pointer arthritic |
---|
497 | and type cast are used to move between the components or go from the embedded |
---|
498 | @_Unwind_Exception@ to the entire node. |
---|
499 | |
---|
500 | Multipe exceptions can exist at the same time because exceptions can be |
---|
501 | raised inside handlers, destructors and finally blocks. |
---|
502 | Figure~\vref{f:MultipleExceptions} shows a program that has multiple |
---|
503 | exceptions active at one time. |
---|
504 | Each time an exception is thrown and caught the stack unwinds and the finally |
---|
505 | clause runs. This will throw another exception (until @num_exceptions@ gets |
---|
506 | high enough) which must be allocated. The previous exceptions may not be |
---|
507 | freed because the handler/catch clause has not been run. |
---|
508 | So the EHM must keep them alive while it allocates exceptions for new throws. |
---|
509 | |
---|
510 | \begin{figure} |
---|
511 | \centering |
---|
512 | \newsavebox{\codeBox} |
---|
513 | \newsavebox{\stackBox} |
---|
514 | \begin{lrbox}{\codeBox} |
---|
515 | \begin{lstlisting}[language=CFA,{moredelim=**[is][\color{red}]{@}{@}}] |
---|
516 | unsigned num_exceptions = 0; |
---|
517 | void throws() { |
---|
518 | try { |
---|
519 | try { |
---|
520 | ++num_exceptions; |
---|
521 | throw (Example){table}; |
---|
522 | } finally { |
---|
523 | if (num_exceptions < 3) { |
---|
524 | throws(); |
---|
525 | } |
---|
526 | } |
---|
527 | } catch (exception_t *) { |
---|
528 | --num_exceptions; |
---|
529 | } |
---|
530 | } |
---|
531 | int main() { |
---|
532 | throws(); |
---|
533 | } |
---|
534 | \end{lstlisting} |
---|
535 | \end{lrbox} |
---|
536 | |
---|
537 | \begin{lrbox}{\stackBox} |
---|
538 | \begin{lstlisting} |
---|
539 | | try-finally |
---|
540 | | try-catch (Example) |
---|
541 | throws() |
---|
542 | | try-finally |
---|
543 | | try-catch (Example) |
---|
544 | throws() |
---|
545 | | try-finally |
---|
546 | | try-catch (Example) |
---|
547 | throws() |
---|
548 | main() |
---|
549 | \end{lstlisting} |
---|
550 | \end{lrbox} |
---|
551 | |
---|
552 | {\usebox\codeBox} |
---|
553 | \hspace{25pt} |
---|
554 | {\usebox\stackBox} |
---|
555 | |
---|
556 | \caption{Multiple Exceptions} |
---|
557 | \label{f:MultipleExceptions} |
---|
558 | \end{figure} |
---|
559 | \todo*{Work on multiple exceptions code sample.} |
---|
560 | |
---|
561 | All exceptions are stored in nodes which are then linked together in lists, |
---|
562 | one list per stack, with the |
---|
563 | list head stored in the exception context. Within each linked list, the most |
---|
564 | recently thrown exception is at the head followed by older thrown |
---|
565 | exceptions. This format allows exceptions to be thrown, while a different |
---|
566 | exception is being handled. The exception at the head of the list is currently |
---|
567 | being handled, while other exceptions wait for the exceptions before them to be |
---|
568 | removed. |
---|
569 | |
---|
570 | The virtual members in the exception's virtual table provide the size of the |
---|
571 | exception, the copy function, and the free function, so they are specific to an |
---|
572 | exception type. The size and copy function are used immediately to copy an |
---|
573 | exception into managed memory. After the exception is handled, the free |
---|
574 | function is used to clean up the exception and then the entire node is |
---|
575 | passed to free so the memory can be given back to the heap. |
---|
576 | |
---|
577 | \subsection{Try Statements and Catch Clauses} |
---|
578 | The try statement with termination handlers is complex because it must |
---|
579 | compensate for the lack of assembly-code generated from \CFA. Libunwind |
---|
580 | requires an LSDA and personality function for control to unwind across a |
---|
581 | function. The LSDA in particular is hard to mimic in generated C code. |
---|
582 | |
---|
583 | The workaround is a function called @__cfaehm_try_terminate@ in the standard |
---|
584 | library. The contents of a try block and the termination handlers are converted |
---|
585 | into functions. These are then passed to the try terminate function and it |
---|
586 | calls them. |
---|
587 | Because this function is known and fixed (and not an arbitrary function that |
---|
588 | happens to contain a try statement), the LSDA can be generated ahead |
---|
589 | of time. |
---|
590 | |
---|
591 | Both the LSDA and the personality function are set ahead of time using |
---|
592 | embedded assembly. This assembly code is handcrafted using C @asm@ statements |
---|
593 | and contains |
---|
594 | enough information for the single try statement the function repersents. |
---|
595 | |
---|
596 | The three functions passed to try terminate are: |
---|
597 | \begin{description} |
---|
598 | \item[try function:] This function is the try block, all the code inside the |
---|
599 | try block is placed inside the try function. It takes no parameters and has no |
---|
600 | return value. This function is called during regular execution to run the try |
---|
601 | block. |
---|
602 | |
---|
603 | \item[match function:] This function is called during the search phase and |
---|
604 | decides if a catch clause matches the termination exception. It is constructed |
---|
605 | from the conditional part of each handler and runs each check, top to bottom, |
---|
606 | in turn, first checking to see if the exception type matches and then if the |
---|
607 | condition is true. It takes a pointer to the exception and returns 0 if the |
---|
608 | exception is not handled here. Otherwise the return value is the id of the |
---|
609 | handler that matches the exception. |
---|
610 | |
---|
611 | \item[handler function:] This function handles the exception. It takes a |
---|
612 | pointer to the exception and the handler's id and returns nothing. It is called |
---|
613 | after the cleanup phase. It is constructed by stitching together the bodies of |
---|
614 | each handler and dispatches to the selected handler. |
---|
615 | \end{description} |
---|
616 | All three functions are created with GCC nested functions. GCC nested functions |
---|
617 | can be used to create closures, functions that can refer to the state of other |
---|
618 | functions on the stack. This approach allows the functions to refer to all the |
---|
619 | variables in scope for the function containing the @try@ statement. These |
---|
620 | nested functions and all other functions besides @__cfaehm_try_terminate@ in |
---|
621 | \CFA use the GCC personality function and the @-fexceptions@ flag to generate |
---|
622 | the LSDA. |
---|
623 | Using this pattern, \CFA implements destructors with the cleanup attribute. |
---|
624 | |
---|
625 | \begin{figure} |
---|
626 | \begin{cfa} |
---|
627 | try { |
---|
628 | // TRY BLOCK |
---|
629 | } catch (Exception1 * name1 ; check(name1)) { |
---|
630 | // CATCH BLOCK 1 |
---|
631 | } catch (Exception2 * name2) { |
---|
632 | // CATCH BLOCK 2 |
---|
633 | } |
---|
634 | \end{cfa} |
---|
635 | |
---|
636 | \begin{cfa} |
---|
637 | void try(void) { |
---|
638 | // TRY BLOCK |
---|
639 | } |
---|
640 | int match(exception_t * __exception_inst) { |
---|
641 | { |
---|
642 | Exception1 * name1; |
---|
643 | if (name1 = (virtual Exception1 *)__exception_inst |
---|
644 | && check(name1)) { |
---|
645 | return 1; |
---|
646 | } |
---|
647 | } |
---|
648 | { |
---|
649 | Exception2 * name2; |
---|
650 | if (name2 = (virtual Exception2 *)__exception_inst) { |
---|
651 | return 2; |
---|
652 | } |
---|
653 | } |
---|
654 | return 0; |
---|
655 | } |
---|
656 | void catch(exception_t * __exception_inst, int __handler_index) { |
---|
657 | switch (__handler_index) { |
---|
658 | case 1: |
---|
659 | { |
---|
660 | Exception1 * name1 = (virtual Exception1 *)__exception_inst; |
---|
661 | // CATCH BLOCK 1 |
---|
662 | } |
---|
663 | return; |
---|
664 | case 2: |
---|
665 | { |
---|
666 | Exception2 * name2 = (virtual Exception2 *)__exception_inst; |
---|
667 | // CATCH BLOCK 2 |
---|
668 | } |
---|
669 | return; |
---|
670 | } |
---|
671 | } |
---|
672 | { |
---|
673 | __cfaehm_try_terminate(try, catch, match); |
---|
674 | } |
---|
675 | \end{cfa} |
---|
676 | |
---|
677 | \caption{Termination Transformation} |
---|
678 | \label{f:TerminationTransformation} |
---|
679 | \todo*{Improve (compress?) Termination Transformations.} |
---|
680 | \end{figure} |
---|
681 | |
---|
682 | \section{Resumption} |
---|
683 | % The stack-local data, the linked list of nodes. |
---|
684 | |
---|
685 | Resumption simpler to implement than termination |
---|
686 | because there is no stack unwinding. |
---|
687 | Instead of storing the data in a special area using assembly, |
---|
688 | there is just a linked list of possible handlers for each stack, |
---|
689 | with each node on the list reperenting a try statement on the stack. |
---|
690 | |
---|
691 | The head of the list is stored in the exception context. |
---|
692 | The nodes are stored in order, with the more recent try statements closer |
---|
693 | to the head of the list. |
---|
694 | Instead of traversing the stack resumption handling traverses the list. |
---|
695 | At each node the EHM checks to see if the try statement the node repersents |
---|
696 | can handle the exception. If it can, then the exception is handled and |
---|
697 | the operation finishes, otherwise the search continues to the next node. |
---|
698 | If the search reaches the end of the list without finding a try statement |
---|
699 | that can handle the exception the default handler is executed and the |
---|
700 | operation finishes. |
---|
701 | |
---|
702 | In each node is a handler function which does most of the work there. |
---|
703 | The handler function is passed the raised the exception and returns true |
---|
704 | if the exception is handled and false if it cannot be handled here. |
---|
705 | |
---|
706 | For each @catchResume@ clause the handler function will: |
---|
707 | check to see if the raised exception is a descendant type of the declared |
---|
708 | exception type, if it is and there is a conditional expression then it will |
---|
709 | run the test, if both checks pass the handling code for the clause is run |
---|
710 | and the function returns true, otherwise it moves onto the next clause. |
---|
711 | If this is the last @catchResume@ clause then instead of moving onto |
---|
712 | the next clause the function returns false as no handler could be found. |
---|
713 | |
---|
714 | \begin{figure} |
---|
715 | \begin{cfa} |
---|
716 | try { |
---|
717 | // TRY BLOCK |
---|
718 | } catchResume (Exception1 * name1 ; check(name1)) { |
---|
719 | // CATCH BLOCK 1 |
---|
720 | } catchResume (Exception2 * name2) { |
---|
721 | // CATCH BLOCK 2 |
---|
722 | } |
---|
723 | \end{cfa} |
---|
724 | |
---|
725 | \begin{cfa} |
---|
726 | bool handle(exception_t * __exception_inst) { |
---|
727 | { |
---|
728 | Exception1 * name1; |
---|
729 | if (name1 = (virtual Exception1 *)__exception_inst |
---|
730 | && check(name1)) { |
---|
731 | // CATCH BLOCK 1 |
---|
732 | return 1; |
---|
733 | } |
---|
734 | } |
---|
735 | { |
---|
736 | Exception2 * name2; |
---|
737 | if (name2 = (virtual Exception2 *)__exception_inst) { |
---|
738 | // CATCH BLOCK 2 |
---|
739 | return 2; |
---|
740 | } |
---|
741 | } |
---|
742 | return false; |
---|
743 | } |
---|
744 | struct __try_resume_node __resume_node |
---|
745 | __attribute__((cleanup( __cfaehm_try_resume_cleanup ))); |
---|
746 | __cfaehm_try_resume_setup( &__resume_node, handler ); |
---|
747 | \end{cfa} |
---|
748 | |
---|
749 | \caption{Resumption Transformation} |
---|
750 | \label{f:ResumptionTransformation} |
---|
751 | \todo*{Improve (compress?) Resumption Transformations.} |
---|
752 | \end{figure} |
---|
753 | |
---|
754 | % Recursive Resumption Stuff: |
---|
755 | Search skipping (see \vpageref{s:ResumptionMarking}), which ignores parts of |
---|
756 | the stack |
---|
757 | already examined, is accomplished by updating the front of the list as the |
---|
758 | search continues. Before the handler at a node is called, the head of the list |
---|
759 | is updated to the next node of the current node. After the search is complete, |
---|
760 | successful or not, the head of the list is reset. |
---|
761 | |
---|
762 | This mechanism means the current handler and every handler that has already |
---|
763 | been checked are not on the list while a handler is run. If a resumption is |
---|
764 | thrown during the handling of another resumption the active handlers and all |
---|
765 | the other handler checked up to this point are not checked again. |
---|
766 | |
---|
767 | This structure also supports new handler added while the resumption is being |
---|
768 | handled. These are added to the front of the list, pointing back along the |
---|
769 | stack -- the first one points over all the checked handlers -- and the ordering |
---|
770 | is maintained. |
---|
771 | |
---|
772 | \begin{figure} |
---|
773 | \input{resumption-marking} |
---|
774 | \caption{Resumption Marking} |
---|
775 | \label{f:ResumptionMarking} |
---|
776 | \todo*{Convert Resumption Marking into a line figure.} |
---|
777 | \end{figure} |
---|
778 | |
---|
779 | \label{p:zero-cost} |
---|
780 | Note, the resumption implementation has a cost for entering/exiting a @try@ |
---|
781 | statement with @catchResume@ clauses, whereas a @try@ statement with @catch@ |
---|
782 | clauses has zero-cost entry/exit. While resumption does not need the stack |
---|
783 | unwinding and cleanup provided by libunwind, it could use the search phase to |
---|
784 | providing zero-cost enter/exit using the LSDA. Unfortunately, there is no way |
---|
785 | to return from a libunwind search without installing a handler or raising an |
---|
786 | error. Although workarounds might be possible, they are beyond the scope of |
---|
787 | this thesis. The current resumption implementation has simplicity in its |
---|
788 | favour. |
---|
789 | % Seriously, just compare the size of the two chapters and then consider |
---|
790 | % that unwind is required knowledge for that chapter. |
---|
791 | |
---|
792 | \section{Finally} |
---|
793 | % Uses destructors and GCC nested functions. |
---|
794 | A finally clause is placed into a GCC nested-function with a unique name, |
---|
795 | and no arguments or return values. |
---|
796 | This nested function is then set as the cleanup |
---|
797 | function of an empty object that is declared at the beginning of a block placed |
---|
798 | around the context of the associated @try@ statement. |
---|
799 | |
---|
800 | The rest is handled by GCC. The try block and all handlers are inside this |
---|
801 | block. At completion, control exits the block and the empty object is cleaned |
---|
802 | up, which runs the function that contains the finally code. |
---|
803 | |
---|
804 | \section{Cancellation} |
---|
805 | % Stack selections, the three internal unwind functions. |
---|
806 | |
---|
807 | Cancellation also uses libunwind to do its stack traversal and unwinding, |
---|
808 | however it uses a different primary function: @_Unwind_ForcedUnwind@. Details |
---|
809 | of its interface can be found in the Section~\vref{s:ForcedUnwind}. |
---|
810 | |
---|
811 | The first step of cancellation is to find the cancelled stack and its type: |
---|
812 | coroutine or thread. Fortunately, the thread library stores the main thread |
---|
813 | pointer and the current thread pointer, and every thread stores a pointer to |
---|
814 | its main coroutine and the coroutine it is currently executing. |
---|
815 | \todo*{Consider adding a description of how threads are coroutines.} |
---|
816 | |
---|
817 | If a the current thread's main and current coroutines are the same then the |
---|
818 | current stack is a thread stack. Furthermore it is easy to compare the |
---|
819 | current thread to the main thread to see if they are the same. And if this |
---|
820 | is not a thread stack then it must be a coroutine stack. |
---|
821 | |
---|
822 | However, if the threading library is not linked, the sequential execution is on |
---|
823 | the main stack. Hence, the entire check is skipped because the weak-symbol |
---|
824 | function is loaded. Therefore, a main thread cancellation is unconditionally |
---|
825 | performed. |
---|
826 | |
---|
827 | Regardless of how the stack is chosen, the stop function and parameter are |
---|
828 | passed to the forced-unwind function. The general pattern of all three stop |
---|
829 | functions is the same: they continue unwinding until the end of stack and |
---|
830 | then preform their transfer. |
---|
831 | |
---|
832 | For main stack cancellation, the transfer is just a program abort. |
---|
833 | |
---|
834 | For coroutine cancellation, the exception is stored on the coroutine's stack, |
---|
835 | and the coroutine context switches to its last resumer. The rest is handled on |
---|
836 | the backside of the resume, which check if the resumed coroutine is |
---|
837 | cancelled. If cancelled, the exception is retrieved from the resumed coroutine, |
---|
838 | and a @CoroutineCancelled@ exception is constructed and loaded with the |
---|
839 | cancelled exception. It is then resumed as a regular exception with the default |
---|
840 | handler coming from the context of the resumption call. |
---|
841 | |
---|
842 | For thread cancellation, the exception is stored on the thread's main stack and |
---|
843 | then context switched to the scheduler. The rest is handled by the thread |
---|
844 | joiner. When the join is complete, the joiner checks if the joined thread is |
---|
845 | cancelled. If cancelled, the exception is retrieved and the joined thread, and |
---|
846 | a @ThreadCancelled@ exception is constructed and loaded with the cancelled |
---|
847 | exception. The default handler is passed in as a function pointer. If it is |
---|
848 | null (as it is for the auto-generated joins on destructor call), the default is |
---|
849 | used, which is a program abort. |
---|
850 | %; which gives the required handling on implicate join. |
---|