Skip to content

Commit 450ef91

Browse files
author
Ellen Arteca
committed
broken, but at least finding poitner to internal bytes in allocation. now need to make allocid the memory address
1 parent 7d0ad06 commit 450ef91

File tree

9 files changed

+255
-7
lines changed

9 files changed

+255
-7
lines changed

ffi_tests/Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi_tests/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "ffi-tests"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
#
8+
[build-dependencies]
9+
cc = "1.0"

ffi_tests/build.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extern crate cc;
2+
3+
fn main() {
4+
cc::Build::new()
5+
.file("src/test.c")
6+
.compile("libtest.a");
7+
}

ffi_tests/src/libtestlib.so

15.3 KB
Binary file not shown.

ffi_tests/src/main.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
extern "C" {
2+
fn get_num(x: i32) -> i32;
3+
fn printer();
4+
fn get_dbl(x: i32) -> f64;
5+
fn test_stack_spill(a:i32, b:i32, c:i32, d:i32, e:i32, f:i32, g:i32, h:i32, i:i32, j:i32, k:i32, l:i32) -> i32;
6+
fn pointer_test() -> *mut i32;
7+
fn ptr_printer(x: *mut i32);
8+
fn ddref_print(x: *mut *mut i32);
9+
}
10+
11+
//extern "C" { pub fn get_num () -> :: std :: os :: raw :: c_int ; }
12+
13+
fn main() {
14+
//let x;
15+
unsafe {
16+
let mut y: i32 = 45;
17+
let mut z = (&mut y) as *mut i32;
18+
let mut z = &mut z;
19+
println!("{:?}, {:?}, {:?}", z, *z, **z);
20+
println!("as **i32 in rust: {:?}", (z as *mut *mut i32));
21+
println!("as **i32 in rust: {:?}", **(z as *mut *mut i32));
22+
ddref_print(z as *mut *mut i32);
23+
/*
24+
//println!("{}", get_num());
25+
printer();
26+
x = get_num(1);
27+
// let y = get_dbl(x);
28+
29+
println!("{}", x);
30+
printer();
31+
let y = test_stack_spill(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
32+
println!("{}", y);
33+
34+
let ptr = pointer_test();
35+
36+
ptr_printer(ptr);
37+
println!("In Rust this pointer has value: {:?}", *ptr);
38+
39+
*ptr = 10;
40+
ptr_printer(ptr);
41+
println!("In Rust this pointer has value: {:?}", *ptr);
42+
43+
//let ptr2 = pointer_test();
44+
//println!("{:?}, {:?}", *ptr, *ptr2);*/
45+
}
46+
//println!("x: {:?}", x);
47+
//println!("rjeiworjweio");
48+
}

ffi_tests/src/test.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
void ddref_print(int **x) {
5+
printf("PTR %d\n", x);
6+
printf("*PTR %d\n", *x);
7+
printf("**PTR %d\n", **x);
8+
}
9+
10+
int get_num(int x) {
11+
return 2 + x;
12+
}
13+
14+
int* pointer_test() {
15+
int *point = malloc(sizeof(int));
16+
*point=1;
17+
return point;
18+
}
19+
20+
void ptr_printer(int *x) {
21+
printf("pointer has value: %d\n", *x);
22+
}
23+
24+
double get_dbl(int x) {
25+
return 2.75 + x;
26+
}
27+
28+
void printer() {
29+
printf("printing from C\n");
30+
}
31+
32+
int test_stack_spill(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k, int l) {
33+
return a+b+c+d+e+f+g+h+i+j+k+l;
34+
}

src/machine.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ pub enum Provenance {
135135
sb: SbTag,
136136
},
137137
Wildcard,
138+
CHasAccess,
138139
}
139140

140141
/// The "extra" information a pointer has over a regular AllocId.
@@ -145,12 +146,12 @@ pub enum ProvenanceExtra {
145146
}
146147

147148
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
148-
static_assert_size!(Pointer<Provenance>, 24);
149+
static_assert_size!(Pointer<Provenance>, 32);
149150
// FIXME: this would with in 24bytes but layout optimizations are not smart enough
150151
// #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
151152
//static_assert_size!(Pointer<Option<Provenance>>, 24);
152153
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
153-
static_assert_size!(ScalarMaybeUninit<Provenance>, 32);
154+
static_assert_size!(ScalarMaybeUninit<Provenance>, 40);
154155

