Skip to content

Commit dae48ea

Browse files
committed
fast_math: detect non-finite results
1 parent 4658d38 commit dae48ea

File tree

9 files changed

+27
-1
lines changed

9 files changed

+27
-1
lines changed

src/tools/miri/src/shims/intrinsics/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
268268
),
269269
_ => {}
270270
}
271-
this.binop_ignore_overflow(op, &a, &b, dest)?;
271+
let res = this.wrapping_binary_op(op, &a, &b)?;
272+
if !float_finite(&res)? {
273+
throw_ub_format!("`{intrinsic_name}` intrinsic produced non-finite value as result");
274+
}
275+
this.write_immediate(*res, dest)?;
272276
}
273277

274278
#[rustfmt::skip]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![feature(core_intrinsics)]
2+
3+
fn main() {
4+
unsafe {
5+
let _x: f32 = core::intrinsics::fdiv_fast(1.0, 0.0); //~ ERROR: `fdiv_fast` intrinsic produced non-finite value as result
6+
}
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: `fdiv_fast` intrinsic produced non-finite value as result
2+
--> $DIR/fast_math_result.rs:LL:CC
3+
|
4+
LL | let _x: f32 = core::intrinsics::fdiv_fast(1.0, 0.0);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `fdiv_fast` intrinsic produced non-finite value as result
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: BACKTRACE:
10+
= note: inside `main` at $DIR/fast_math_result.rs:LL:CC
11+
12+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
14+
error: aborting due to 1 previous error
15+

0 commit comments

Comments
 (0)