Skip to content

Commit 14c8fe7

Browse files
authored
Merge pull request rust-lang#248 from RalfJung/pointer-games
Memory::read_ptr has to check for relocations on the edges
2 parents fda18f6 + 1fe310c commit 14c8fe7

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/memory.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,7 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
745745
if self.check_defined(ptr, size).is_err() {
746746
return Ok(PrimVal::Undef.into());
747747
}
748+
self.check_relocation_edges(ptr, size)?; // Make sure we don't read part of a pointer as a pointer
748749
let endianess = self.endianess();
749750
let bytes = self.get_bytes_unchecked(ptr, size, size)?;
750751
let offset = read_target_uint(endianess, bytes).unwrap();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#![allow(dead_code)]
2+
3+
// We use packed structs to get around alignment restrictions
4+
#[repr(packed)]
5+
struct Data {
6+
pad: u8,
7+
ptr: &'static i32,
8+
}
9+
10+
// But we need to gurantee some alignment
11+
struct Wrapper {
12+
align: u64,
13+
data: Data,
14+
}
15+
16+
static G : i32 = 0;
17+
18+
fn main() {
19+
let mut w = Wrapper { align: 0, data: Data { pad: 0, ptr: &G } };
20+
21+
// Get a pointer to the beginning of the Data struct (one u8 byte, then the pointer bytes).
22+
// Thanks to the wrapper, we know this is aligned-enough to perform a load at ptr size.
23+
// We load at pointer type, so having a relocation is okay -- but here, the relocation
24+
// starts 1 byte to the right, so using it would actually be wrong!
25+
let d_alias = &mut w.data as *mut _ as *mut *const u8;
26+
unsafe {
27+
let _x = *d_alias; //~ ERROR: tried to access part of a pointer value as raw bytes
28+
}
29+
}

0 commit comments

Comments
 (0)