1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // Implementation of Rust stack unwinding
13 // For background on exception handling and stack unwinding please see
14 // "Exception Handling in LLVM" (llvm.org/docs/ExceptionHandling.html) and
15 // documents linked from it.
16 // These are also good reads:
17 // http://theofilos.cs.columbia.edu/blog/2013/09/22/base_abi/
18 // http://monoinfinito.wordpress.com/series/exception-handling-in-c/
19 // http://www.airs.com/blog/index.php?s=exception+frames
21 // ~~~ A brief summary ~~~
22 // Exception handling happens in two phases: a search phase and a cleanup phase.
24 // In both phases the unwinder walks stack frames from top to bottom using
25 // information from the stack frame unwind sections of the current process's
26 // modules ("module" here refers to an OS module, i.e. an executable or a
29 // For each stack frame, it invokes the associated "personality routine", whose
30 // address is also stored in the unwind info section.
32 // In the search phase, the job of a personality routine is to examine exception
33 // object being thrown, and to decide whether it should be caught at that stack
34 // frame. Once the handler frame has been identified, cleanup phase begins.
36 // In the cleanup phase, personality routines invoke cleanup code associated
37 // with their stack frames (i.e. destructors). Once stack has been unwound down
38 // to the handler frame level, unwinding stops and the last personality routine
39 // transfers control to its' catch block.
41 // ~~~ Frame unwind info registration ~~~
42 // Each module has its' own frame unwind info section (usually ".eh_frame"), and
43 // unwinder needs to know about all of them in order for unwinding to be able to
44 // cross module boundaries.
46 // On some platforms, like Linux, this is achieved by dynamically enumerating
47 // currently loaded modules via the dl_iterate_phdr() API and finding all
48 // .eh_frame sections.
50 // Others, like Windows, require modules to actively register their unwind info
51 // sections by calling __register_frame_info() API at startup. In the latter
52 // case it is essential that there is only one copy of the unwinder runtime in
53 // the process. This is usually achieved by linking to the dynamic version of
54 // the unwind runtime.
56 // Currently Rust uses unwind runtime provided by libgcc.
58 use any::{Any, AnyRefExt};
64 use option::{Some, None, Option};
67 use result::{Err, Ok};
75 use uw = rt::libunwind;
79 priv cause: Option<~Any>
83 pub fn new() -> Unwinder {
90 pub fn unwinding(&self) -> bool {
94 pub fn try(&mut self, f: ||) {
99 let closure: Closure = cast::transmute(f);
100 let ep = rust_try(try_fn, closure.code as *c_void,
101 closure.env as *c_void);
103 rtdebug!("caught {}", (*ep).exception_class);
104 uw::_Unwind_DeleteException(ep);
108 extern fn try_fn(code: *c_void, env: *c_void) {
110 let closure: || = cast::transmute(Closure {
120 // When f(...) returns normally, the return value is null.
121 // When f(...) throws, the return value is a pointer to the caught
123 fn rust_try(f: extern "C" fn(*c_void, *c_void),
125 data: *c_void) -> *uw::_Unwind_Exception;
129 pub fn begin_unwind(&mut self, cause: ~Any) -> ! {
130 rtdebug!("begin_unwind()");
132 self.unwinding = true;
133 self.cause = Some(cause);
137 // An uninlined, unmangled function upon which to slap yer breakpoints
140 fn rust_fail() -> ! {
142 let exception = ~uw::_Unwind_Exception {
143 exception_class: rust_exception_class(),
144 exception_cleanup: exception_cleanup,
145 private: [0, ..uw::unwinder_private_data_size],
147 let error = uw::_Unwind_RaiseException(cast::transmute(exception));
148 rtabort!("Could not unwind stack, error = {}", error as int)
151 extern "C" fn exception_cleanup(_unwind_code: uw::_Unwind_Reason_Code,
152 exception: *uw::_Unwind_Exception) {
153 rtdebug!("exception_cleanup()");
155 let _: ~uw::_Unwind_Exception = cast::transmute(exception);
161 pub fn result(&mut self) -> TaskResult {
163 Err(self.cause.take().unwrap())
170 // Rust's exception class identifier. This is used by personality routines to
171 // determine whether the exception was thrown by their own runtime.
172 fn rust_exception_class() -> uw::_Unwind_Exception_Class {
173 // M O Z \0 R U S T -- vendor, language
177 // We could implement our personality routine in pure Rust, however exception
178 // info decoding is tedious. More importantly, personality routines have to
179 // handle various platform quirks, which are not fun to maintain. For this
180 // reason, we attempt to reuse personality routine of the C language:
181 // __gcc_personality_v0.
183 // Since C does not support exception catching, __gcc_personality_v0 simply
184 // always returns _URC_CONTINUE_UNWIND in search phase, and always returns
185 // _URC_INSTALL_CONTEXT (i.e. "invoke cleanup code") in cleanup phase.
187 // This is pretty close to Rust's exception handling approach, except that Rust
188 // does have a single "catch-all" handler at the bottom of each task's stack.
189 // So we have two versions:
190 // - rust_eh_personality, used by all cleanup landing pads, which never catches,
191 // so the behavior of __gcc_personality_v0 is perfectly adequate there, and
192 // - rust_eh_personality_catch, used only by rust_try(), which always catches.
193 // This is achieved by overriding the return value in search phase to always
196 #[cfg(not(target_arch = "arm"), not(test))]
198 #[allow(visible_private_types)]
200 use uw = rt::libunwind;
204 fn __gcc_personality_v0(version: c_int,
205 actions: uw::_Unwind_Action,
206 exception_class: uw::_Unwind_Exception_Class,
207 ue_header: *uw::_Unwind_Exception,
208 context: *uw::_Unwind_Context)
209 -> uw::_Unwind_Reason_Code;
212 #[lang="eh_personality"]
213 #[no_mangle] // so we can reference it by name from middle/trans/base.rs
214 pub extern "C" fn rust_eh_personality(
216 actions: uw::_Unwind_Action,
217 exception_class: uw::_Unwind_Exception_Class,
218 ue_header: *uw::_Unwind_Exception,
219 context: *uw::_Unwind_Context
220 ) -> uw::_Unwind_Reason_Code
223 __gcc_personality_v0(version, actions, exception_class, ue_header,
228 #[no_mangle] // referenced from rust_try.ll
229 pub extern "C" fn rust_eh_personality_catch(
231 actions: uw::_Unwind_Action,
232 exception_class: uw::_Unwind_Exception_Class,
233 ue_header: *uw::_Unwind_Exception,
234 context: *uw::_Unwind_Context
235 ) -> uw::_Unwind_Reason_Code
237 if (actions as c_int & uw::_UA_SEARCH_PHASE as c_int) != 0 { // search phase
238 uw::_URC_HANDLER_FOUND // catch!
240 else { // cleanup phase
242 __gcc_personality_v0(version, actions, exception_class, ue_header,
249 // ARM EHABI uses a slightly different personality routine signature,
250 // but otherwise works the same.
251 #[cfg(target_arch = "arm", not(test))]
252 #[allow(visible_private_types)]
254 use uw = rt::libunwind;
258 fn __gcc_personality_v0(state: uw::_Unwind_State,
259 ue_header: *uw::_Unwind_Exception,
260 context: *uw::_Unwind_Context)
261 -> uw::_Unwind_Reason_Code;
264 #[lang="eh_personality"]
265 #[no_mangle] // so we can reference it by name from middle/trans/base.rs
266 pub extern "C" fn rust_eh_personality(
267 state: uw::_Unwind_State,
268 ue_header: *uw::_Unwind_Exception,
269 context: *uw::_Unwind_Context
270 ) -> uw::_Unwind_Reason_Code
273 __gcc_personality_v0(state, ue_header, context)
277 #[no_mangle] // referenced from rust_try.ll
278 pub extern "C" fn rust_eh_personality_catch(
279 state: uw::_Unwind_State,
280 ue_header: *uw::_Unwind_Exception,
281 context: *uw::_Unwind_Context
282 ) -> uw::_Unwind_Reason_Code
284 if (state as c_int & uw::_US_ACTION_MASK as c_int)
285 == uw::_US_VIRTUAL_UNWIND_FRAME as c_int { // search phase
286 uw::_URC_HANDLER_FOUND // catch!
288 else { // cleanup phase
290 __gcc_personality_v0(state, ue_header, context)
299 pub fn fail_(expr: *u8, file: *u8, line: uint) -> ! {
300 begin_unwind_raw(expr, file, line);
304 #[lang="fail_bounds_check"]
306 pub fn fail_bounds_check(file: *u8, line: uint, index: uint, len: uint) -> ! {
309 let msg = format!("index out of bounds: the len is {} but the index is {}",
310 len as uint, index as uint);
311 msg.with_c_str(|buf| fail_(buf as *u8, file, line))
314 /// This is the entry point of unwinding for things like lang items and such.
315 /// The arguments are normally generated by the compiler, and need to
316 /// have static lifetimes.
317 #[inline(never)] #[cold] // this is the slow path, please never inline this
318 pub fn begin_unwind_raw(msg: *u8, file: *u8, line: uint) -> ! {
321 fn static_char_ptr(p: *u8) -> &'static str {
322 let s = unsafe { CString::new(p as *c_char, false) };
324 Some(s) => unsafe { cast::transmute::<&str, &'static str>(s) },
325 None => rtabort!("message wasn't utf8?")
329 let msg = static_char_ptr(msg);
330 let file = static_char_ptr(file);
332 begin_unwind(msg, file, line as uint)
335 /// The entry point for unwinding with a formatted message.
337 /// This is designed to reduce the amount of code required at the call
338 /// site as much as possible (so that `fail!()` has as low an impact
339 /// on (e.g.) the inlining of other functions as possible), by moving
340 /// the actual formatting into this shared place.
341 #[inline(never)] #[cold]
342 pub fn begin_unwind_fmt(msg: &fmt::Arguments, file: &'static str, line: uint) -> ! {
343 // We do two allocations here, unfortunately. But (a) they're
344 // required with the current scheme, and (b) we don't handle
345 // failure + OOM properly anyway (see comment in begin_unwind
347 begin_unwind_inner(~fmt::format(msg), file, line)
350 /// This is the entry point of unwinding for fail!() and assert!().
351 #[inline(never)] #[cold] // avoid code bloat at the call sites as much as possible
352 pub fn begin_unwind<M: Any + Send>(msg: M, file: &'static str, line: uint) -> ! {
353 // Note that this should be the only allocation performed in this code path.
354 // Currently this means that fail!() on OOM will invoke this code path,
355 // but then again we're not really ready for failing on OOM anyway. If
356 // we do start doing this, then we should propagate this allocation to
357 // be performed in the parent of this task instead of the task that's
360 // see below for why we do the `Any` coercion here.
361 begin_unwind_inner(~msg, file, line)
365 /// The core of the unwinding.
367 /// This is non-generic to avoid instantiation bloat in other crates
368 /// (which makes compilation of small crates noticably slower). (Note:
369 /// we need the `Any` object anyway, we're not just creating it to
370 /// avoid being generic.)
372 /// Do this split took the LLVM IR line counts of `fn main() { fail!()
373 /// }` from ~1900/3700 (-O/no opts) to 180/590.
374 #[inline(never)] #[cold] // this is the slow path, please never inline this
375 fn begin_unwind_inner(msg: ~Any, file: &'static str, line: uint) -> ! {
378 let msg_s = match msg.as_ref::<&'static str>() {
380 None => match msg.as_ref::<~str>() {
381 Some(s) => s.as_slice(),
386 // It is assumed that all reasonable rust code will have a local task at
387 // all times. This means that this `try_take` will succeed almost all of
388 // the time. There are border cases, however, when the runtime has
389 // *almost* set up the local task, but hasn't quite gotten there yet. In
390 // order to get some better diagnostics, we print on failure and
391 // immediately abort the whole process if there is no local task
393 let opt_task: Option<~Task> = Local::try_take();
394 task = match opt_task {
397 rterrln!("failed at '{}', {}:{}", msg_s, file, line);
398 if backtrace::log_enabled() {
399 let mut err = ::rt::util::Stderr;
400 let _err = backtrace::write(&mut err);
402 unsafe { intrinsics::abort() }
406 // See comments in io::stdio::with_task_stdout as to why we have to be
407 // careful when using an arbitrary I/O handle from the task. We
408 // essentially need to dance to make sure when a task is in TLS when
409 // running user code.
410 let name = task.name.take();
412 let n = name.as_ref().map(|n| n.as_slice()).unwrap_or("<unnamed>");
414 match task.stderr.take() {
415 Some(mut stderr) => {
417 // FIXME: what to do when the task printing fails?
418 let _err = format_args!(|args| ::fmt::writeln(stderr, args),
419 "task '{}' failed at '{}', {}:{}",
420 n, msg_s, file, line);
421 if backtrace::log_enabled() {
422 let _err = backtrace::write(stderr);
424 task = Local::take();
426 match mem::replace(&mut task.stderr, Some(stderr)) {
430 task = Local::take();
436 rterrln!("task '{}' failed at '{}', {}:{}", n, msg_s,
438 if backtrace::log_enabled() {
439 let mut err = ::rt::util::Stderr;
440 let _err = backtrace::write(&mut err);
447 if task.unwinder.unwinding {
448 // If a task fails while it's already unwinding then we
449 // have limited options. Currently our preference is to
450 // just abort. In the future we may consider resuming
451 // unwinding or otherwise exiting the task cleanly.
452 rterrln!("task failed during unwinding (double-failure - total drag!)")
453 rterrln!("rust must abort now. so sorry.");
455 // Don't print the backtrace twice (it would have already been
456 // printed if logging was enabled).
457 if !backtrace::log_enabled() {
458 let mut err = ::rt::util::Stderr;
459 let _err = backtrace::write(&mut err);
461 unsafe { intrinsics::abort() }
465 // The unwinder won't actually use the task at all, so we put the task back
466 // into TLS right before we invoke the unwinder, but this means we need an
467 // unsafe reference back to the unwinder once it's in TLS.
470 let task: *mut Task = Local::unsafe_borrow();
471 (*task).unwinder.begin_unwind(msg);