You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rollup merge of #126265 - RalfJung:interpret-cast-validity, r=oli-obk
interpret: ensure we check bool/char for validity when they are used in a cast
In general, `Scalar::to_bits` is a bit dangerous as it bypasses all type information. We should usually prefer matching on the type and acting according to that. So I also refactored `unary_op` handling of integers to do that. The remaining `to_bits` uses are operations that just fundamentally don't care about the sign (and only work on integers).
invalid_char_cast.rs is the key new test, the others already passed before this PR.
r? `@oli-obk`
error: Undefined Behavior: interpreting an invalid 32-bit value as a char: $HEX
2
+
--> $DIR/invalid_char_cast.rs:LL:CC
3
+
|
4
+
LL | RET = *ptr as u32;
5
+
| ^^^^^^^^^^^^^^^^^ interpreting an invalid 32-bit value as a char: $HEX
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 `cast` at $DIR/invalid_char_cast.rs:LL:CC
11
+
note: inside `main`
12
+
--> $DIR/invalid_char_cast.rs:LL:CC
13
+
|
14
+
LL | cast(&v as *const u32 as *const char);
15
+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16
+
17
+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
error: Undefined Behavior: interpreting an invalid 32-bit value as a char: $HEX
2
+
--> $DIR/invalid_char_match.rs:LL:CC
3
+
|
4
+
LL | / match *ptr {
5
+
LL | | '0' => ret,
6
+
LL | | _ => ret,
7
+
LL | | }
8
+
| |_____________^ interpreting an invalid 32-bit value as a char: $HEX
9
+
|
10
+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
11
+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
12
+
= note: BACKTRACE:
13
+
= note: inside `switch_int` at $DIR/invalid_char_match.rs:LL:CC
14
+
note: inside `main`
15
+
--> $DIR/invalid_char_match.rs:LL:CC
16
+
|
17
+
LL | switch_int(&v as *const u32 as *const char);
18
+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19
+
20
+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
error: Undefined Behavior: enum value has invalid tag: 0xff
2
+
--> $DIR/invalid_enum_cast.rs:LL:CC
3
+
|
4
+
LL | let _val = *ptr as u32;
5
+
| ^^^^^^^^^^^ enum value has invalid tag: 0xff
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 `cast` at $DIR/invalid_enum_cast.rs:LL:CC
11
+
note: inside `main`
12
+
--> $DIR/invalid_enum_cast.rs:LL:CC
13
+
|
14
+
LL | cast(&v as *const u32 as *const E);
15
+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16
+
17
+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
0 commit comments