Skip to content

Commit d3e4587

Browse files
committed
Add strict_provenance and replace to_bits with expose_addr
Unstable feature strict_provenace is used. This is used to require more strict pointer operations. The case is explained in rust-lang/rust#95228 "If you ever want to treat something as a Real Pointer that can be Offset and Dereferenced, there must be an unbroken chain of custody from that pointer to the original allocation you are trying to access using only pointer->pointer operations. If at any point you turn a pointer into an integer, that integer cannot be turned back into a pointer. This includes usize as ptr, transmute, type punning with raw pointer reads/writes, whatever. Just assume the memory "knows" it contains a pointer and that writing to it as a non-pointer makes it forget." This commit includes replacement of to_bits() calls with calls to expose_addr(). Signed-off-by: Markku Kylänpää <markku.kylanpaa@vtt.fi>
1 parent ff8a729 commit d3e4587

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

libhapp/src/device.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ impl Device {
279279
MapFlags::MAP_SHARED,
280280
self.device_file.as_ref().unwrap().as_raw_fd(),
281281
addr as off_t) } {
282-
return Ok(ptr.to_bits());
282+
return Ok(ptr.expose_addr());
283283
}
284284

285285
return Err(Error::Device);

libhapp/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Copyright (C) 2022 VTT Technical Research Centre of Finland Ltd
55

66
#![feature(ptr_to_from_bits)]
7+
#![feature(strict_provenance)]
78

89
extern crate std;
910

libhapp/src/memory.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,17 @@ impl MemoryArea {
104104
}
105105

106106
fn base_addr(&self) -> uintptr {
107-
self.base.to_bits()
107+
self.base.expose_addr()
108108
}
109109

110110
fn size(&self) -> usize {
111111
self.size
112112
}
113113

114114
fn top(&self) -> uintptr {
115-
unsafe { self.base.add(self.base_offset + self.free_offset).to_bits() }
115+
unsafe {
116+
self.base.add(self.base_offset + self.free_offset).expose_addr()
117+
}
116118
}
117119

118120
fn alloc_page(&mut self) -> Result<Page, Error> {
@@ -207,7 +209,7 @@ impl <'a>Page<'a> {
207209
}
208210

209211
fn base_addr(&self) -> uintptr {
210-
self.content.as_ptr().to_bits()
212+
self.content.as_ptr().expose_addr()
211213
}
212214

213215
pub(crate) fn write(&mut self,
@@ -278,7 +280,7 @@ impl <'a>Memory<'a> {
278280
let priv_mem = MemoryArea::new(base, pm_size);
279281
// Allocate the root page table. Since this is the first allocation
280282
// it will always be at offset 0.
281-
let root_page = base.to_bits();
283+
let root_page = base.expose_addr();
282284
Ok(Self {device: device,
283285
mappings: None,
284286
root_pgt: root_page,
@@ -310,7 +312,7 @@ impl <'a>Memory<'a> {
310312
}
311313

312314
let offset = PageTable::index(page_addr, 0) * PageTableEntry::SIZE;
313-
let addr = pt.entries.as_ptr().to_bits() + offset;
315+
let addr = pt.entries.as_ptr().expose_addr() + offset;
314316
assert!(addr != 0);
315317
return Ok(PageTableEntry::wrap(addr));
316318
}

0 commit comments

Comments
 (0)