2014-05-01 10:47:18 -07:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-10-09 15:17:22 -04:00
|
|
|
//! Panic support for libcore
|
2014-05-10 13:46:05 -07:00
|
|
|
//!
|
2014-10-09 15:17:22 -04:00
|
|
|
//! The core library cannot define panicking, but it does *declare* panicking. This
|
|
|
|
//! means that the functions inside of libcore are allowed to panic, but to be
|
|
|
|
//! useful an upstream crate must define panicking for libcore to use. The current
|
|
|
|
//! interface for panicking is:
|
2014-05-10 13:46:05 -07:00
|
|
|
//!
|
2014-06-18 01:04:35 -07:00
|
|
|
//! ```ignore
|
2014-10-09 15:17:22 -04:00
|
|
|
//! fn panic_impl(fmt: &fmt::Arguments, &(&'static str, uint)) -> !;
|
2014-06-18 01:04:35 -07:00
|
|
|
//! ```
|
2014-05-10 13:46:05 -07:00
|
|
|
//!
|
2014-10-09 15:17:22 -04:00
|
|
|
//! This definition allows for panicking with any general message, but it does not
|
|
|
|
//! allow for failing with a `Box<Any>` value. The reason for this is that libcore
|
2014-05-10 13:46:05 -07:00
|
|
|
//! is not allowed to allocate.
|
|
|
|
//!
|
2014-10-09 15:17:22 -04:00
|
|
|
//! This module contains a few other panicking functions, but these are just the
|
|
|
|
//! necessary lang items for the compiler. All panics are funneled through this
|
2014-05-10 13:46:05 -07:00
|
|
|
//! one function. Currently, the actual symbol is declared in the standard
|
|
|
|
//! library, but the location of this may change over time.
|
2014-05-01 10:47:18 -07:00
|
|
|
|
2014-05-12 21:23:13 -07:00
|
|
|
#![allow(dead_code, missing_doc)]
|
2014-05-01 10:47:18 -07:00
|
|
|
|
2014-05-10 13:46:05 -07:00
|
|
|
use fmt;
|
2014-06-28 13:57:36 -07:00
|
|
|
use intrinsics;
|
2014-05-26 10:33:18 -07:00
|
|
|
|
2014-10-28 14:07:33 -04:00
|
|
|
// NOTE(stage0): remove after a snapshot
|
|
|
|
#[cfg(stage0)]
|
|
|
|
#[cold] #[inline(never)] // this is the slow path, always
|
|
|
|
#[lang="fail"]
|
|
|
|
pub fn panic(expr_file_line: &(&'static str, &'static str, uint)) -> ! {
|
|
|
|
let (expr, file, line) = *expr_file_line;
|
|
|
|
let ref file_line = (file, line);
|
|
|
|
format_args!(|args| -> () {
|
|
|
|
panic_fmt(args, file_line);
|
|
|
|
}, "{}", expr);
|
|
|
|
|
|
|
|
unsafe { intrinsics::abort() }
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE(stage0): remove after a snapshot
|
|
|
|
#[cfg(stage0)]
|
|
|
|
#[cold] #[inline(never)]
|
|
|
|
#[lang="fail_bounds_check"]
|
|
|
|
fn panic_bounds_check(file_line: &(&'static str, uint),
|
|
|
|
index: uint, len: uint) -> ! {
|
|
|
|
format_args!(|args| -> () {
|
|
|
|
panic_fmt(args, file_line);
|
|
|
|
}, "index out of bounds: the len is {} but the index is {}", len, index);
|
|
|
|
unsafe { intrinsics::abort() }
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE(stage0): remove after a snapshot
|
|
|
|
#[cfg(stage0)]
|
|
|
|
#[cold] #[inline(never)]
|
|
|
|
pub fn panic_fmt(fmt: &fmt::Arguments, file_line: &(&'static str, uint)) -> ! {
|
|
|
|
#[allow(ctypes)]
|
|
|
|
extern {
|
|
|
|
#[lang = "fail_fmt"]
|
|
|
|
fn panic_impl(fmt: &fmt::Arguments, file: &'static str,
|
|
|
|
line: uint) -> !;
|
|
|
|
|
|
|
|
}
|
|
|
|
let (file, line) = *file_line;
|
|
|
|
unsafe { panic_impl(fmt, file, line) }
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE(stage0): remove cfg after a snapshot
|
|
|
|
#[cfg(not(stage0))]
|
2014-09-21 23:35:43 +02:00
|
|
|
#[cold] #[inline(never)] // this is the slow path, always
|
2014-10-09 15:17:22 -04:00
|
|
|
#[lang="panic"]
|
|
|
|
pub fn panic(expr_file_line: &(&'static str, &'static str, uint)) -> ! {
|
2014-09-21 23:35:43 +02:00
|
|
|
let (expr, file, line) = *expr_file_line;
|
|
|
|
let ref file_line = (file, line);
|
|
|
|
format_args!(|args| -> () {
|
2014-10-09 15:17:22 -04:00
|
|
|
panic_fmt(args, file_line);
|
2014-09-21 23:35:43 +02:00
|
|
|
}, "{}", expr);
|
|
|
|
|
|
|
|
unsafe { intrinsics::abort() }
|
|
|
|
}
|
|
|
|
|
2014-10-28 14:07:33 -04:00
|
|
|
// NOTE(stage0): remove cfg after a snapshot
|
|
|
|
#[cfg(not(stage0))]
|
2014-07-29 23:18:31 -07:00
|
|
|
#[cold] #[inline(never)]
|
2014-10-09 15:17:22 -04:00
|
|
|
#[lang="panic_bounds_check"]
|
|
|
|
fn panic_bounds_check(file_line: &(&'static str, uint),
|
Modify failure lang items to take less pointers.
Divide-by-zero before:
```
leaq "str\"str\"(1762)"(%rip), %rax
movq %rax, 16(%rsp)
movq $27, 24(%rsp)
leaq "str\"str\"(1542)"(%rip), %rax
movq %rax, (%rsp)
movq $19, 8(%rsp)
leaq 16(%rsp), %rdi
leaq (%rsp), %rsi
movl $32, %edx
callq _ZN7failure5fail_20hc04408f955ce60aaqWjE@PLT
```
After:
```
leaq .Lconst(%rip), %rdi
callq _ZN7failure5fail_20haf918a97c8f7f2bfqWjE@PLT
```
Bounds check before:
```
leaq "str\"str\"(1542)"(%rip), %rax
movq %rax, 8(%rsp)
movq $19, 16(%rsp)
leaq 8(%rsp), %rdi
movl $38, %esi
movl $1, %edx
movl $1, %ecx
callq _ZN7failure17fail_bounds_check20hf4bc3c69e96caf41RXjE@PLT
```
Bounds check after:
```
leaq .Lconst2(%rip), %rdi
movl $1, %esi
movl $1, %edx
callq _ZN7failure17fail_bounds_check20h5267276a537a7de22XjE@PLT
```
Size before:
21277995 librustc-4e7c5e5c.s
```
text data
12554881 6089335
```
Size after:
21247617 librustc-4e7c5e5c.so
```
text data
12518497 6095748
```
2014-07-29 16:40:59 -07:00
|
|
|
index: uint, len: uint) -> ! {
|
|
|
|
format_args!(|args| -> () {
|
2014-10-09 15:17:22 -04:00
|
|
|
panic_fmt(args, file_line);
|
Modify failure lang items to take less pointers.
Divide-by-zero before:
```
leaq "str\"str\"(1762)"(%rip), %rax
movq %rax, 16(%rsp)
movq $27, 24(%rsp)
leaq "str\"str\"(1542)"(%rip), %rax
movq %rax, (%rsp)
movq $19, 8(%rsp)
leaq 16(%rsp), %rdi
leaq (%rsp), %rsi
movl $32, %edx
callq _ZN7failure5fail_20hc04408f955ce60aaqWjE@PLT
```
After:
```
leaq .Lconst(%rip), %rdi
callq _ZN7failure5fail_20haf918a97c8f7f2bfqWjE@PLT
```
Bounds check before:
```
leaq "str\"str\"(1542)"(%rip), %rax
movq %rax, 8(%rsp)
movq $19, 16(%rsp)
leaq 8(%rsp), %rdi
movl $38, %esi
movl $1, %edx
movl $1, %ecx
callq _ZN7failure17fail_bounds_check20hf4bc3c69e96caf41RXjE@PLT
```
Bounds check after:
```
leaq .Lconst2(%rip), %rdi
movl $1, %esi
movl $1, %edx
callq _ZN7failure17fail_bounds_check20h5267276a537a7de22XjE@PLT
```
Size before:
21277995 librustc-4e7c5e5c.s
```
text data
12554881 6089335
```
Size after:
21247617 librustc-4e7c5e5c.so
```
text data
12518497 6095748
```
2014-07-29 16:40:59 -07:00
|
|
|
}, "index out of bounds: the len is {} but the index is {}", len, index);
|
|
|
|
unsafe { intrinsics::abort() }
|
|
|
|
}
|
|
|
|
|
2014-10-28 14:07:33 -04:00
|
|
|
// NOTE(stage0): remove cfg after a snapshot
|
|
|
|
#[cfg(not(stage0))]
|
2014-07-29 23:18:31 -07:00
|
|
|
#[cold] #[inline(never)]
|
2014-10-09 15:17:22 -04:00
|
|
|
pub fn panic_fmt(fmt: &fmt::Arguments, file_line: &(&'static str, uint)) -> ! {
|
2014-05-01 10:47:18 -07:00
|
|
|
#[allow(ctypes)]
|
2014-05-19 09:30:09 -07:00
|
|
|
extern {
|
2014-10-09 15:17:22 -04:00
|
|
|
#[lang = "panic_fmt"]
|
|
|
|
fn panic_impl(fmt: &fmt::Arguments, file: &'static str,
|
2014-09-21 23:11:33 +02:00
|
|
|
line: uint) -> !;
|
|
|
|
|
2014-05-19 09:30:09 -07:00
|
|
|
}
|
2014-07-24 21:33:46 -07:00
|
|
|
let (file, line) = *file_line;
|
2014-10-09 15:17:22 -04:00
|
|
|
unsafe { panic_impl(fmt, file, line) }
|
2014-07-24 21:33:46 -07:00
|
|
|
}
|