155156
impl interpret::Provenance for Provenance {
156157
/// We use absolute addresses in the `offset` of a `Pointer<Provenance>`.
@@ -177,6 +178,9 @@ impl interpret::Provenance for Provenance {
177178
Provenance::Wildcard => {
178179
write!(f, "[wildcard]")?;
179180
}
181+
Provenance::CHasAccess => {
182+
write!(f, "[c has access]")?;
183+
}
180184
}
181185

182186
Ok(())
@@ -186,6 +190,7 @@ impl interpret::Provenance for Provenance {
186190
match self {
187191
Provenance::Concrete { alloc_id, .. } => Some(alloc_id),
188192
Provenance::Wildcard => None,
193+
Provenance::CHasAccess => None,
189194
}
190195
}
191196
}
@@ -219,6 +224,7 @@ pub struct AllocExtra {
219224
/// Weak memory emulation via the use of store buffers,
220225
/// this is only added if it is enabled.
221226
pub weak_memory: Option<weak_memory::AllocExtra>,
227+
// pub real_pointer: u64,
222228
}
223229

224230
/// Precomputed layouts of primitive types
@@ -692,6 +698,16 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
692698
alloc: Cow<'b, Allocation>,
693699
kind: Option<MemoryKind<Self::MemoryKind>>,
694700
) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra>>> {
701+
let (size, _, _) = ecx.get_alloc_info(id);
702+
let fake_range = AllocRange{ start: rustc_target::abi::Size::ZERO, size: size};
703+
let ree = ecx.memory.alloc_map().get(id).unwrap().1.get_bytes_with_uninit_and_ptr(ecx, fake_range).unwrap();
704+
705+
// let bytes_ptr = alloc.get_bytes( ecx, fake_range);
706+
// unsafe {
707+
// if bytes_ptr.is_ok() {//&& id.0 > std::num::NonZeroU64::new(1600).unwrap(){
708+
// // println!("{:?}, {:?}", id, *(bytes_ptr.unwrap().as_ptr()));
709+
// }
710+
// }
695711
let kind = kind.expect("we set our STATIC_KIND so this cannot be None");
696712
if ecx.machine.tracked_alloc_ids.contains(&id) {
697713
register_diagnostic(NonHaltingDiagnostic::CreatedAlloc(
@@ -729,12 +745,14 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
729745
} else {
730746
None
731747
};
748+
// println!("{:?}", ree.as_ptr() as u64);
732749
let alloc: Allocation<Provenance, Self::AllocExtra> = alloc.adjust_from_tcx(
733750
&ecx.tcx,
734751
AllocExtra {
735752
stacked_borrows: stacks.map(RefCell::new),
736753
data_race: race_alloc,
737754
weak_memory: buffer_alloc,
755+
// real_pointer: 0,
738756
},
739757
|ptr| ecx.global_base_pointer(ptr),
740758
)?;
@@ -794,7 +812,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
794812
match ptr.provenance {
795813
Provenance::Concrete { alloc_id, sb } =>
796814
intptrcast::GlobalStateInner::expose_ptr(ecx, alloc_id, sb),
797-
Provenance::Wildcard => {
815+
Provenance::Wildcard | Provenance::CHasAccess => {
798816
// No need to do anything for wildcard pointers as
799817
// their provenances have already been previously exposed.
800818
Ok(())
@@ -813,7 +831,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
813831
rel.map(|(alloc_id, size)| {
814832
let sb = match ptr.provenance {
815833
Provenance::Concrete { sb, .. } => ProvenanceExtra::Concrete(sb),
816-
Provenance::Wildcard => ProvenanceExtra::Wildcard,
834+
Provenance::Wildcard | Provenance::CHasAccess => ProvenanceExtra::Wildcard,
817835
};
818836
(alloc_id, size, sb)
819837
})

src/mono_hash_map.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl<K: Hash + Eq, V> Default for MonoHashMap<K, V> {
3737
}
3838
}
3939

40-
impl<K: Hash + Eq, V> AllocMap<K, V> for MonoHashMap<K, V> {
40+
impl<K: Hash + Eq, V: std::fmt::Debug> AllocMap<K, V> for MonoHashMap<K, V> {
4141
#[inline(always)]
4242
fn contains_key<Q: ?Sized + Hash + Eq>(&mut self, k: &Q) -> bool
4343
where
@@ -48,6 +48,7 @@ impl<K: Hash + Eq, V> AllocMap<K, V> for MonoHashMap<K, V> {
4848

4949
#[inline(always)]
5050
fn insert(&mut self, k: K, v: V) -> Option<V> {
51+
println!("WHY: {:?}", v);
5152
self.0.get_mut().insert(k, Box::new(v)).map(|x| *x)
5253
}
5354

0 commit comments

Comments
 (0)