Skip to content

Commit 3bee29b

Browse files
committed
try to fix write_wildcards, use Box instead of Vec for test, try to write new expose nested pointer test
1 parent f86ba13 commit 3bee29b

File tree

5 files changed

+51
-35
lines changed

5 files changed

+51
-35
lines changed

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -976,10 +976,9 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
976976

977977
// Prepare for possible write from native code if mutable.
978978
if info.mutbl.is_mut() {
979-
let tcx = self.tcx;
980979
self.get_alloc_raw_mut(id)?
981980
.0
982-
.prepare_for_native_call(&tcx)
981+
.prepare_for_native_call()
983982
.map_err(|e| e.to_interp_error(id))?;
984983
}
985984
}

compiler/rustc_middle/src/mir/interpret/allocation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
646646
/// Initialize all previously uninitialized bytes in the entire allocation, and set
647647
/// provenance of everything to `Wildcard`. Before calling this, make sure all
648648
/// provenance in this allocation is exposed!
649-
pub fn prepare_for_native_call(&mut self, cx: &impl HasDataLayout) -> AllocResult {
649+
pub fn prepare_for_native_call(&mut self) -> AllocResult {
650650
let full_range = AllocRange { start: Size::ZERO, size: Size::from_bytes(self.len()) };
651651
// Overwrite uninitialized bytes.
652652
for chunk in self.init_mask.range_as_init_chunks(full_range) {
@@ -660,7 +660,7 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
660660
self.mark_init(full_range, true);
661661

662662
// Set provenance of all bytes to wildcard.
663-
self.provenance.write_wildcards(self.len(), cx);
663+
self.provenance.write_wildcards(self.len());
664664

665665
Ok(())
666666
}

compiler/rustc_middle/src/mir/interpret/allocation/provenance_map.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -195,27 +195,21 @@ impl<Prov: Provenance> ProvenanceMap<Prov> {
195195

196196
Ok(())
197197
}
198-
199-
pub fn write_wildcards(&mut self, alloc_size: usize, cx: &impl HasDataLayout) {
198+
pub fn write_wildcards(&mut self, alloc_size: usize) {
200199
// We can only write wildcards in Miri.
201200
assert!(
202201
Prov::OFFSET_IS_ADDR,
203202
"writing wildcard provenance is not supported when `OFFSET_IS_ADDR` is false"
204203
);
205204
let wildcard = Prov::WILDCARD.unwrap();
206-
let ptr_size = cx.data_layout().pointer_size;
207205

208-
// Write wildcards in intervals of pointer size.
209-
let end = Size::from_bytes(alloc_size / ptr_size.bytes_usize());
210-
for offset in Size::ZERO..end {
211-
self.ptrs.insert(offset, wildcard);
212-
}
213-
// Write wildcards into the remaining bytes.
206+
// Remove all pointer provenances, then write wildcards into the whole byte range.
207+
self.ptrs.clear();
214208
let last = Size::from_bytes(alloc_size);
215209
let bytes = self.bytes.get_or_insert_with(Box::default);
216-
for offset in end..last {
210+
for offset in Size::ZERO..last {
217211
bytes.insert(offset, wildcard);
218-
} // TODO: Is the above even remotely correct?
212+
}
219213
}
220214
}
221215

src/tools/miri/tests/native-lib/pass/ptr_write_access.rs

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//@ignore-target: windows wasm
33
//@only-on-host
44

5+
#![feature(box_as_ptr)]
6+
57
use std::mem::MaybeUninit;
68

79
fn main() {
@@ -11,11 +13,13 @@ fn main() {
1113

1214
test_init_array();
1315

16+
test_init_interior_mutable();
17+
1418
test_swap_ptr();
1519

16-
test_init_interior_mutable();
20+
test_interact_dangling();
1721

18-
test_dangling();
22+
test_inner_alloc_exposed();
1923
}
2024

2125
fn test_modify_int() {
@@ -59,19 +63,6 @@ fn test_init_array() {
5963
assert_eq!(array, [31; LEN]);
6064
}
6165

62-
fn test_swap_ptr() {
63-
extern "C" {
64-
fn swap_ptr(pptr0: *mut *const i32, pptr1: *mut *const i32);
65-
}
66-
67-
let x = 41;
68-
let mut ptr0 = &raw const x;
69-
let mut ptr1 = std::ptr::null();
70-
unsafe { swap_ptr(&mut ptr0, &mut ptr1) };
71-
72-
assert_eq!(unsafe { *ptr1 }, x);
73-
}
74-
7566
fn test_init_interior_mutable() {
7667
extern "C" {
7768
fn init_interior_mutable(pptr: *const UnsafeInterior);
@@ -90,16 +81,42 @@ fn test_init_interior_mutable() {
9081
assert_eq!(unsafe { x.assume_init() }, 51);
9182
}
9283

93-
fn test_dangling() {
84+
fn test_swap_ptr() {
85+
extern "C" {
86+
fn swap_ptr(pptr0: *mut *const i32, pptr1: *mut *const i32);
87+
}
88+
89+
let x = 41;
90+
let mut ptr0 = &raw const x;
91+
let mut ptr1 = std::ptr::null();
92+
unsafe { swap_ptr(&mut ptr0, &mut ptr1) };
93+
94+
assert_eq!(unsafe { *ptr1 }, x);
95+
}
96+
97+
fn test_interact_dangling() {
9498
extern "C" {
9599
fn overwrite_ptr(pptr: *mut *const i32);
96100
}
97101

98-
let x = vec![61];
99-
let mut ptr = x.as_ptr();
102+
let x = Box::new(61);
103+
let mut ptr = Box::as_ptr(&x);
100104
drop(x);
101105
unsafe { overwrite_ptr(&mut ptr) };
106+
102107
assert_eq!(ptr, std::ptr::null());
103108
}
104109

105-
// TODO: Write tests for (forgetting to) expose: -initial allocation -recursively all allocations -unexposed pointers.
110+
// TODO: Write tests for correctly exposing: [initial allocation; recursively all allocations; previously unexposed pointers].
111+
fn test_inner_alloc_exposed() {
112+
extern "C" {
113+
fn blackbox(ptr: *mut *mut *const i32);
114+
}
115+
116+
let x = 71i32;
117+
let mut ptr = &raw const x;
118+
let mut pptr = &raw mut ptr;
119+
unsafe { blackbox(&mut pptr) }
120+
121+
assert_eq!(unsafe { *ptr }, x);
122+
}

src/tools/miri/tests/native-lib/ptr_write_access.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@ EXPORT void init_interior_mutable(const UnsafeInterior *u_ptr) {
4141
*(u_ptr->mut_ptr) = 51;
4242
}
4343

44-
/* Test: test_dangling */
44+
/* Test: test_interact_dangling */
4545

4646
EXPORT void overwrite_ptr(const int **pptr) {
4747
*pptr = NULL;
4848
}
49+
50+
/* Test: test_initial_alloc_exposed */
51+
52+
EXPORT void blackbox(__attribute__((unused)) const int ***_ppptr) {
53+
return;
54+
}

0 commit comments

Comments
 (0)