]> git.lizzy.rs Git - rust.git/blobdiff - src/trap.rs
Merge commit 'c19edfd71a1d0ddef86c2c67fdb40718d40a72b4' into sync_cg_clif-2022-07-25
[rust.git] / src / trap.rs
index f492bcf4b1ebd1d86d93d79a2427c1f0ef3e19fc..923269c4de9ab8993fcf5e564f0a0a7f2f11dea4 100644 (file)
@@ -1,53 +1,57 @@
+//! Helpers used to print a message and abort in case of certain panics and some detected UB.
+
 use crate::prelude::*;
 
-fn codegen_print(fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>, msg: &str) {
-    let puts = fx.module.declare_function("puts", Linkage::Import, &Signature {
-        call_conv: CallConv::SystemV,
-        params: vec![AbiParam::new(pointer_ty(fx.tcx))],
-        returns: vec![],
-    }).unwrap();
+fn codegen_print(fx: &mut FunctionCx<'_, '_, '_>, msg: &str) {
+    let puts = fx
+        .module
+        .declare_function(
+            "puts",
+            Linkage::Import,
+            &Signature {
+                call_conv: fx.target_config.default_call_conv,
+                params: vec![AbiParam::new(fx.pointer_type)],
+                returns: vec![AbiParam::new(types::I32)],
+            },
+        )
+        .unwrap();
     let puts = fx.module.declare_func_in_func(puts, &mut fx.bcx.func);
+    if fx.clif_comments.enabled() {
+        fx.add_comment(puts, "puts");
+    }
 
-    let symbol_name = fx.tcx.symbol_name(fx.instance);
-    let msg_bytes = format!("trap at {:?} ({}): {}\0", fx.instance, symbol_name, msg).into_bytes().into_boxed_slice();
-    let mut data_ctx = DataContext::new();
-    data_ctx.define(msg_bytes);
-    let msg_id = fx.module.declare_data(&(symbol_name.as_str().to_string() + msg), Linkage::Local, false).unwrap();
-
-    // Ignore DuplicateDefinition error, as the data will be the same
-    let _ = fx.module.define_data(msg_id, &data_ctx);
-
-    let local_msg_id = fx.module.declare_data_in_func(msg_id, fx.bcx.func);
-    let msg_ptr = fx.bcx.ins().global_value(pointer_ty(fx.tcx), local_msg_id);
+    let real_msg = format!("trap at {:?} ({}): {}\0", fx.instance, fx.symbol_name, msg);
+    let msg_ptr = fx.anonymous_str(&real_msg);
     fx.bcx.ins().call(puts, &[msg_ptr]);
 }
 
-/// Trap code: user0
-pub fn trap_panic(fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>, msg: impl AsRef<str>) {
-    codegen_print(fx, msg.as_ref());
-    fx.bcx.ins().trap(TrapCode::User(0));
-}
-
+/// Use this for example when a function call should never return. This will fill the current block,
+/// so you can **not** add instructions to it afterwards.
+///
 /// Trap code: user65535
-pub fn trap_unreachable(fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>, msg: impl AsRef<str>) {
+pub(crate) fn trap_unreachable(fx: &mut FunctionCx<'_, '_, '_>, msg: impl AsRef<str>) {
     codegen_print(fx, msg.as_ref());
-    fx.bcx.ins().trap(TrapCode::User(!0));
+    fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
 }
-
+/// Use this when something is unimplemented, but `libcore` or `libstd` requires it to codegen.
+/// Unlike `trap_unreachable` this will not fill the current block, so you **must** add instructions
+/// to it afterwards.
+///
 /// Trap code: user65535
-pub fn trap_unreachable_ret_value<'tcx>(fx: &mut FunctionCx<'_, 'tcx, impl cranelift_module::Backend>, dest_layout: TyLayout<'tcx>, msg: impl AsRef<str>) -> CValue<'tcx> {
+pub(crate) fn trap_unimplemented(fx: &mut FunctionCx<'_, '_, '_>, msg: impl AsRef<str>) {
     codegen_print(fx, msg.as_ref());
     let true_ = fx.bcx.ins().iconst(types::I32, 1);
     fx.bcx.ins().trapnz(true_, TrapCode::User(!0));
-    let zero = fx.bcx.ins().iconst(fx.pointer_type, 0);
-    CValue::ByRef(zero, dest_layout)
 }
 
+/// Like `trap_unimplemented` but returns a fake value of the specified type.
+///
 /// Trap code: user65535
-pub fn trap_unreachable_ret_place<'tcx>(fx: &mut FunctionCx<'_, 'tcx, impl cranelift_module::Backend>, dest_layout: TyLayout<'tcx>, msg: impl AsRef<str>) -> CPlace<'tcx> {
-    codegen_print(fx, msg.as_ref());
-    let true_ = fx.bcx.ins().iconst(types::I32, 1);
-    fx.bcx.ins().trapnz(true_, TrapCode::User(!0));
-    let zero = fx.bcx.ins().iconst(fx.pointer_type, 0);
-    CPlace::Addr(zero, None, dest_layout)
+pub(crate) fn trap_unimplemented_ret_value<'tcx>(
+    fx: &mut FunctionCx<'_, '_, 'tcx>,
+    dest_layout: TyAndLayout<'tcx>,
+    msg: impl AsRef<str>,
+) -> CValue<'tcx> {
+    trap_unimplemented(fx, msg);
+    CValue::by_ref(Pointer::const_addr(fx, 0), dest_layout)
 }