]> git.lizzy.rs Git - rust.git/blob - src/trap.rs
Merge pull request #1093 from bjorn3/use_new_module_interface
[rust.git] / src / trap.rs
1 //! Helpers used to print a message and abort in case of certain panics and some detected UB.
2
3 use crate::prelude::*;
4
5 fn codegen_print(fx: &mut FunctionCx<'_, '_, impl Module>, msg: &str) {
6     let puts = fx
7         .cx
8         .module
9         .declare_function(
10             "puts",
11             Linkage::Import,
12             &Signature {
13                 call_conv: CallConv::triple_default(fx.triple()),
14                 params: vec![AbiParam::new(pointer_ty(fx.tcx))],
15                 returns: vec![AbiParam::new(types::I32)],
16             },
17         )
18         .unwrap();
19     let puts = fx.cx.module.declare_func_in_func(puts, &mut fx.bcx.func);
20     #[cfg(debug_assertions)]
21     {
22         fx.add_comment(puts, "puts");
23     }
24
25     let symbol_name = fx.tcx.symbol_name(fx.instance);
26     let real_msg = format!("trap at {:?} ({}): {}\0", fx.instance, symbol_name, msg);
27     let msg_ptr = fx.anonymous_str("trap", &real_msg);
28     fx.bcx.ins().call(puts, &[msg_ptr]);
29 }
30
31 /// Trap code: user1
32 pub(crate) fn trap_abort(
33     fx: &mut FunctionCx<'_, '_, impl Module>,
34     msg: impl AsRef<str>,
35 ) {
36     codegen_print(fx, msg.as_ref());
37     fx.bcx.ins().trap(TrapCode::User(1));
38 }
39
40 /// Use this for example when a function call should never return. This will fill the current block,
41 /// so you can **not** add instructions to it afterwards.
42 ///
43 /// Trap code: user65535
44 pub(crate) fn trap_unreachable(
45     fx: &mut FunctionCx<'_, '_, impl Module>,
46     msg: impl AsRef<str>,
47 ) {
48     codegen_print(fx, msg.as_ref());
49     fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
50 }
51
52 /// Like `trap_unreachable` but returns a fake value of the specified type.
53 ///
54 /// Trap code: user65535
55 pub(crate) fn trap_unreachable_ret_value<'tcx>(
56     fx: &mut FunctionCx<'_, 'tcx, impl Module>,
57     dest_layout: TyAndLayout<'tcx>,
58     msg: impl AsRef<str>,
59 ) -> CValue<'tcx> {
60     codegen_print(fx, msg.as_ref());
61     let true_ = fx.bcx.ins().iconst(types::I32, 1);
62     fx.bcx.ins().trapnz(true_, TrapCode::UnreachableCodeReached);
63     CValue::by_ref(Pointer::const_addr(fx, 0), dest_layout)
64 }
65
66 /// Use this when something is unimplemented, but `libcore` or `libstd` requires it to codegen.
67 /// Unlike `trap_unreachable` this will not fill the current block, so you **must** add instructions
68 /// to it afterwards.
69 ///
70 /// Trap code: user65535
71 pub(crate) fn trap_unimplemented(
72     fx: &mut FunctionCx<'_, '_, impl Module>,
73     msg: impl AsRef<str>,
74 ) {
75     codegen_print(fx, msg.as_ref());
76     let true_ = fx.bcx.ins().iconst(types::I32, 1);
77     fx.bcx.ins().trapnz(true_, TrapCode::User(!0));
78 }
79
80 /// Like `trap_unimplemented` but returns a fake value of the specified type.
81 ///
82 /// Trap code: user65535
83 pub(crate) fn trap_unimplemented_ret_value<'tcx>(
84     fx: &mut FunctionCx<'_, 'tcx, impl Module>,
85     dest_layout: TyAndLayout<'tcx>,
86     msg: impl AsRef<str>,
87 ) -> CValue<'tcx> {
88     trap_unimplemented(fx, msg);
89     CValue::by_ref(Pointer::const_addr(fx, 0), dest_layout)
90 }