about summary refs log tree commit diff
path: root/src/tools/miri/tests/panic/alloc_error_handler_hook.rs
blob: a1eadb45fd13bcfc49c17e4f1cbbacac7ede53b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#![feature(allocator_api, alloc_error_hook)]

use std::alloc::*;

struct Bomb;
impl Drop for Bomb {
    fn drop(&mut self) {
        eprintln!("yes we are unwinding!");
    }
}

#[allow(unreachable_code, unused_variables)]
fn main() {
    // This is a particularly tricky hook, since it unwinds, which the default one does not.
    set_alloc_error_hook(|_layout| panic!("alloc error hook called"));

    let bomb = Bomb;
    handle_alloc_error(Layout::for_value(&0));
    std::mem::forget(bomb); // defuse unwinding bomb
}