Skip to content

Commit 6200c87

Browse files
committed
Add todo implementations for address-taken libcc2rs functions
1 parent 22edd84 commit 6200c87

8 files changed

Lines changed: 346 additions & 2 deletions

File tree

libcc2rs/src/alloc.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
// Copyright (c) 2022-present INESC-ID.
22
// Distributed under the MIT license that can be found in the LICENSE file.
33

4+
use crate::rc::{AnyPtr, Ptr};
5+
6+
pub fn malloc_refcount(_a0: usize) -> AnyPtr {
7+
todo!("malloc_refcount")
8+
}
9+
10+
pub fn free_refcount(_a0: AnyPtr) {
11+
todo!("free_refcount")
12+
}
13+
14+
pub fn realloc_refcount(_a0: AnyPtr, _a1: usize) -> AnyPtr {
15+
todo!("realloc_refcount")
16+
}
17+
18+
pub fn calloc_refcount(_a0: usize, _a1: usize) -> AnyPtr {
19+
todo!("calloc_refcount")
20+
}
21+
22+
pub fn strdup_refcount(_a0: Ptr<u8>) -> Ptr<u8> {
23+
todo!("strdup_refcount")
24+
}
25+
426
/// # Safety
527
///
628
/// Same contract as C's `malloc`.

rules/cstdlib/tgt_refcount.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
use libcc2rs::*;
5+
6+
fn f2(a0: AnyPtr) {
7+
libcc2rs::free_refcount(a0)
8+
}
9+
10+
fn f3(a0: usize) -> AnyPtr {
11+
libcc2rs::malloc_refcount(a0)
12+
}
13+
14+
fn f4(a0: AnyPtr, a1: usize) -> AnyPtr {
15+
libcc2rs::realloc_refcount(a0, a1)
16+
}
17+
18+
fn f5(a0: usize, a1: usize) -> AnyPtr {
19+
libcc2rs::calloc_refcount(a0, a1)
20+
}

