Skip to content

Commit 8b90dee

Browse files
committed
fix current compile errors, fmt
1 parent bf5d369 commit 8b90dee

File tree

6 files changed

+44
-25
lines changed

6 files changed

+44
-25
lines changed

compiler/rustc_const_eval/src/const_eval/machine.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ use crate::errors::{LongRunning, LongRunningWarn};
2121
use crate::fluent_generated as fluent;
2222
use crate::interpret::{
2323
self, AllocId, AllocRange, ConstAllocation, CtfeProvenance, FnArg, Frame, GlobalAlloc, ImmTy,
24-
InterpCx, InterpResult, MPlaceTy, OpTy, Pointer, RangeSet, Scalar, compile_time_machine,
25-
interp_ok, throw_exhaust, throw_inval, throw_ub, throw_ub_custom, throw_unsup,
26-
throw_unsup_format,
24+
InterpCx, InterpResult, MPlaceTy, OpTy, RangeSet, Scalar, compile_time_machine, interp_ok,
25+
throw_exhaust, throw_inval, throw_ub, throw_ub_custom, throw_unsup, throw_unsup_format,
2726
};
2827

2928
/// When hitting this many interpreted terminators we emit a deny by default lint
@@ -586,7 +585,10 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
586585
}
587586

588587
#[inline(always)]
589-
fn expose_provenance(_ecx: &mut InterpCx<'tcx, Self>, _provenance: Self::Provenance) -> InterpResult<'tcx> {
588+
fn expose_provenance(
589+
_ecx: &mut InterpCx<'tcx, Self>,
590+
_provenance: Self::Provenance,
591+
) -> InterpResult<'tcx> {
590592
// This is only reachable with -Zunleash-the-miri-inside-of-you.
591593
throw_unsup_format!("exposing pointers is not possible at compile-time")
592594
}

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -943,32 +943,41 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
943943
self.get_alloc_raw_mut(id)?.0.mutability = Mutability::Not;
944944
interp_ok(())
945945
}
946-
947-
pub fn prepare_for_native_call(&mut self, id: AllocId, initial_prov: M) -> InterpResult<'tcx> {
946+
947+
pub fn prepare_for_native_call(
948+
&mut self,
949+
id: AllocId,
950+
initial_prov: M::Provenance,
951+
) -> InterpResult<'tcx> {
952+
// Expose provenance of the root allocation.
953+
M::expose_provenance(self, initial_prov)?; // TODO: Is this the right way to expose provenance?
954+
948955
let mut done = rustc_data_structures::fx::FxHashSet::default();
949956
let mut todo = vec![id];
950957
while let Some(id) = todo.pop() {
951958
if done.insert(id) {
952959
// This is a new allocation, add the allocation it points to `todo`.
953-
let (_size, _align, kind, mutability) = self.get_alloc_info(id); // TODO: Rebasing will give me the mutaility.
960+
let info = self.get_alloc_info(id);
954961

955962
// If there is no data behind this pointer, skip this.
956-
if !matches!(kind, AllocKind::LiveData) {
963+
if !matches!(info.kind, AllocKind::LiveData) {
957964
continue;
958965
}
959966

960967
let alloc = self.get_alloc_raw(id)?;
961968
for prov in alloc.provenance().provenances() {
962-
M::expose_provenance(self, prov)?; // TODO: Is this right?
969+
//M::expose_provenance(self, prov)?; // TODO: Is this the right way to expose provenance? + mutable borrow here gives issues due to provenances iterator lifetime...
963970
if let Some(id) = prov.get_alloc_id() {
964971
todo.push(id);
965972
}
966973
}
967974

968975
// Prepare for possible write from native code if mutable.
969-
if mutability.is_mut() {
976+
if info.mutbl.is_mut() {
977+
let tcx = self.tcx;
970978
self.get_alloc_raw_mut(id)?
971-
.prepare_for_native_call()
979+
.0
980+
.prepare_for_native_call(&tcx)
972981
.map_err(|e| e.to_interp_error(id))?;
973982
}
974983
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -645,21 +645,22 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
645645

646646
/// Initialize all previously uninitialized bytes in the entire allocation, and set
647647
/// provenance of everything to `Wildcard`
648-
pub fn prepare_for_native_call(&mut self) -> AllocResult {
648+
pub fn prepare_for_native_call(&mut self, cx: &impl HasDataLayout) -> AllocResult {
649649
let full_range = AllocRange { start: Size::ZERO, size: Size::from_bytes(self.len()) };
650650
// Overwrite uninitialized bytes.
651651
for chunk in self.init_mask.range_as_init_chunks(full_range) {
652652
if !chunk.is_init() {
653-
let uninit_bytes = &mut self.bytes[chunk.range().start.bytes_usize()..chunk.range().end.bytes_usize()];
653+
let uninit_bytes = &mut self.bytes
654+
[chunk.range().start.bytes_usize()..chunk.range().end.bytes_usize()];
654655
uninit_bytes.fill(0);
655656
}
656657
}
657658
// Mark everything as initialized now.
658659
self.mark_init(full_range, true);
659660

660661
// Set provenance of all bytes to wildcard.
661-
self.provenance.write_wildcards(self.len());
662-
662+
self.provenance.write_wildcards(self.len(), cx);
663+
663664
Ok(())
664665
}
665666

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,15 @@ impl<Prov: Provenance> ProvenanceMap<Prov> {
196196
Ok(())
197197
}
198198

199-
pub fn write_wildcards(&mut self, _alloc_size: usize) {
199+
pub fn write_wildcards(&mut self, _alloc_size: usize, _cx: &impl HasDataLayout) {
200200
// We can only write wildcards in Miri.
201-
assert!(Prov::OFFSET_IS_ADDR, "writing wildcard provenance is not supported when `OFFSET_IS_ADDR` is false");
201+
assert!(
202+
Prov::OFFSET_IS_ADDR,
203+
"writing wildcard provenance is not supported when `OFFSET_IS_ADDR` is false"
204+
);
202205
let _wildcard = Prov::WILDCARD.unwrap();
203-
204-
// TODO: This.
205206

207+
// TODO: Write wildcards into `self` in intervals of `cx.data_layout().pointer_size` + remaining bytes?
206208
}
207209
}
208210

src/tools/miri/src/shims/native_lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,17 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
155155
let imm = this.read_immediate(arg)?;
156156
libffi_args.push(imm_to_carg(&imm, this)?);
157157
if matches!(arg.layout.ty.kind(), ty::RawPtr(..)) {
158-
let ptr = imm.to_scalar().to_pointer(self)?;
158+
let ptr = imm.to_scalar().to_pointer(this)?;
159+
let Some(prov) = ptr.provenance else {
160+
// Pointer without provenance may access any memory.
161+
continue;
162+
};
159163
// We use `get_alloc_id` for its best-effort behaviour with Wildcard provenance.
160-
let Ok(alloc_id) = ptr.provenance.and_then(|prov| prov.get_alloc_id()) else {
164+
let Some(alloc_id) = prov.get_alloc_id() else {
161165
// Pointer without provenance may access any memory.
162166
continue;
163167
};
164-
this.prepare_for_native_call(alloc_id, ptr.provenance)?;
165-
// TODO: Write tests for (forgetting to) expose: -initial allocation -recursively all allocations -unexposed pointers.
168+
this.prepare_for_native_call(alloc_id, prov)?;
166169
}
167170
}
168171

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,7 @@ fn test_dangling() {
9292
let mut ptr = &raw const x;
9393
drop(x);
9494
unsafe { write_nullptr(&mut ptr) };
95-
assert_eq!(ptr)
96-
}
95+
assert_eq!(ptr, std::ptr::null());
96+
}
97+
98+
// TODO: Write tests for (forgetting to) expose: -initial allocation -recursively all allocations -unexposed pointers.

0 commit comments

Comments
 (0)