Skip to content

Commit e132f21

Browse files
committed
Auto merge of #118408 - RalfJung:aggregate-assign-uninit, r=saethlin
miri: add test checking that aggregate assignments reset memory to uninit Also, `write_aggregate` is really just a helper for evaluating `Aggregate` rvalues, so it should be in `step.rs`, not `place.rs`. Also factor out `Repeat` rvalues into their own function while we are at it. r? `@saethlin` Fixes #3195
2 parents 4118d21 + e459e43 commit e132f21

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![feature(core_intrinsics)]
2+
#![feature(custom_mir)]
3+
4+
use std::intrinsics::mir::*;
5+
use std::ptr;
6+
7+
#[repr(C)]
8+
struct S(u8, u16);
9+
10+
#[custom_mir(dialect = "runtime", phase = "optimized")]
11+
fn main() {
12+
mir! {
13+
let s: S;
14+
let sptr;
15+
let sptr2;
16+
let _val;
17+
{
18+
sptr = ptr::addr_of_mut!(s);
19+
sptr2 = sptr as *mut [u8; 4];
20+
*sptr2 = [0; 4];
21+
*sptr = S(0, 0); // should reset the padding
22+
_val = *sptr2; // should hence be UB
23+
//~^ERROR: encountered uninitialized memory
24+
Return()
25+
}
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: constructing invalid value at [1]: encountered uninitialized memory, but expected an integer
2+
--> $DIR/uninit-after-aggregate-assign.rs:LL:CC
3+
|
4+
LL | _val = *sptr2; // should hence be UB
5+
| ^^^^^^^^^^^^^ constructing invalid value at [1]: encountered uninitialized memory, but expected an integer
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/uninit-after-aggregate-assign.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)