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