fast_math: detect non-finite results

This commit is contained in:
Ralf Jung 2023-12-27 20:31:28 +01:00
parent 4658d3816c
commit dae48ea4bb
9 changed files with 27 additions and 1 deletions

View File

@ -268,7 +268,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
),
_ => {}
}
this.binop_ignore_overflow(op, &a, &b, dest)?;
let res = this.wrapping_binary_op(op, &a, &b)?;
if !float_finite(&res)? {
throw_ub_format!("`{intrinsic_name}` intrinsic produced non-finite value as result");
}
this.write_immediate(*res, dest)?;
}
#[rustfmt::skip]

View File

@ -0,0 +1,7 @@
#![feature(core_intrinsics)]
fn main() {
unsafe {
let _x: f32 = core::intrinsics::fdiv_fast(1.0, 0.0); //~ ERROR: `fdiv_fast` intrinsic produced non-finite value as result
}
}

View File

@ -0,0 +1,15 @@
error: Undefined Behavior: `fdiv_fast` intrinsic produced non-finite value as result
--> $DIR/fast_math_result.rs:LL:CC
|
LL | let _x: f32 = core::intrinsics::fdiv_fast(1.0, 0.0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `fdiv_fast` intrinsic produced non-finite value as result
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
= note: BACKTRACE:
= note: inside `main` at $DIR/fast_math_result.rs:LL:CC
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
error: aborting due to 1 previous error