|
| 1 | +// run-pass |
| 2 | +// ignore-android no libc |
| 3 | +// ignore-cloudabi no libc |
| 4 | +// ignore-emscripten no libc |
| 5 | +// ignore-sgx no libc |
| 6 | +// ignore-wasm32 no libc |
| 7 | +// only-linux |
| 8 | +// compile-flags:-C panic=abort |
| 9 | +// aux-build:helper.rs |
| 10 | + |
| 11 | +#![feature(start, rustc_private, new_uninit, panic_info_message)] |
| 12 | +#![feature(alloc_error_handler)] |
| 13 | +#![no_std] |
| 14 | + |
| 15 | +extern crate alloc; |
| 16 | +extern crate libc; |
| 17 | + |
| 18 | +// ARM targets need these symbols |
| 19 | +#[no_mangle] |
| 20 | +pub fn __aeabi_unwind_cpp_pr0() {} |
| 21 | + |
| 22 | +#[no_mangle] |
| 23 | +pub fn __aeabi_unwind_cpp_pr1() {} |
| 24 | + |
| 25 | +use core::ptr::null_mut; |
| 26 | +use core::alloc::{GlobalAlloc, Layout}; |
| 27 | +use alloc::boxed::Box; |
| 28 | + |
| 29 | +extern crate helper; |
| 30 | + |
| 31 | +struct MyAllocator; |
| 32 | + |
| 33 | +#[alloc_error_handler] |
| 34 | +fn my_oom(layout: Layout) -> ! |
| 35 | +{ |
| 36 | + use alloc::fmt::write; |
| 37 | + unsafe { |
| 38 | + let size = layout.size(); |
| 39 | + let mut s = alloc::string::String::new(); |
| 40 | + write(&mut s, format_args!("My OOM: failed to allocate {} bytes!\n", size)).unwrap(); |
| 41 | + let s = s.as_str(); |
| 42 | + libc::write(libc::STDERR_FILENO, s as *const _ as _, s.len()); |
| 43 | + libc::exit(0) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +unsafe impl GlobalAlloc for MyAllocator { |
| 48 | + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { |
| 49 | + if layout.size() < 4096 { |
| 50 | + libc::malloc(layout.size()) as _ |
| 51 | + } else { |
| 52 | + null_mut() |
| 53 | + } |
| 54 | + } |
| 55 | + unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {} |
| 56 | +} |
| 57 | + |
| 58 | +#[global_allocator] |
| 59 | +static A: MyAllocator = MyAllocator; |
| 60 | + |
| 61 | +#[panic_handler] |
| 62 | +fn panic(panic_info: &core::panic::PanicInfo) -> ! { |
| 63 | + unsafe { |
| 64 | + if let Some(s) = panic_info.payload().downcast_ref::<&str>() { |
| 65 | + const PSTR: &str = "panic occurred: "; |
| 66 | + const CR: &str = "\n"; |
| 67 | + libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len()); |
| 68 | + libc::write(libc::STDERR_FILENO, s as *const _ as _, s.len()); |
| 69 | + libc::write(libc::STDERR_FILENO, CR as *const _ as _, CR.len()); |
| 70 | + } |
| 71 | + if let Some(args) = panic_info.message() { |
| 72 | + let mut s = alloc::string::String::new(); |
| 73 | + alloc::fmt::write(&mut s, *args).unwrap(); |
| 74 | + let s = s.as_str(); |
| 75 | + const PSTR: &str = "panic occurred: "; |
| 76 | + const CR: &str = "\n"; |
| 77 | + libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len()); |
| 78 | + libc::write(libc::STDERR_FILENO, s as *const _ as _, s.len()); |
| 79 | + libc::write(libc::STDERR_FILENO, CR as *const _ as _, CR.len()); |
| 80 | + } else { |
| 81 | + const PSTR: &str = "panic occurred\n"; |
| 82 | + libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len()); |
| 83 | + } |
| 84 | + libc::exit(1) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +#[derive(Debug)] |
| 89 | +struct Page([[u64; 32]; 16]); |
| 90 | + |
| 91 | +#[start] |
| 92 | +pub fn main(_argc: isize, _argv: *const *const u8) -> isize { |
| 93 | + let zero = Box::<Page>::new_zeroed(); |
| 94 | + let zero = unsafe { zero.assume_init() }; |
| 95 | + helper::work_with(&zero); |
| 96 | + 1 |
| 97 | +} |
0 commit comments