rules/cstring/tgt_refcount.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ fn f4(a0: AnyPtr, a1: AnyPtr, a2: usize) -> AnyPtr {
2525
unsafe fn f7(a0: Ptr<u8>) -> usize {
2626
a0.to_string_iterator().count()
2727
}
28+
29+
fn f15(a0: Ptr<u8>) -> Ptr<u8> {
30+
libcc2rs::strdup_refcount(a0)
31+
}

rules/src/modules.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ pub mod carray_tgt_refcount;
2626
pub mod carray_tgt_unsafe;
2727
#[path = r#"../cmath/tgt_unsafe.rs"#]
2828
pub mod cmath_tgt_unsafe;
29+
#[path = r#"../cstdlib/tgt_refcount.rs"#]
30+
pub mod cstdlib_tgt_refcount;
2931
#[path = r#"../cstdlib/tgt_unsafe.rs"#]
3032
pub mod cstdlib_tgt_unsafe;
3133
#[path = r#"../cstring/tgt_refcount.rs"#]

tests/unit/malloc_realloc_free.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// no-compile: refcount
1+
// panic: refcount
22
#include <assert.h>
33
#include <stdlib.h>
44

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+
pub fn main() {
10+
std::process::exit(main_0());
11+
}
12+
fn main_0() -> i32 {
13+
let mut __do_while = true;
14+
'loop_: while __do_while || (0 != 0) {
15+
__do_while = false;
16+
let p: Value<Ptr<i32>> = Rc::new(RefCell::new(
17+
libcc2rs::malloc_refcount(::std::mem::size_of::<i32>()).reinterpret_cast::<i32>(),
18+
));
19+
(*p.borrow()).write(42);
20+
assert!((((((*p.borrow()).read()) == 42) as i32) != 0));
21+
libcc2rs::free_refcount(((*p.borrow()).clone() as Ptr<i32>).to_any());
22+
let arr: Value<Ptr<i32>> = Rc::new(RefCell::new(
23+
libcc2rs::malloc_refcount(
24+
(4_usize).wrapping_mul((::std::mem::size_of::<i32>() as usize)),
25+
)
26+
.reinterpret_cast::<i32>(),
27+
));
28+
let i: Value<i32> = Rc::new(RefCell::new(0));
29+
'loop_: while ((((*i.borrow()) < 4) as i32) != 0) {
30+
let __rhs = ((*i.borrow()) * 10);
31+
(*arr.borrow()).offset((*i.borrow()) as isize).write(__rhs);
32+
(*i.borrow_mut()).postfix_inc();
33+
}
34+
assert!((((((*arr.borrow()).offset((0) as isize).read()) == 0) as i32) != 0));
35+
assert!((((((*arr.borrow()).offset((3) as isize).read()) == 30) as i32) != 0));
36+
libcc2rs::free_refcount(((*arr.borrow()).clone() as Ptr<i32>).to_any());
37+
let grow: Value<Ptr<i32>> = Rc::new(RefCell::new(
38+
libcc2rs::malloc_refcount(
39+
(2_usize).wrapping_mul((::std::mem::size_of::<i32>() as usize)),
40+
)
41+
.reinterpret_cast::<i32>(),
42+
));
43+
(*grow.borrow()).offset((0) as isize).write(1);
44+
(*grow.borrow()).offset((1) as isize).write(2);
45+
let __rhs = libcc2rs::realloc_refcount(
46+
((*grow.borrow()).clone() as Ptr<i32>).to_any(),
47+
(4_usize).wrapping_mul((::std::mem::size_of::<i32>() as usize)),
48+
)
49+
.reinterpret_cast::<i32>();
50+
(*grow.borrow_mut()) = __rhs;
51+
(*grow.borrow()).offset((2) as isize).write(3);
52+
(*grow.borrow()).offset((3) as isize).write(4);
53+
assert!((((((*grow.borrow()).offset((0) as isize).read()) == 1) as i32) != 0));
54+
assert!((((((*grow.borrow()).offset((1) as isize).read()) == 2) as i32) != 0));
55+
assert!((((((*grow.borrow()).offset((2) as isize).read()) == 3) as i32) != 0));
56+
assert!((((((*grow.borrow()).offset((3) as isize).read()) == 4) as i32) != 0));
57+
libcc2rs::free_refcount(((*grow.borrow()).clone() as Ptr<i32>).to_any());
58+
let zeros: Value<Ptr<i32>> = Rc::new(RefCell::new(
59+
libcc2rs::calloc_refcount(4_usize, ::std::mem::size_of::<i32>())
60+
.reinterpret_cast::<i32>(),
61+
));
62+
let i: Value<i32> = Rc::new(RefCell::new(0));
63+
'loop_: while ((((*i.borrow()) < 4) as i32) != 0) {
64+
assert!(
65+
(((((*zeros.borrow()).offset((*i.borrow()) as isize).read()) == 0) as i32) != 0)
66+
);
67+
(*i.borrow_mut()).postfix_inc();
68+
}
69+
libcc2rs::free_refcount(((*zeros.borrow()).clone() as Ptr<i32>).to_any());
70+
}
71+
let pmalloc: Value<FnPtr<fn(usize) -> AnyPtr>> =
72+
Rc::new(RefCell::new(FnPtr::<fn(usize) -> AnyPtr>::new(
73+
libcc2rs::malloc_refcount,
74+
)));
75+
let pfree: Value<FnPtr<fn(AnyPtr)>> = Rc::new(RefCell::new(FnPtr::<fn(AnyPtr)>::new(
76+
libcc2rs::free_refcount,
77+
)));
78+
let prealloc: Value<FnPtr<fn(AnyPtr, usize) -> AnyPtr>> =
79+
Rc::new(RefCell::new(FnPtr::<fn(AnyPtr, usize) -> AnyPtr>::new(
80+
libcc2rs::realloc_refcount,
81+
)));
82+
let pcalloc: Value<FnPtr<fn(usize, usize) -> AnyPtr>> =
83+
Rc::new(RefCell::new(FnPtr::<fn(usize, usize) -> AnyPtr>::new(
84+
libcc2rs::calloc_refcount,
85+
)));
86+
let mut __do_while = true;
87+
'loop_: while __do_while || (0 != 0) {
88+
__do_while = false;
89+
let p: Value<Ptr<i32>> = Rc::new(RefCell::new(
90+
({ (*(*pmalloc.borrow()))(::std::mem::size_of::<i32>()) }).reinterpret_cast::<i32>(),
91+
));
92+
(*p.borrow()).write(42);
93+
assert!((((((*p.borrow()).read()) == 42) as i32) != 0));
94+
({ (*(*pfree.borrow()))(((*p.borrow()).clone() as Ptr<i32>).to_any()) });
95+
let arr: Value<Ptr<i32>> = Rc::new(RefCell::new(
96+
({
97+
(*(*pmalloc.borrow()))(
98+
(4_usize).wrapping_mul((::std::mem::size_of::<i32>() as usize)),
99+
)
100+
})
101+
.reinterpret_cast::<i32>(),
102+
));
103+
let i: Value<i32> = Rc::new(RefCell::new(0));
104+
'loop_: while ((((*i.borrow()) < 4) as i32) != 0) {
105+
let __rhs = ((*i.borrow()) * 10);
106+
(*arr.borrow()).offset((*i.borrow()) as isize).write(__rhs);
107+
(*i.borrow_mut()).postfix_inc();
108+
}
109+
assert!((((((*arr.borrow()).offset((0) as isize).read()) == 0) as i32) != 0));
110+
assert!((((((*arr.borrow()).offset((3) as isize).read()) == 30) as i32) != 0));
111+
({ (*(*pfree.borrow()))(((*arr.borrow()).clone() as Ptr<i32>).to_any()) });
112+
let grow: Value<Ptr<i32>> = Rc::new(RefCell::new(
113+
({
114+
(*(*pmalloc.borrow()))(
115+
(2_usize).wrapping_mul((::std::mem::size_of::<i32>() as usize)),
116+
)
117+
})
118+
.reinterpret_cast::<i32>(),
119+
));
120+
(*grow.borrow()).offset((0) as isize).write(1);
121+
(*grow.borrow()).offset((1) as isize).write(2);
122+
let __rhs = ({
123+
(*(*prealloc.borrow()))(
124+
((*grow.borrow()).clone() as Ptr<i32>).to_any(),
125+
(4_usize).wrapping_mul((::std::mem::size_of::<i32>() as usize)),
126+
)
127+
})
128+
.reinterpret_cast::<i32>();
129+
(*grow.borrow_mut()) = __rhs;
130+
(*grow.borrow()).offset((2) as isize).write(3);
131+
(*grow.borrow()).offset((3) as isize).write(4);
132+
assert!((((((*grow.borrow()).offset((0) as isize).read()) == 1) as i32) != 0));
133+
assert!((((((*grow.borrow()).offset((1) as isize).read()) == 2) as i32) != 0));
134+
assert!((((((*grow.borrow()).offset((2) as isize).read()) == 3) as i32) != 0));
135+
assert!((((((*grow.borrow()).offset((3) as isize).read()) == 4) as i32) != 0));
136+
({ (*(*pfree.borrow()))(((*grow.borrow()).clone() as Ptr<i32>).to_any()) });
137+
let zeros: Value<Ptr<i32>> = Rc::new(RefCell::new(
138+
({ (*(*pcalloc.borrow()))(4_usize, ::std::mem::size_of::<i32>()) })
139+
.reinterpret_cast::<i32>(),
140+
));
141+
let i: Value<i32> = Rc::new(RefCell::new(0));
142+
'loop_: while ((((*i.borrow()) < 4) as i32) != 0) {
143+
assert!(
144+
(((((*zeros.borrow()).offset((*i.borrow()) as isize).read()) == 0) as i32) != 0)
145+
);
146+
(*i.borrow_mut()).postfix_inc();
147+
}
148+
({ (*(*pfree.borrow()))(((*zeros.borrow()).clone() as Ptr<i32>).to_any()) });
149+
}
150+
return 0;
151+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
pub struct anon_0 {
10+
__bytes: Value<Box<[u8]>>,
11+
}
12+
impl anon_0 {
13+
pub fn bytes(&self) -> Ptr<u8> {
14+
(self.__bytes.as_pointer() as Ptr<u8>).reinterpret_cast()
15+
}
16+
pub fn aligner(&self) -> Ptr<AnyPtr> {
17+
(self.__bytes.as_pointer() as Ptr<u8>).reinterpret_cast()
18+
}
19+
}
20+
impl Clone for anon_0 {
21+
fn clone(&self) -> Self {
22+
anon_0 {
23+
__bytes: Rc::new(RefCell::new(self.__bytes.borrow().clone())),
24+
}
25+
}
26+
}
27+
impl Default for anon_0 {
28+
fn default() -> Self {
29+
anon_0 {
30+
__bytes: Rc::new(RefCell::new(Box::from([0u8; 8]))),
31+
}
32+
}
33+
}
34+
impl ByteRepr for anon_0 {
35+
fn byte_size() -> usize {
36+
8
37+
}
38+
fn to_bytes(&self, buf: &mut [u8]) {
39+
buf.copy_from_slice(&self.__bytes.borrow());
40+
}
41+
fn from_bytes(buf: &[u8]) -> Self {
42+
anon_0 {
43+
__bytes: Rc::new(RefCell::new(Box::from(buf))),
44+
}
45+
}
46+
}
47+
#[derive(Default)]
48+
pub struct node {
49+
pub len: Value<usize>,
50+
pub pos: Value<usize>,
51+
pub x: Value<anon_0>,
52+
}
53+
impl Clone for node {
54+
fn clone(&self) -> Self {
55+
Self {
56+
len: Rc::new(RefCell::new((*self.len.borrow()).clone())),
57+
pos: Rc::new(RefCell::new((*self.pos.borrow()).clone())),
58+
x: Rc::new(RefCell::new((*self.x.borrow()).clone())),
59+
}
60+
}
61+
}
62+
impl ByteRepr for node {
63+
fn byte_size() -> usize {
64+
24
65+
}
66+
fn to_bytes(&self, buf: &mut [u8]) {
67+
(*self.len.borrow()).to_bytes(&mut buf[0..8]);
68+
(*self.pos.borrow()).to_bytes(&mut buf[8..16]);
69+
(*self.x.borrow()).to_bytes(&mut buf[16..24]);
70+
}
71+
fn from_bytes(buf: &[u8]) -> Self {
72+
Self {
73+
len: Rc::new(RefCell::new(<usize>::from_bytes(&buf[0..8]))),
74+
pos: Rc::new(RefCell::new(<usize>::from_bytes(&buf[8..16]))),
75+
x: Rc::new(RefCell::new(<anon_0>::from_bytes(&buf[16..24]))),
76+
}
77+
}
78+
}
79+
pub fn main() {
80+
std::process::exit(main_0());
81+
}
82+
fn main_0() -> i32 {
83+
let tail_size: Value<usize> = Rc::new(RefCell::new(32_usize));
84+
let n: Value<Ptr<node>> = Rc::new(RefCell::new(
85+
libcc2rs::malloc_refcount(
86+
((24usize as u64).wrapping_add(((*tail_size.borrow()) as u64)) as usize),
87+
)
88+
.reinterpret_cast::<node>(),
89+
));
90+
(*(*(*n.borrow()).upgrade().deref()).len.borrow_mut()) = (*tail_size.borrow());
91+
let i: Value<usize> = Rc::new(RefCell::new(0_usize));
92+
'loop_: while ((((*i.borrow()) < (*tail_size.borrow())) as i32) != 0) {
93+
let __rhs = (((*i.borrow()) & 255_usize) as u8);
94+
((*(*(*n.borrow()).upgrade().deref()).x.borrow())
95+
.bytes()
96+
.reinterpret_cast::<u8>() as Ptr<u8>)
97+
.offset((*i.borrow()) as isize)
98+
.write(__rhs);
99+
(*i.borrow_mut()).postfix_inc();
100+
}
101+
let i: Value<usize> = Rc::new(RefCell::new(0_usize));
102+
'loop_: while ((((*i.borrow()) < (*tail_size.borrow())) as i32) != 0) {
103+
assert!(
104+
((({
105+
let _lhs = ((((*(*(*n.borrow()).upgrade().deref()).x.borrow())
106+
.bytes()
107+
.reinterpret_cast::<u8>() as Ptr<u8>)
108+
.offset((*i.borrow()) as isize)
109+
.read()) as i32);
110+
_lhs == ((((*i.borrow()) & 255_usize) as u8) as i32)
111+
}) as i32)
112+
!= 0)
113+
);
114+
(*i.borrow_mut()).postfix_inc();
115+
}
116+
let p: Value<Ptr<u8>> = Rc::new(RefCell::new(
117+
(((*(*(*n.borrow()).upgrade().deref()).x.borrow())
118+
.bytes()
119+
.reinterpret_cast::<u8>() as Ptr<u8>)
120+
.offset((10) as isize)),
121+
));
122+
assert!(((((((*p.borrow()).read()) as i32) == 10) as i32) != 0));
123+
(*p.borrow()).write(170_u8);
124+
assert!(
125+
(((((((*(*(*n.borrow()).upgrade().deref()).x.borrow())
126+
.bytes()
127+
.reinterpret_cast::<u8>() as Ptr::<u8>)
128+
.offset((10) as isize)
129+
.read()) as i32)
130+
== 170) as i32)
131+
!= 0)
132+
);
133+
(*(*(*n.borrow()).upgrade().deref()).pos.borrow_mut()) = 20_usize;
134+
let q: Value<Ptr<u8>> = Rc::new(RefCell::new(
135+
(((*(*(*n.borrow()).upgrade().deref()).x.borrow())
136+
.bytes()
137+
.reinterpret_cast::<u8>() as Ptr<u8>)
138+
.offset((*(*(*n.borrow()).upgrade().deref()).pos.borrow()) as isize)),
139+
));
140+
assert!(((((((*q.borrow()).read()) as i32) == 20) as i32) != 0));
141+
(*q.borrow()).write(187_u8);
142+
assert!(((((((*q.borrow()).read()) as i32) == 187) as i32) != 0));
143+
libcc2rs::free_refcount(((*n.borrow()).clone() as Ptr<node>).to_any());
144+
return 0;
145+
}

tests/unit/union_flex_array_member.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// no-compile: refcount
1+
// panic: refcount
22
#include <assert.h>
33
#include <stddef.h>
44
#include <stdint.h>

0 commit comments

Comments
 (0)