2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / docs / exceptions
1              Exception Implementation in the Mono Runtime
2                  Dietmar Maurer (dietmar@ximian.com)
3                         (C) 2001 Ximian, Inc.
4
5 Exception implementation (jit):
6 ===============================
7
8 Stack unwinding:
9 ================
10
11 We record the code address (start_address, size) of all methods. That way it is
12 possible to map an instruction pointer (IP) to the method information needed
13 for unwinding the stack:
14
15 We also save a Last Managed Frame (LMF) structure at each call from managed to
16 unmanaged code. That way we can recover from exceptions inside unmanaged code.
17
18 void handle_exception ((struct sigcontext *ctx, gpointer obj)
19 {
20         if (ctx->bp < mono_end_of_stack) {
21                 /* unhandled exception */
22                 abort ();
23         }
24
25         info = mono_jit_info_table_find (mono_jit_info_table, ctx->ip);
26
27         if (info) { // we are inside managed code
28
29                 if (ch =  find_catch_handler ())
30                         execute_catch_handler (ch, ctx, obj); 
31                 
32                 execute_all_finally_handler ();
33
34                 // restore register, including IP and Frame pointer
35                 ctx = restore_caller_saved_registers_from_ctx (ji, ctx);
36
37                 // continue unwinding
38                 handle_exception (ctx, obj);
39
40         } else {
41
42                 lmf = get_last_managed_frame ();
43                 
44                 // restore register, including IP and Frame pointer
45                 ctx = restore_caller_saved_registers_from_lmf (ji, lmf);
46                 
47                 // continue unwinding
48                 handle_exception (ctx, obj);
49         }
50 }
51
52
53 Code generation:
54 ================
55
56 leave: is simply translated into a branch to the target. If the leave
57 instruction is inside a finally block (but not inside another handler)
58 we call the finally handler before we branch to the target.
59
60 finally/endfinally, filter/endfilter: is translated into subroutine ending with
61 a "return" statement. The subroutine does not save EBP, because we need access
62 to the local variables of the enclosing method. Its is possible that
63 instructions inside those handlers modify the stack pointer, thus we save the
64 stack pointer at the start of the handler, and restore it at the end. We have
65 to use a "call" instruction to execute such finally handlers. This makes it
66 also possible to execute them inside the stack unwinding code. The exception
67 object for filters is passed in a local variable (cfg->exvar).
68
69 throw: we first save all regs into a sigcontext struct and then call the stack
70 unwinding code.
71
72 catch handler: catch hanlders are always called from the stack unwinding
73 code. The exception object is passed in a local variable (cfg->exvar).
74
75 gcc support for Exceptions
76 ==========================
77
78 gcc supports exceptions in files compiled with the -fexception option. gcc
79 generates DWARF exceptions tables in that case, so it is possible to unwind the
80 stack. The method to read those exception tables is contained in libgcc.a, and
81 in newer versions of glibc (glibc 2.2.5 for example), and it is called
82 __frame_state_for(). Another usable glibc function is backtrace_symbols() which
83 returns the function name corresponding to a code address.
84
85 We dynamically check if those features are available using g_module_symbol(),
86 and we use them only when available. If not available we use the LMF as
87 fallback.
88
89 Using gcc exception information prevents us from saving the LMF at each native
90 call, so this is a way to speed up native calls. This is especially valuable
91 for internal calls, because we can make sure that all internal calls are
92 compiled with -fexceptions (we compile the whole mono runtime with that
93 option).
94
95 All native function are able to call function without exception tables, and so
96 we are unable to restore all caller saved registers if an exception is raised
97 in such function. Well, its possible if the previous function already saves all
98 registers. So we only omit the the LMF if a function has an exception table
99 able to restore all caller saved registers.
100
101 One problem is that gcc almost never saves all caller saved registers, because
102 it is just unnecessary in normal situations. But there is a trick forcing gcc
103 to save all register, we just need to call __builtin_unwind_init() at the
104 beginning of a function. That way gcc generates code to save all caller saved
105 register on the stack.
106
107
108
109
110