Skip to content

Commit 49bbc5b

Browse files
committed
Add pointer to struct serialization test
In C and Rust refcount struct sizes are different. Make sure that the pointer serialization uses the C size.
1 parent c111151 commit 49bbc5b

3 files changed

Lines changed: 196 additions & 0 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
#[derive(Default)]
10+
pub struct pair {
11+
pub x: Value<i32>,
12+
pub y: Value<i32>,
13+
}
14+
impl ByteRepr for pair {
15+
fn byte_size() -> usize {
16+
8
17+
}
18+
fn to_bytes(&self, buf: &mut [u8]) {
19+
(*self.x.borrow()).to_bytes(&mut buf[0..4]);
20+
(*self.y.borrow()).to_bytes(&mut buf[4..8]);
21+
}
22+
fn from_bytes(buf: &[u8]) -> Self {
23+
Self {
24+
x: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
25+
y: Rc::new(RefCell::new(<i32>::from_bytes(&buf[4..8]))),
26+
}
27+
}
28+
}
29+
pub fn main() {
30+
std::process::exit(main_0());
31+
}
32+
fn main_0() -> i32 {
33+
let arr: Value<Box<[pair]>> = Rc::new(RefCell::new(
34+
(0..3).map(|_| <pair>::default()).collect::<Box<[pair]>>(),
35+
));
36+
(*(*arr.borrow())[(0) as usize].x.borrow_mut()) = 10;
37+
(*(*arr.borrow())[(1) as usize].x.borrow_mut()) = 20;
38+
(*(*arr.borrow())[(2) as usize].x.borrow_mut()) = 30;
39+
pub struct anon_0 {
40+
__bytes: Value<Box<[u8]>>,
41+
}
42+
impl anon_0 {
43+
pub fn p(&self) -> Ptr<Ptr<pair>> {
44+
(self.__bytes.as_pointer() as Ptr<u8>).reinterpret_cast()
45+
}
46+
pub fn bits(&self) -> Ptr<u64> {
47+
(self.__bytes.as_pointer() as Ptr<u8>).reinterpret_cast()
48+
}
49+
}
50+
impl Clone for anon_0 {
51+
fn clone(&self) -> Self {
52+
anon_0 {
53+
__bytes: Rc::new(RefCell::new(self.__bytes.borrow().clone())),
54+
}
55+
}
56+
}
57+
impl Default for anon_0 {
58+
fn default() -> Self {
59+
anon_0 {
60+
__bytes: Rc::new(RefCell::new(Box::from([0u8; 8]))),
61+
}
62+
}
63+
}
64+
impl ByteRepr for anon_0 {
65+
fn byte_size() -> usize {
66+
8
67+
}
68+
fn to_bytes(&self, buf: &mut [u8]) {
69+
buf.copy_from_slice(&self.__bytes.borrow());
70+
}
71+
fn from_bytes(buf: &[u8]) -> Self {
72+
anon_0 {
73+
__bytes: Rc::new(RefCell::new(Box::from(buf))),
74+
}
75+
}
76+
};
77+
let u: Value<anon_0> = <Value<anon_0>>::default();
78+
(*u.borrow_mut())
79+
.p()
80+
.write(((arr.as_pointer() as Ptr<pair>).offset(1)));
81+
let q: Value<Ptr<pair>> = Rc::new(RefCell::new(((*u.borrow()).p().read()).clone()));
82+
assert!(((((*(*(*q.borrow()).upgrade().deref()).x.borrow()) == 20) as i32) != 0));
83+
assert!(
84+
((({
85+
let _lhs = (*q.borrow()).clone();
86+
_lhs == ((arr.as_pointer() as Ptr<pair>).offset(1))
87+
}) as i32)
88+
!= 0)
89+
);
90+
let rhs_0 = ((((*u.borrow()).bits().read()) as u64).wrapping_add((8usize as u64))) as u64;
91+
(*u.borrow_mut()).bits().write(rhs_0);
92+
assert!(((((*(*((*u.borrow()).p().read()).upgrade().deref()).x.borrow()) == 30) as i32) != 0));
93+
assert!(
94+
((({
95+
let _lhs = ((*u.borrow()).p().read()).clone();
96+
_lhs == ((arr.as_pointer() as Ptr<pair>).offset(2))
97+
}) as i32)
98+
!= 0)
99+
);
100+
let rhs_0 = ((((*u.borrow()).bits().read()) as u64)
101+
.wrapping_sub(((2_usize).wrapping_mul((8usize as usize)) as u64))) as u64;
102+
(*u.borrow_mut()).bits().write(rhs_0);
103+
assert!(((((*(*((*u.borrow()).p().read()).upgrade().deref()).x.borrow()) == 10) as i32) != 0));
104+
assert!(
105+
((({
106+
let _lhs = ((*u.borrow()).p().read()).clone();
107+
_lhs == ((arr.as_pointer() as Ptr<pair>).offset(0))
108+
}) as i32)
109+
!= 0)
110+
);
111+
return 0;
112+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
#[repr(C)]
10+
#[derive(Copy, Clone, Default)]
11+
pub struct pair {
12+
pub x: i32,
13+
pub y: i32,
14+
}
15+
pub fn main() {
16+
unsafe {
17+
std::process::exit(main_0() as i32);
18+
}
19+
}
20+
unsafe fn main_0() -> i32 {
21+
let mut arr: [pair; 3] = [<pair>::default(); 3];
22+
arr[(0) as usize].x = 10;
23+
arr[(1) as usize].x = 20;
24+
arr[(2) as usize].x = 30;
25+
#[repr(C)]
26+
#[derive(Copy, Clone)]
27+
pub union anon_0 {
28+
pub p: *mut pair,
29+
pub bits: u64,
30+
}
31+
impl Default for anon_0 {
32+
fn default() -> Self {
33+
unsafe { std::mem::zeroed() }
34+
}
35+
};
36+
let mut u: anon_0 = <anon_0>::default();
37+
u.p = (&mut arr[(1) as usize] as *mut pair);
38+
let mut q: *mut pair = u.p;
39+
assert!((((((*q).x) == (20)) as i32) != 0));
40+
assert!(((((q) == (&mut arr[(1) as usize] as *mut pair)) as i32) != 0));
41+
u.bits = ((u.bits as u64).wrapping_add((::std::mem::size_of::<pair>() as u64))) as u64;
42+
assert!((((((*u.p).x) == (30)) as i32) != 0));
43+
assert!(((((u.p) == (&mut arr[(2) as usize] as *mut pair)) as i32) != 0));
44+
u.bits = ((u.bits as u64)
45+
.wrapping_sub(((2_usize).wrapping_mul((::std::mem::size_of::<pair>() as usize)) as u64)))
46+
as u64;
47+
assert!((((((*u.p).x) == (10)) as i32) != 0));
48+
assert!(((((u.p) == (&mut arr[(0) as usize] as *mut pair)) as i32) != 0));
49+
return 0;
50+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <assert.h>
2+
#include <stdint.h>
3+
4+
struct pair {
5+
int x;
6+
int y;
7+
};
8+
9+
int main(void) {
10+
struct pair arr[3];
11+
arr[0].x = 10;
12+
arr[1].x = 20;
13+
arr[2].x = 30;
14+
15+
union {
16+
struct pair *p;
17+
uintptr_t bits;
18+
} u;
19+
20+
u.p = &arr[1];
21+
struct pair *q = u.p;
22+
assert(q->x == 20);
23+
assert(q == &arr[1]);
24+
25+
u.bits += sizeof(struct pair);
26+
assert(u.p->x == 30);
27+
assert(u.p == &arr[2]);
28+
29+
u.bits -= 2 * sizeof(struct pair);
30+
assert(u.p->x == 10);
31+
assert(u.p == &arr[0]);
32+
33+
return 0;
34+
}

0 commit comments

Comments
 (0)