Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions libcc2rs/src/alloc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

use crate::rc::{AnyPtr, Ptr};

pub fn malloc_refcount(_a0: usize) -> AnyPtr {
todo!("malloc_refcount")
}

pub fn free_refcount(_a0: AnyPtr) {
todo!("free_refcount")
}

pub fn realloc_refcount(_a0: AnyPtr, _a1: usize) -> AnyPtr {
todo!("realloc_refcount")
}

pub fn calloc_refcount(_a0: usize, _a1: usize) -> AnyPtr {
todo!("calloc_refcount")
}

pub fn strdup_refcount(_a0: Ptr<u8>) -> Ptr<u8> {
todo!("strdup_refcount")
}

/// # Safety
///
/// Same contract as C's `malloc`.
Expand Down
20 changes: 20 additions & 0 deletions rules/cstdlib/tgt_refcount.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

use libcc2rs::*;

fn f2(a0: AnyPtr) {
libcc2rs::free_refcount(a0)
}

fn f3(a0: usize) -> AnyPtr {
libcc2rs::malloc_refcount(a0)
}

fn f4(a0: AnyPtr, a1: usize) -> AnyPtr {
libcc2rs::realloc_refcount(a0, a1)
}

fn f5(a0: usize, a1: usize) -> AnyPtr {
libcc2rs::calloc_refcount(a0, a1)
}
4 changes: 4 additions & 0 deletions rules/cstring/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ fn f4(a0: AnyPtr, a1: AnyPtr, a2: usize) -> AnyPtr {
unsafe fn f7(a0: Ptr<u8>) -> usize {
a0.to_string_iterator().count()
}

fn f15(a0: Ptr<u8>) -> Ptr<u8> {
libcc2rs::strdup_refcount(a0)
}
2 changes: 2 additions & 0 deletions rules/src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub mod carray_tgt_refcount;
pub mod carray_tgt_unsafe;
#[path = r#"../cmath/tgt_unsafe.rs"#]
pub mod cmath_tgt_unsafe;
#[path = r#"../cstdlib/tgt_refcount.rs"#]
pub mod cstdlib_tgt_refcount;
#[path = r#"../cstdlib/tgt_unsafe.rs"#]
pub mod cstdlib_tgt_unsafe;
#[path = r#"../cstring/tgt_refcount.rs"#]
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/malloc_realloc_free.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// no-compile: refcount
// panic: refcount
#include <assert.h>
#include <stdlib.h>

Expand Down
151 changes: 151 additions & 0 deletions tests/unit/out/refcount/malloc_realloc_free.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
extern crate libcc2rs;
use libcc2rs::*;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
let mut __do_while = true;
'loop_: while __do_while || (0 != 0) {
__do_while = false;
let p: Value<Ptr<i32>> = Rc::new(RefCell::new(
libcc2rs::malloc_refcount(::std::mem::size_of::<i32>()).reinterpret_cast::<i32>(),
));
(*p.borrow()).write(42);
assert!((((((*p.borrow()).read()) == 42) as i32) != 0));
libcc2rs::free_refcount(((*p.borrow()).clone() as Ptr<i32>).to_any());
let arr: Value<Ptr<i32>> = Rc::new(RefCell::new(
libcc2rs::malloc_refcount(
(4_usize).wrapping_mul((::std::mem::size_of::<i32>() as usize)),
)
.reinterpret_cast::<i32>(),
));
let i: Value<i32> = Rc::new(RefCell::new(0));
'loop_: while ((((*i.borrow()) < 4) as i32) != 0) {
let __rhs = ((*i.borrow()) * 10);
(*arr.borrow()).offset((*i.borrow()) as isize).write(__rhs);
(*i.borrow_mut()).postfix_inc();
}
assert!((((((*arr.borrow()).offset((0) as isize).read()) == 0) as i32) != 0));
assert!((((((*arr.borrow()).offset((3) as isize).read()) == 30) as i32) != 0));
libcc2rs::free_refcount(((*arr.borrow()).clone() as Ptr<i32>).to_any());
let grow: Value<Ptr<i32>> = Rc::new(RefCell::new(
libcc2rs::malloc_refcount(
(2_usize).wrapping_mul((::std::mem::size_of::<i32>() as usize)),
)
.reinterpret_cast::<i32>(),
));
(*grow.borrow()).offset((0) as isize).write(1);
(*grow.borrow()).offset((1) as isize).write(2);
let __rhs = libcc2rs::realloc_refcount(
((*grow.borrow()).clone() as Ptr<i32>).to_any(),
(4_usize).wrapping_mul((::std::mem::size_of::<i32>() as usize)),
)
.reinterpret_cast::<i32>();
(*grow.borrow_mut()) = __rhs;
(*grow.borrow()).offset((2) as isize).write(3);
(*grow.borrow()).offset((3) as isize).write(4);
assert!((((((*grow.borrow()).offset((0) as isize).read()) == 1) as i32) != 0));
assert!((((((*grow.borrow()).offset((1) as isize).read()) == 2) as i32) != 0));
assert!((((((*grow.borrow()).offset((2) as isize).read()) == 3) as i32) != 0));
assert!((((((*grow.borrow()).offset((3) as isize).read()) == 4) as i32) != 0));
libcc2rs::free_refcount(((*grow.borrow()).clone() as Ptr<i32>).to_any());
let zeros: Value<Ptr<i32>> = Rc::new(RefCell::new(
libcc2rs::calloc_refcount(4_usize, ::std::mem::size_of::<i32>())
.reinterpret_cast::<i32>(),
));
let i: Value<i32> = Rc::new(RefCell::new(0));
'loop_: while ((((*i.borrow()) < 4) as i32) != 0) {
assert!(
(((((*zeros.borrow()).offset((*i.borrow()) as isize).read()) == 0) as i32) != 0)
);
(*i.borrow_mut()).postfix_inc();
}
libcc2rs::free_refcount(((*zeros.borrow()).clone() as Ptr<i32>).to_any());
}
let pmalloc: Value<FnPtr<fn(usize) -> AnyPtr>> =
Rc::new(RefCell::new(FnPtr::<fn(usize) -> AnyPtr>::new(
libcc2rs::malloc_refcount,
)));
let pfree: Value<FnPtr<fn(AnyPtr)>> = Rc::new(RefCell::new(FnPtr::<fn(AnyPtr)>::new(
libcc2rs::free_refcount,
)));
let prealloc: Value<FnPtr<fn(AnyPtr, usize) -> AnyPtr>> =
Rc::new(RefCell::new(FnPtr::<fn(AnyPtr, usize) -> AnyPtr>::new(
libcc2rs::realloc_refcount,
)));
let pcalloc: Value<FnPtr<fn(usize, usize) -> AnyPtr>> =
Rc::new(RefCell::new(FnPtr::<fn(usize, usize) -> AnyPtr>::new(
libcc2rs::calloc_refcount,
)));
let mut __do_while = true;
'loop_: while __do_while || (0 != 0) {
__do_while = false;
let p: Value<Ptr<i32>> = Rc::new(RefCell::new(
({ (*(*pmalloc.borrow()))(::std::mem::size_of::<i32>()) }).reinterpret_cast::<i32>(),
));
(*p.borrow()).write(42);
assert!((((((*p.borrow()).read()) == 42) as i32) != 0));
({ (*(*pfree.borrow()))(((*p.borrow()).clone() as Ptr<i32>).to_any()) });
let arr: Value<Ptr<i32>> = Rc::new(RefCell::new(
({
(*(*pmalloc.borrow()))(
(4_usize).wrapping_mul((::std::mem::size_of::<i32>() as usize)),
)
})
.reinterpret_cast::<i32>(),
));
let i: Value<i32> = Rc::new(RefCell::new(0));
'loop_: while ((((*i.borrow()) < 4) as i32) != 0) {
let __rhs = ((*i.borrow()) * 10);
(*arr.borrow()).offset((*i.borrow()) as isize).write(__rhs);
(*i.borrow_mut()).postfix_inc();
}
assert!((((((*arr.borrow()).offset((0) as isize).read()) == 0) as i32) != 0));
assert!((((((*arr.borrow()).offset((3) as isize).read()) == 30) as i32) != 0));
({ (*(*pfree.borrow()))(((*arr.borrow()).clone() as Ptr<i32>).to_any()) });
let grow: Value<Ptr<i32>> = Rc::new(RefCell::new(
({
(*(*pmalloc.borrow()))(
(2_usize).wrapping_mul((::std::mem::size_of::<i32>() as usize)),
)
})
.reinterpret_cast::<i32>(),
));
(*grow.borrow()).offset((0) as isize).write(1);
(*grow.borrow()).offset((1) as isize).write(2);
let __rhs = ({
(*(*prealloc.borrow()))(
((*grow.borrow()).clone() as Ptr<i32>).to_any(),
(4_usize).wrapping_mul((::std::mem::size_of::<i32>() as usize)),
)
})
.reinterpret_cast::<i32>();
(*grow.borrow_mut()) = __rhs;
(*grow.borrow()).offset((2) as isize).write(3);
(*grow.borrow()).offset((3) as isize).write(4);
assert!((((((*grow.borrow()).offset((0) as isize).read()) == 1) as i32) != 0));
assert!((((((*grow.borrow()).offset((1) as isize).read()) == 2) as i32) != 0));
assert!((((((*grow.borrow()).offset((2) as isize).read()) == 3) as i32) != 0));
assert!((((((*grow.borrow()).offset((3) as isize).read()) == 4) as i32) != 0));
({ (*(*pfree.borrow()))(((*grow.borrow()).clone() as Ptr<i32>).to_any()) });
let zeros: Value<Ptr<i32>> = Rc::new(RefCell::new(
({ (*(*pcalloc.borrow()))(4_usize, ::std::mem::size_of::<i32>()) })
.reinterpret_cast::<i32>(),
));
let i: Value<i32> = Rc::new(RefCell::new(0));
'loop_: while ((((*i.borrow()) < 4) as i32) != 0) {
assert!(
(((((*zeros.borrow()).offset((*i.borrow()) as isize).read()) == 0) as i32) != 0)
);
(*i.borrow_mut()).postfix_inc();
}
({ (*(*pfree.borrow()))(((*zeros.borrow()).clone() as Ptr<i32>).to_any()) });
}
return 0;
}
145 changes: 145 additions & 0 deletions tests/unit/out/refcount/union_flex_array_member.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
extern crate libcc2rs;
use libcc2rs::*;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub struct anon_0 {
__bytes: Value<Box<[u8]>>,
}
impl anon_0 {
pub fn bytes(&self) -> Ptr<u8> {
(self.__bytes.as_pointer() as Ptr<u8>).reinterpret_cast()
}
pub fn aligner(&self) -> Ptr<AnyPtr> {
(self.__bytes.as_pointer() as Ptr<u8>).reinterpret_cast()
}
}
impl Clone for anon_0 {
fn clone(&self) -> Self {
anon_0 {
__bytes: Rc::new(RefCell::new(self.__bytes.borrow().clone())),
}
}
}
impl Default for anon_0 {
fn default() -> Self {
anon_0 {
__bytes: Rc::new(RefCell::new(Box::from([0u8; 8]))),
}
}
}
impl ByteRepr for anon_0 {
fn byte_size() -> usize {
8
}
fn to_bytes(&self, buf: &mut [u8]) {
buf.copy_from_slice(&self.__bytes.borrow());
}
fn from_bytes(buf: &[u8]) -> Self {
anon_0 {
__bytes: Rc::new(RefCell::new(Box::from(buf))),
}
}
}
#[derive(Default)]
pub struct node {
pub len: Value<usize>,
pub pos: Value<usize>,
pub x: Value<anon_0>,
}
impl Clone for node {
fn clone(&self) -> Self {
Self {
len: Rc::new(RefCell::new((*self.len.borrow()).clone())),
pos: Rc::new(RefCell::new((*self.pos.borrow()).clone())),
x: Rc::new(RefCell::new((*self.x.borrow()).clone())),
}
}
}
impl ByteRepr for node {
fn byte_size() -> usize {
24
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.len.borrow()).to_bytes(&mut buf[0..8]);
(*self.pos.borrow()).to_bytes(&mut buf[8..16]);
(*self.x.borrow()).to_bytes(&mut buf[16..24]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
len: Rc::new(RefCell::new(<usize>::from_bytes(&buf[0..8]))),
pos: Rc::new(RefCell::new(<usize>::from_bytes(&buf[8..16]))),
x: Rc::new(RefCell::new(<anon_0>::from_bytes(&buf[16..24]))),
}
}
}
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
let tail_size: Value<usize> = Rc::new(RefCell::new(32_usize));
let n: Value<Ptr<node>> = Rc::new(RefCell::new(
libcc2rs::malloc_refcount(
((24usize as u64).wrapping_add(((*tail_size.borrow()) as u64)) as usize),
)
.reinterpret_cast::<node>(),
));
(*(*(*n.borrow()).upgrade().deref()).len.borrow_mut()) = (*tail_size.borrow());
let i: Value<usize> = Rc::new(RefCell::new(0_usize));
'loop_: while ((((*i.borrow()) < (*tail_size.borrow())) as i32) != 0) {
let __rhs = (((*i.borrow()) & 255_usize) as u8);
((*(*(*n.borrow()).upgrade().deref()).x.borrow())
.bytes()
.reinterpret_cast::<u8>() as Ptr<u8>)
.offset((*i.borrow()) as isize)
.write(__rhs);
(*i.borrow_mut()).postfix_inc();
}
let i: Value<usize> = Rc::new(RefCell::new(0_usize));
'loop_: while ((((*i.borrow()) < (*tail_size.borrow())) as i32) != 0) {
assert!(
((({
let _lhs = ((((*(*(*n.borrow()).upgrade().deref()).x.borrow())
.bytes()
.reinterpret_cast::<u8>() as Ptr<u8>)
.offset((*i.borrow()) as isize)
.read()) as i32);
_lhs == ((((*i.borrow()) & 255_usize) as u8) as i32)
}) as i32)
!= 0)
);
(*i.borrow_mut()).postfix_inc();
}
let p: Value<Ptr<u8>> = Rc::new(RefCell::new(
(((*(*(*n.borrow()).upgrade().deref()).x.borrow())
.bytes()
.reinterpret_cast::<u8>() as Ptr<u8>)
.offset((10) as isize)),
));
assert!(((((((*p.borrow()).read()) as i32) == 10) as i32) != 0));
(*p.borrow()).write(170_u8);
assert!(
(((((((*(*(*n.borrow()).upgrade().deref()).x.borrow())
.bytes()
.reinterpret_cast::<u8>() as Ptr::<u8>)
.offset((10) as isize)
.read()) as i32)
== 170) as i32)
!= 0)
);
(*(*(*n.borrow()).upgrade().deref()).pos.borrow_mut()) = 20_usize;
let q: Value<Ptr<u8>> = Rc::new(RefCell::new(
(((*(*(*n.borrow()).upgrade().deref()).x.borrow())
.bytes()
.reinterpret_cast::<u8>() as Ptr<u8>)
.offset((*(*(*n.borrow()).upgrade().deref()).pos.borrow()) as isize)),
));
assert!(((((((*q.borrow()).read()) as i32) == 20) as i32) != 0));
(*q.borrow()).write(187_u8);
assert!(((((((*q.borrow()).read()) as i32) == 187) as i32) != 0));
libcc2rs::free_refcount(((*n.borrow()).clone() as Ptr<node>).to_any());
return 0;
}
2 changes: 1 addition & 1 deletion tests/unit/union_flex_array_member.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// no-compile: refcount
// panic: refcount
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
Expand Down
Loading