Skip to content

Commit c0ffc42

Browse files
committed
Print message when reaching trap
1 parent 970d164 commit c0ffc42

File tree

5 files changed

+78
-22
lines changed

5 files changed

+78
-22
lines changed

src/abi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ pub fn codegen_terminator_call<'a, 'tcx: 'a>(
590590
let ret_ebb = fx.get_ebb(dest);
591591
fx.bcx.ins().jump(ret_ebb, &[]);
592592
} else {
593-
trap_unreachable(&mut fx.bcx);
593+
trap_unreachable(fx, "[corruption] Diverging function returned");
594594
}
595595
}
596596

src/base.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,28 @@ fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
108108
let start_ebb = bcx.create_ebb();
109109
bcx.append_ebb_params_for_function_params(start_ebb);
110110
bcx.switch_to_block(start_ebb);
111-
crate::trap::trap_unreachable(&mut bcx);
112-
bcx.seal_all_blocks();
113-
bcx.finalize();
111+
112+
let mut fx = FunctionCx {
113+
tcx,
114+
module: cx.module,
115+
pointer_type: pointer_ty(tcx),
116+
117+
instance,
118+
mir,
119+
120+
bcx,
121+
ebb_map: HashMap::new(),
122+
local_map: HashMap::new(),
123+
124+
clif_comments: crate::pretty_clif::CommentWriter::new(tcx, instance),
125+
constants: &mut cx.ccx,
126+
caches: &mut cx.caches,
127+
source_info_set: indexmap::IndexSet::new(),
128+
};
129+
130+
crate::trap::trap_unreachable(&mut fx, "[unimplemented] Called function with u128 or i128 as argument.");
131+
fx.bcx.seal_all_blocks();
132+
fx.bcx.finalize();
114133

115134
// Step 2b3. Define function
116135
cx.caches.context.func = func;
@@ -254,7 +273,7 @@ fn codegen_fn_content<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx, impl Backend>)
254273
TerminatorKind::Assert {
255274
cond,
256275
expected,
257-
msg: _,
276+
msg,
258277
target,
259278
cleanup: _,
260279
} => {
@@ -267,7 +286,7 @@ fn codegen_fn_content<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx, impl Backend>)
267286
} else {
268287
fx.bcx.ins().brz(cond, target, &[]);
269288
};
270-
trap_panic(&mut fx.bcx);
289+
trap_panic(fx, format!("[panic] Assert {:?} failed.", msg));
271290
}
272291

273292
TerminatorKind::SwitchInt {
@@ -294,8 +313,11 @@ fn codegen_fn_content<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx, impl Backend>)
294313
} => {
295314
crate::abi::codegen_terminator_call(fx, func, args, destination);
296315
}
297-
TerminatorKind::Resume | TerminatorKind::Abort | TerminatorKind::Unreachable => {
298-
trap_unreachable(&mut fx.bcx);
316+
TerminatorKind::Resume | TerminatorKind::Abort => {
317+
trap_unreachable(fx, "[corruption] Unwinding bb reached.");
318+
}
319+
TerminatorKind::Unreachable => {
320+
trap_unreachable(fx, "[corruption] Hit unreachable code.");
299321
}
300322
TerminatorKind::Yield { .. }
301323
| TerminatorKind::FalseEdges { .. }
@@ -742,7 +764,7 @@ pub fn trans_get_discriminant<'a, 'tcx: 'a>(
742764
let layout = place.layout();
743765

744766
if layout.abi == layout::Abi::Uninhabited {
745-
return trap_unreachable_ret_value(fx, dest_layout);
767+
return trap_unreachable_ret_value(fx, dest_layout, "[panic] Tried to get discriminant for uninhabited type.");
746768
}
747769

748770
let (discr_scalar, discr_kind) = match &layout.variants {

src/constant.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,13 @@ pub fn trans_promoted<'a, 'tcx: 'a>(
6969
debug_assert_eq!(cplace.layout(), fx.layout_of(dest_ty));
7070
cplace
7171
}
72-
Err(_) => crate::trap::trap_unreachable_ret_place(fx, fx.layout_of(dest_ty)),
72+
Err(_) => {
73+
crate::trap::trap_unreachable_ret_place(
74+
fx,
75+
fx.layout_of(dest_ty),
76+
"[panic] Tried to get value of promoted value with errored during const eval.",
77+
)
78+
}
7379
}
7480
}
7581

