Skip to content
Merged
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
2 changes: 1 addition & 1 deletion cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2028,7 +2028,7 @@ bool Converter::VisitBinaryOperator(clang::BinaryOperator *expr) {
std::format("::std::mem::size_of::<{}>()", pointee_type_as_string);
StrCat(size_of_as_string);
}
StrCat(keyword::kAs, "u64");
ConvertCast(expr->getType());
computed_expr_type_ = ComputedExprType::FreshValue;
} else {
ConvertGenericBinaryOperator(expr);
Expand Down
8 changes: 6 additions & 2 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1247,8 +1247,12 @@ bool ConverterRefCount::VisitBinaryOperator(clang::BinaryOperator *expr) {
// fresh pointers
if (expr->isAdditiveOp() && lhs_type->isPointerType() &&
rhs_type->isPointerType()) {
StrCat(ConvertFreshPointer(lhs), expr->getOpcodeStr(),
ConvertFreshPointer(rhs));
{
PushParen paren(*this);
StrCat(ConvertFreshPointer(lhs), expr->getOpcodeStr(),
ConvertFreshPointer(rhs));
}
ConvertCast(expr->getType());
computed_expr_type_ = ComputedExprType::FreshValue;
return false;
}
Expand Down
60 changes: 22 additions & 38 deletions libcc2rs/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,45 +675,29 @@ impl<T> Sub for Ptr<T> {
}
}

impl<T> std::ops::AddAssign<u64> for Ptr<T> {
#[inline]
fn add_assign(&mut self, other: u64) {
let step = self.elem_step();
self.offset = self
.offset
.wrapping_add((other as usize).wrapping_mul(step));
}
}

impl<T> std::ops::AddAssign<i32> for Ptr<T> {
#[inline]
fn add_assign(&mut self, other: i32) {
let step = self.elem_step();
self.offset = self
.offset
.wrapping_add(((other as isize).wrapping_mul(step as isize)) as usize);
}
}

impl<T> std::ops::AddAssign<u32> for Ptr<T> {
#[inline]
fn add_assign(&mut self, other: u32) {
let step = self.elem_step();
self.offset = self
.offset
.wrapping_add((other as usize).wrapping_mul(step));
}
}

impl<T> std::ops::AddAssign<isize> for Ptr<T> {
#[inline]
fn add_assign(&mut self, other: isize) {
let step = self.elem_step();
self.offset = self
.offset
.wrapping_add((other.wrapping_mul(step as isize)) as usize);
}
macro_rules! impl_ptr_add_sub_assign {
($($rhs:ty),+) => { $(
impl<T> std::ops::AddAssign<$rhs> for Ptr<T> {
#[inline]
fn add_assign(&mut self, other: $rhs) {
let step = self.elem_step();
self.offset = self.offset.wrapping_add(
((other as isize).wrapping_mul(step as isize)) as usize,
);
}
}
impl<T> std::ops::SubAssign<$rhs> for Ptr<T> {
#[inline]
fn sub_assign(&mut self, other: $rhs) {
let step = self.elem_step();
self.offset = self.offset.wrapping_sub(
((other as isize).wrapping_mul(step as isize)) as usize,
);
}
}
)+ }
}
impl_ptr_add_sub_assign!(i32, u32, u64, isize);

macro_rules! impl_ptr_add_sub {
($($rhs:ty),+) => { $(
Expand Down
2 changes: 1 addition & 1 deletion tests/ub/out/refcount/ub7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn strlen_0(s: Ptr<u8>) -> u64 {
'loop_: while (((*s.borrow()).read()) != 0) {
(*s.borrow_mut()).prefix_inc();
}
return (((*s.borrow()).clone() - (*begin.borrow()).clone()) as u64);
return ((((*s.borrow()).clone() - (*begin.borrow()).clone()) as i64) as u64);
}
pub fn main() {
std::process::exit(main_0());
Expand Down
2 changes: 1 addition & 1 deletion tests/ub/out/unsafe/ub7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub unsafe fn strlen_0(mut s: *const u8) -> u64 {
'loop_: while ((*s) != 0) {
s.prefix_inc();
}
return ((((s as usize - begin as usize) / ::std::mem::size_of::<u8>()) as u64) as u64);
return ((((s as usize - begin as usize) / ::std::mem::size_of::<u8>()) as i64) as u64);
}
pub fn main() {
unsafe {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/02_address_taken.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main_0() -> i32 {
let __rhs = (((*b_ptr_ptr.borrow()).read()).read());
(*b_ptr.borrow()).write(__rhs);
let offset: Value<u64> = Rc::new(RefCell::new(
(((*b_ptr.borrow()).clone() - (*b_ptr.borrow()).clone()) as u64),
((((*b_ptr.borrow()).clone() - (*b_ptr.borrow()).clone()) as i64) as u64),
));
return (*b.borrow());
}
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/pointer_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ fn main_0() -> i32 {
let p1: Value<Ptr<i32>> = Rc::new(RefCell::new(
((a.as_pointer() as Ptr<i32>).offset(4 as isize)),
));
return (((*p1.borrow()).clone() - (*p0.borrow()).clone()) as i32);
return ((((*p1.borrow()).clone() - (*p0.borrow()).clone()) as i64) as i32);
}
116 changes: 116 additions & 0 deletions tests/unit/out/refcount/pointer_usize_arith.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
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 arr: Value<Box<[i32]>> = Rc::new(RefCell::new(Box::new([10, 11, 12, 13, 14, 15, 16, 17])));
let p: Value<Ptr<i32>> = Rc::new(RefCell::new((arr.as_pointer() as Ptr<i32>)));
let q: Value<Ptr<i32>> = Rc::new(RefCell::new((*p.borrow()).offset((1) as isize)));
assert!((((*q.borrow()).read()) == 11));
let r: Value<Ptr<i32>> = Rc::new(RefCell::new((*p.borrow()).offset((3) as isize)));
assert!((((*r.borrow()).read()) == 13));
let s: Value<Ptr<i32>> = Rc::new(RefCell::new((*r.borrow()).offset(-((2) as isize))));
assert!((((*s.borrow()).read()) == 11));
let diff: Value<i64> = Rc::new(RefCell::new(
((*r.borrow()).clone() - (*p.borrow()).clone()) as i64,
));
assert!(((*diff.borrow()) == 3_i64));
let idx: Value<u64> = Rc::new(RefCell::new(
(((((*r.borrow()).clone() - (*p.borrow()).clone()) as i64) as i64) as u64),
));
assert!(((*idx.borrow()) == 3_u64));
let q2: Value<Ptr<i32>> = Rc::new(RefCell::new((*p.borrow()).clone()));
(*q2.borrow_mut()).prefix_inc();
assert!((((*q2.borrow()).read()) == 11));
(*q2.borrow_mut()).postfix_inc();
assert!((((*q2.borrow()).read()) == 12));
(*q2.borrow_mut()).prefix_dec();
assert!((((*q2.borrow()).read()) == 11));
(*q2.borrow_mut()).postfix_dec();
assert!((((*q2.borrow()).read()) == 10));
assert!({
let _lhs = (*q2.borrow()).clone();
_lhs == (*p.borrow()).clone()
});
let q3: Value<Ptr<i32>> = Rc::new(RefCell::new((*p.borrow()).clone()));
(*q3.borrow_mut()) += 4;
assert!((((*q3.borrow()).read()) == 14));
(*q3.borrow_mut()) -= 2;
assert!((((*q3.borrow()).read()) == 12));
let step: Value<u64> = Rc::new(RefCell::new(2_u64));
let q4: Value<Ptr<i32>> = Rc::new(RefCell::new(
(*p.borrow()).offset((*step.borrow()) as isize),
));
assert!((((*q4.borrow()).read()) == 12));
let v: Value<i32> = Rc::new(RefCell::new(((*p.borrow()).offset((3) as isize).read())));
assert!(((*v.borrow()) == 13));
let v2: Value<i32> = Rc::new(RefCell::new((((*p.borrow()).offset((4) as isize)).read())));
assert!(((*v2.borrow()) == 14));
((*p.borrow()).offset((5) as isize)).write(99);
assert!((((*p.borrow()).offset((5) as isize).read()) == 99));
assert!(((*arr.borrow())[(5) as usize] == 99));
let end: Value<Ptr<i32>> = Rc::new(RefCell::new((*p.borrow()).offset((8) as isize)));
let sum: Value<i32> = Rc::new(RefCell::new(0));
let it: Value<Ptr<i32>> = Rc::new(RefCell::new((*p.borrow()).clone()));
'loop_: while {
let _lhs = (*it.borrow()).clone();
_lhs != (*end.borrow()).clone()
} {
let __rhs = ((*it.borrow()).read());
(*sum.borrow_mut()) += __rhs;
(*it.borrow_mut()).prefix_inc();
}
assert!(((*sum.borrow()) == (((((((10 + 11) + 12) + 13) + 14) + 99) + 16) + 17)));
let bytes: Value<Box<[u8]>> = Rc::new(RefCell::new(Box::new([
0_u8, 1_u8, 2_u8, 3_u8, 4_u8, 5_u8, 6_u8, 7_u8,
])));
let bp: Value<Ptr<u8>> = Rc::new(RefCell::new((bytes.as_pointer() as Ptr<u8>)));
let bq: Value<Ptr<u8>> = Rc::new(RefCell::new((*bp.borrow()).offset((4) as isize)));
assert!(((((*bq.borrow()).read()) as i32) == 4));
let bdiff: Value<i64> = Rc::new(RefCell::new(
((*bq.borrow()).clone() - (*bp.borrow()).clone()) as i64,
));
assert!(((*bdiff.borrow()) == 4_i64));
let cp: Value<Ptr<i32>> = Rc::new(RefCell::new((arr.as_pointer() as Ptr<i32>)));
let cq: Value<Ptr<i32>> = Rc::new(RefCell::new((*cp.borrow()).offset((2) as isize)));
assert!((((*cq.borrow()).read()) == 12));
let cdiff: Value<i64> = Rc::new(RefCell::new(
((*cq.borrow()).clone() - (*cp.borrow()).clone()) as i64,
));
assert!(((*cdiff.borrow()) == 2_i64));
let n: Value<u64> = Rc::new(RefCell::new(3_u64));
let q5: Value<Ptr<i32>> = Rc::new(RefCell::new(
(arr.as_pointer() as Ptr<i32>).offset((*n.borrow()) as isize),
));
assert!((((*q5.borrow()).read()) == 13));
let q6: Value<Ptr<i32>> = Rc::new(RefCell::new(
((arr.as_pointer() as Ptr<i32>).offset((*n.borrow()) as isize)),
));
assert!({
let _lhs = (*q6.borrow()).clone();
_lhs == (*q5.borrow()).clone()
});
let matrix: Value<Box<[Value<Box<[i32]>>]>> = Rc::new(RefCell::new(Box::new([
Rc::new(RefCell::new(Box::new([0, 1, 2, 3]))),
Rc::new(RefCell::new(Box::new([4, 5, 6, 7]))),
Rc::new(RefCell::new(Box::new([8, 9, 10, 11]))),
])));
let row1: Value<Ptr<i32>> = Rc::new(RefCell::new(
((((matrix.as_pointer() as Ptr<Value<Box<[i32]>>>)
.offset(1 as isize)
.read()
.as_pointer()) as Ptr<i32>)
.offset(0 as isize)),
));
assert!((((*row1.borrow()).offset((2) as isize).read()) == 6));
let back: Value<Ptr<i32>> = Rc::new(RefCell::new((*end.borrow()).offset(-((1) as isize))));
assert!((((*back.borrow()).read()) == 17));
return 0;
}
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/strlen_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn strlen_0(s: Ptr<u8>) -> u64 {
'loop_: while (((*s.borrow()).read()) != 0) {
(*s.borrow_mut()).prefix_inc();
}
return (((*s.borrow()).clone() - (*begin.borrow()).clone()) as u64);
return ((((*s.borrow()).clone() - (*begin.borrow()).clone()) as i64) as u64);
}
pub fn main() {
std::process::exit(main_0());
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/unsafe/02_address_taken.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ unsafe fn main_0() -> i32 {
(*(*b_ptr_ptr)) = 4;
(*b_ptr) = (*(*b_ptr_ptr));
let mut offset: u64 =
((((b_ptr as usize - b_ptr as usize) / ::std::mem::size_of::<i32>()) as u64) as u64);
((((b_ptr as usize - b_ptr as usize) / ::std::mem::size_of::<i32>()) as i64) as u64);
return b;
}
2 changes: 1 addition & 1 deletion tests/unit/out/unsafe/pointer_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ unsafe fn main_0() -> i32 {
let mut a: [i32; 5] = [1, 2, 3, 4, 5];
let mut p0: *const i32 = (&mut a[(0) as usize] as *mut i32).cast_const();
let mut p1: *const i32 = (&mut a[(4) as usize] as *mut i32).cast_const();
return ((((p1 as usize - p0 as usize) / ::std::mem::size_of::<i32>()) as u64) as i32);
return ((((p1 as usize - p0 as usize) / ::std::mem::size_of::<i32>()) as i64) as i32);
}
83 changes: 83 additions & 0 deletions tests/unit/out/unsafe/pointer_usize_arith.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
extern crate libc;
use libc::*;
extern crate libcc2rs;
use libcc2rs::*;
use std::collections::BTreeMap;
use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
pub fn main() {
unsafe {
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
let mut arr: [i32; 8] = [10, 11, 12, 13, 14, 15, 16, 17];
let mut p: *mut i32 = arr.as_mut_ptr();
let mut q: *mut i32 = p.offset((1) as isize);
assert!(((*q) == (11)));
let mut r: *mut i32 = p.offset((3) as isize);
assert!(((*r) == (13)));
let mut s: *mut i32 = r.offset(-((2) as isize));
assert!(((*s) == (11)));
let mut diff: i64 = ((r as usize - p as usize) / ::std::mem::size_of::<i32>()) as i64;
assert!(((diff) == (3_i64)));
let mut idx: u64 =
(((((r as usize - p as usize) / ::std::mem::size_of::<i32>()) as i64) as i64) as u64);
assert!(((idx) == (3_u64)));
let mut q2: *mut i32 = p;
q2.prefix_inc();
assert!(((*q2) == (11)));
q2.postfix_inc();
assert!(((*q2) == (12)));
q2.prefix_dec();
assert!(((*q2) == (11)));
q2.postfix_dec();
assert!(((*q2) == (10)));
assert!(((q2) == (p)));
let mut q3: *mut i32 = p;
q3 = (q3).wrapping_add(4 as i32 as usize);
assert!(((*q3) == (14)));
q3 = (q3).wrapping_sub(2 as i32 as usize);
assert!(((*q3) == (12)));
let mut step: u64 = 2_u64;
let mut q4: *mut i32 = p.offset((step) as isize);
assert!(((*q4) == (12)));
let mut v: i32 = (*p.offset((3) as isize));
assert!(((v) == (13)));
let mut v2: i32 = (*(p.offset((4) as isize)));
assert!(((v2) == (14)));
(*(p.offset((5) as isize))) = 99;
assert!(((*p.offset((5) as isize)) == (99)));
assert!(((arr[(5) as usize]) == (99)));
let mut end: *mut i32 = p.offset((8) as isize);
let mut sum: i32 = 0;
let mut it: *mut i32 = p;
'loop_: while ((it) != (end)) {
sum += (*it);
it.prefix_inc();
}
assert!(((sum) == ((((((((10) + (11)) + (12)) + (13)) + (14)) + (99)) + (16)) + (17))));
let mut bytes: [u8; 8] = [0_u8, 1_u8, 2_u8, 3_u8, 4_u8, 5_u8, 6_u8, 7_u8];
let mut bp: *mut u8 = bytes.as_mut_ptr();
let mut bq: *mut u8 = bp.offset((4) as isize);
assert!((((*bq) as i32) == (4)));
let mut bdiff: i64 = ((bq as usize - bp as usize) / ::std::mem::size_of::<u8>()) as i64;
assert!(((bdiff) == (4_i64)));
let mut cp: *const i32 = arr.as_mut_ptr().cast_const();
let mut cq: *const i32 = cp.offset((2) as isize);
assert!(((*cq) == (12)));
let mut cdiff: i64 = ((cq as usize - cp as usize) / ::std::mem::size_of::<i32>()) as i64;
assert!(((cdiff) == (2_i64)));
let mut n: u64 = 3_u64;
let mut q5: *mut i32 = arr.as_mut_ptr().offset((n) as isize);
assert!(((*q5) == (13)));
let mut q6: *mut i32 = (&mut arr[(n) as usize] as *mut i32);
assert!(((q6) == (q5)));
let mut matrix: [[i32; 4]; 3] = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]];
let mut row1: *mut i32 = (&mut matrix[(1) as usize][(0) as usize] as *mut i32);
assert!(((*row1.offset((2) as isize)) == (6)));
let mut back: *mut i32 = end.offset(-((1) as isize));
assert!(((*back) == (17)));
return 0;
}
2 changes: 1 addition & 1 deletion tests/unit/out/unsafe/strlen_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub unsafe fn strlen_0(mut s: *const u8) -> u64 {
'loop_: while ((*s) != 0) {
s.prefix_inc();
}
return ((((s as usize - begin as usize) / ::std::mem::size_of::<u8>()) as u64) as u64);
return ((((s as usize - begin as usize) / ::std::mem::size_of::<u8>()) as i64) as u64);
}
pub fn main() {
unsafe {
Expand Down
Loading
Loading