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