src/intrinsics.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
103103
// Insert non returning intrinsics here
104104
match intrinsic {
105105
"abort" => {
106-
trap_panic(&mut fx.bcx);
106+
trap_panic(fx, "Called intrinisc::abort.");
107107
}
108108
"unreachable" => {
109-
trap_unreachable(&mut fx.bcx);
109+
trap_unreachable(fx, "[corruption] Called intrinsic::unreachable.");
110110
}
111111
_ => unimplemented!("unsupported instrinsic {}", intrinsic),
112112
}
@@ -339,7 +339,7 @@ pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
339339
};
340340
init, () {
341341
if ret.layout().abi == Abi::Uninhabited {
342-
crate::trap::trap_panic(&mut fx.bcx);
342+
crate::trap::trap_panic(fx, "[panic] Called intrinsic::init for uninhabited type.");
343343
return;
344344
}
345345

@@ -377,7 +377,7 @@ pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
377377
};
378378
uninit, <T> () {
379379
if ret.layout().abi == Abi::Uninhabited {
380-
crate::trap::trap_panic(&mut fx.bcx);
380+
crate::trap::trap_panic(fx, "[panic] Called intrinsic::uninit for uninhabited type.");
381381
return;
382382
}
383383

@@ -412,7 +412,7 @@ pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
412412
};
413413
panic_if_uninhabited, <T> () {
414414
if fx.layout_of(T).abi.is_uninhabited() {
415-
crate::trap::trap_panic(&mut fx.bcx);
415+
crate::trap::trap_panic(fx, "[panic] Called intrinsic::panic_if_uninhabited for uninhabited type.");
416416
return;
417417
}
418418
};
@@ -492,6 +492,6 @@ pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
492492
let ret_ebb = fx.get_ebb(dest);
493493
fx.bcx.ins().jump(ret_ebb, &[]);
494494
} else {
495-
trap_unreachable(&mut fx.bcx);
495+
trap_unreachable(fx, "[corruption] Diverging intrinsic returned.");
496496
}
497497
}

src/trap.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,51 @@
11
use crate::prelude::*;
22

3+
fn codegen_print(fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>, msg: &str) {
4+
let puts = fx.module.declare_function("puts", Linkage::Import, &Signature {
5+
call_conv: CallConv::SystemV,
6+
params: vec![AbiParam::new(pointer_ty(fx.tcx))],
7+
returns: vec![],
8+
}).unwrap();
9+
let puts = fx.module.declare_func_in_func(puts, &mut fx.bcx.func);
10+
11+
let symbol_name = fx.tcx.symbol_name(fx.instance);
12+
let msg_bytes = format!("trap at {:?} ({}): {}\0", fx.instance, symbol_name, msg).into_bytes().into_boxed_slice();
13+
let mut data_ctx = DataContext::new();
14+
data_ctx.define(msg_bytes);
15+
let msg_id = fx.module.declare_data(&(symbol_name.as_str().to_string() + msg), Linkage::Local, false).unwrap();
16+
17+
// Ignore DuplicateDefinition error, as the data will be the same
18+
let _ = fx.module.define_data(msg_id, &data_ctx);
19+
20+
let local_msg_id = fx.module.declare_data_in_func(msg_id, fx.bcx.func);
21+
let msg_ptr = fx.bcx.ins().global_value(pointer_ty(fx.tcx), local_msg_id);
22+
fx.bcx.ins().call(puts, &[msg_ptr]);
23+
}
24+
325
/// Trap code: user0
4-
pub fn trap_panic(bcx: &mut FunctionBuilder) {
5-
bcx.ins().trap(TrapCode::User(0));
26+
pub fn trap_panic(fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>, msg: impl AsRef<str>) {
27+
codegen_print(fx, msg.as_ref());
28+
fx.bcx.ins().trap(TrapCode::User(0));
629
}
730

831
/// Trap code: user65535
9-
pub fn trap_unreachable(bcx: &mut FunctionBuilder) {
10-
bcx.ins().trap(TrapCode::User(!0));
32+
pub fn trap_unreachable(fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>, msg: impl AsRef<str>) {
33+
codegen_print(fx, msg.as_ref());
34+
fx.bcx.ins().trap(TrapCode::User(!0));
1135
}
1236

13-
pub fn trap_unreachable_ret_value<'tcx>(fx: &mut FunctionCx<'_, 'tcx, impl cranelift_module::Backend>, dest_layout: TyLayout<'tcx>) -> CValue<'tcx> {
37+
/// Trap code: user65535
38+
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> {
39+
codegen_print(fx, msg.as_ref());
1440
let true_ = fx.bcx.ins().iconst(types::I32, 1);
1541
fx.bcx.ins().trapnz(true_, TrapCode::User(!0));
1642
let zero = fx.bcx.ins().iconst(fx.pointer_type, 0);
1743
CValue::ByRef(zero, dest_layout)
1844
}
1945

20-
pub fn trap_unreachable_ret_place<'tcx>(fx: &mut FunctionCx<'_, 'tcx, impl cranelift_module::Backend>, dest_layout: TyLayout<'tcx>) -> CPlace<'tcx> {
46+
/// Trap code: user65535
47+
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> {
48+
codegen_print(fx, msg.as_ref());
2149
let true_ = fx.bcx.ins().iconst(types::I32, 1);
2250
fx.bcx.ins().trapnz(true_, TrapCode::User(!0));
2351
let zero = fx.bcx.ins().iconst(fx.pointer_type, 0);

0 commit comments

Comments
 (0)