From 70bf770462f87245733e02796b79e5586aafd454 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 1 Jul 2026 12:34:34 +0100 Subject: [PATCH] Fix expected output --- .../out/refcount/union_addrof_external.rs | 119 +++++++-- .../unit/out/refcount/union_cross_arm_cast.rs | 14 +- .../unit/out/refcount/union_memset_memcpy.rs | 243 ++++++++++++++++++ tests/unit/union_addrof_external.c | 2 +- tests/unit/union_memset_memcpy.c | 2 +- 5 files changed, 344 insertions(+), 36 deletions(-) create mode 100644 tests/unit/out/refcount/union_memset_memcpy.rs diff --git a/tests/unit/out/refcount/union_addrof_external.rs b/tests/unit/out/refcount/union_addrof_external.rs index c8a203d3..a68fdf87 100644 --- a/tests/unit/out/refcount/union_addrof_external.rs +++ b/tests/unit/out/refcount/union_addrof_external.rs @@ -25,18 +25,80 @@ impl Default for record { } } } -impl ByteRepr for record {} -#[derive()] -pub union anon_0 { - pub h: Value, - pub raw_: Value>, +impl ByteRepr for record { + fn byte_size() -> usize { + 16 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.code.borrow()).to_bytes(&mut buf[0..2]); + (*self.lo.borrow()).to_bytes(&mut buf[2..4]); + (*self.hi.borrow()).to_bytes(&mut buf[4..8]); + (*self.pad.borrow()).to_bytes(&mut buf[8..16]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + code: Rc::new(RefCell::new(::from_bytes(&buf[0..2]))), + lo: Rc::new(RefCell::new(::from_bytes(&buf[2..4]))), + hi: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + pad: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + } + } +} +pub struct anon_0 { + __bytes: Value>, +} +impl anon_0 { + pub fn h(&self) -> Ptr { + (self.__bytes.as_pointer() as Ptr).reinterpret_cast() + } + pub fn raw_(&self) -> Ptr> { + (self.__bytes.as_pointer() as Ptr).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; 128]))), + } + } +} +impl ByteRepr for anon_0 { + fn byte_size() -> usize { + 128 + } + 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))), + } + } } -impl ByteRepr for anon_0 {} #[derive(Default)] pub struct Container { pub view: Value, } -impl ByteRepr for Container {} +impl ByteRepr for Container { + fn byte_size() -> usize { + 128 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.view.borrow()).to_bytes(&mut buf[0..128]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + view: Rc::new(RefCell::new(::from_bytes(&buf[0..128]))), + } + } +} pub fn fill_1(out: AnyPtr, cap: usize) { let out: Value = Rc::new(RefCell::new(out)); let cap: Value = Rc::new(RefCell::new(cap)); @@ -67,11 +129,11 @@ pub fn fill_1(out: AnyPtr, cap: usize) { (*src.borrow_mut())[(6) as usize] = 0_u8; (*src.borrow_mut())[(7) as usize] = 1_u8; let n: Value = Rc::new(RefCell::new( - if (((::std::mem::size_of::<[u8; 16]>() < (*cap.borrow())) as i32) != 0) { - ::std::mem::size_of::<[u8; 16]>() + (if (((::std::mem::size_of::<[u8; 16]>() < (*cap.borrow())) as i32) != 0) { + (::std::mem::size_of::<[u8; 16]>() as u64) } else { - (*cap.borrow()) - }, + ((*cap.borrow()) as u64) + } as usize), )); { (*out.borrow()).memcpy( @@ -89,42 +151,47 @@ fn main_0() -> i32 { { ((c.as_pointer()) as Ptr) .to_any() - .memset((0) as u8, ::std::mem::size_of::() as usize); + .memset((0) as u8, 128usize as usize); ((c.as_pointer()) as Ptr).to_any().clone() }; ({ let _out: AnyPtr = ((*c.borrow()).view.as_pointer()).to_any(); - let _cap: usize = ::std::mem::size_of::(); + let _cap: usize = 128usize; fill_1(_out, _cap) }); assert!( - (((((*(*(*(*c.borrow()).view.borrow()).h.borrow()).code.borrow()) as i32) == 2) as i32) + (((((*(*(*(*c.borrow()).view.borrow()).h().upgrade().deref()) + .code + .borrow()) as i32) + == 2) as i32) != 0) ); assert!( - ((((((((*(*(*c.borrow()).view.borrow()).h.borrow()).lo.as_pointer()) - .to_strong() - .as_pointer() as Ptr::) - .offset((0) as isize) - .read()) as i32) + ((((((((*(*(*c.borrow()).view.borrow()).h().upgrade().deref()) + .lo + .as_pointer()) + .reinterpret_cast::()) + .offset((0) as isize) + .read()) as i32) == 0) as i32) != 0) ); assert!( - ((((((((*(*(*c.borrow()).view.borrow()).h.borrow()).lo.as_pointer()) - .to_strong() - .as_pointer() as Ptr::) - .offset((1) as isize) - .read()) as i32) + ((((((((*(*(*c.borrow()).view.borrow()).h().upgrade().deref()) + .lo + .as_pointer()) + .reinterpret_cast::()) + .offset((1) as isize) + .read()) as i32) == 80) as i32) != 0) ); assert!( - (((((*(*(*c.borrow()).view.borrow()).raw_.borrow())[(0) as usize] as i32) == 2) as i32) + ((((((*(*c.borrow()).view.borrow()).raw_().read())[(0) as usize] as i32) == 2) as i32) != 0) ); assert!( - ((((((*(*(*c.borrow()).view.borrow()).raw_.borrow())[(3) as usize] as u8) as i32) == 80) + (((((((*(*c.borrow()).view.borrow()).raw_().read())[(3) as usize] as u8) as i32) == 80) as i32) != 0) ); diff --git a/tests/unit/out/refcount/union_cross_arm_cast.rs b/tests/unit/out/refcount/union_cross_arm_cast.rs index 0cbc548c..ee56415e 100644 --- a/tests/unit/out/refcount/union_cross_arm_cast.rs +++ b/tests/unit/out/refcount/union_cross_arm_cast.rs @@ -182,19 +182,17 @@ fn main_0() -> i32 { .borrow_mut()) = 8080_u16; assert!( ((((((((*(*c.borrow()).u.borrow()).raw_().reinterpret_cast::()) - .to_strong() - .as_pointer() as Ptr::) - .offset((2) as isize) - .read()) as i32) + .reinterpret_cast::()) + .offset((2) as isize) + .read()) as i32) == 144) as i32) != 0) ); assert!( ((((((((*(*c.borrow()).u.borrow()).raw_().reinterpret_cast::()) - .to_strong() - .as_pointer() as Ptr::) - .offset((3) as isize) - .read()) as i32) + .reinterpret_cast::()) + .offset((3) as isize) + .read()) as i32) == 31) as i32) != 0) ); diff --git a/tests/unit/out/refcount/union_memset_memcpy.rs b/tests/unit/out/refcount/union_memset_memcpy.rs new file mode 100644 index 00000000..95de7573 --- /dev/null +++ b/tests/unit/out/refcount/union_memset_memcpy.rs @@ -0,0 +1,243 @@ +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}; +#[derive()] +pub struct shape_a { + pub code: Value, + pub pad: Value>, +} +impl Default for shape_a { + fn default() -> Self { + shape_a { + code: >::default(), + pad: Rc::new(RefCell::new( + (0..14).map(|_| ::default()).collect::>(), + )), + } + } +} +impl ByteRepr for shape_a { + fn byte_size() -> usize { + 16 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.code.borrow()).to_bytes(&mut buf[0..2]); + (*self.pad.borrow()).to_bytes(&mut buf[2..16]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + code: Rc::new(RefCell::new(::from_bytes(&buf[0..2]))), + pad: Rc::new(RefCell::new(>::from_bytes(&buf[2..16]))), + } + } +} +#[derive()] +pub struct shape_b { + pub code: Value, + pub lo: Value, + pub hi: Value, + pub fill: Value>, +} +impl Default for shape_b { + fn default() -> Self { + shape_b { + code: >::default(), + lo: >::default(), + hi: >::default(), + fill: Rc::new(RefCell::new( + (0..8).map(|_| ::default()).collect::>(), + )), + } + } +} +impl ByteRepr for shape_b { + fn byte_size() -> usize { + 16 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.code.borrow()).to_bytes(&mut buf[0..2]); + (*self.lo.borrow()).to_bytes(&mut buf[2..4]); + (*self.hi.borrow()).to_bytes(&mut buf[4..8]); + (*self.fill.borrow()).to_bytes(&mut buf[8..16]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + code: Rc::new(RefCell::new(::from_bytes(&buf[0..2]))), + lo: Rc::new(RefCell::new(::from_bytes(&buf[2..4]))), + hi: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + fill: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + } + } +} +pub struct anon_0 { + __bytes: Value>, +} +impl anon_0 { + pub fn a(&self) -> Ptr { + (self.__bytes.as_pointer() as Ptr).reinterpret_cast() + } + pub fn b(&self) -> Ptr { + (self.__bytes.as_pointer() as Ptr).reinterpret_cast() + } + pub fn raw_(&self) -> Ptr> { + (self.__bytes.as_pointer() as Ptr).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; 256]))), + } + } +} +impl ByteRepr for anon_0 { + fn byte_size() -> usize { + 256 + } + 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 Container { + pub view: Value, +} +impl ByteRepr for Container { + fn byte_size() -> usize { + 256 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.view.borrow()).to_bytes(&mut buf[0..256]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + view: Rc::new(RefCell::new(::from_bytes(&buf[0..256]))), + } + } +} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let c: Value = >::default(); + { + ((c.as_pointer()) as Ptr) + .to_any() + .memset((0) as u8, 256usize as usize); + ((c.as_pointer()) as Ptr).to_any().clone() + }; + assert!( + (((((*(*(*(*c.borrow()).view.borrow()).a().upgrade().deref()) + .code + .borrow()) as i32) + == 0) as i32) + != 0) + ); + assert!( + (((((*(*(*(*c.borrow()).view.borrow()).b().upgrade().deref()) + .lo + .borrow()) as i32) + == 0) as i32) + != 0) + ); + assert!( + ((((((*(*c.borrow()).view.borrow()).raw_().read())[(0) as usize] as i32) == 0) as i32) + != 0) + ); + assert!( + ((((((*(*c.borrow()).view.borrow()).raw_().read())[(255) as usize] as i32) == 0) as i32) + != 0) + ); + let src: Value> = Rc::new(RefCell::new(Box::new([ + 0_u8, + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ]))); + (*src.borrow_mut())[(0) as usize] = 2_u8; + (*src.borrow_mut())[(2) as usize] = 80_u8; + (*src.borrow_mut())[(3) as usize] = 0_u8; + (*src.borrow_mut())[(4) as usize] = 127_u8; + (*src.borrow_mut())[(5) as usize] = 0_u8; + (*src.borrow_mut())[(6) as usize] = 0_u8; + (*src.borrow_mut())[(7) as usize] = 1_u8; + let len: Value = Rc::new(RefCell::new(16_usize)); + assert!(((((*len.borrow()) <= ::std::mem::size_of::<[u8; 256]>()) as i32) != 0)); + { + (((*(*c.borrow()).view.borrow()) + .raw_() + .reinterpret_cast::()) + .clone() as Ptr) + .to_any() + .memcpy( + &((src.as_pointer() as Ptr) as Ptr).to_any(), + (*len.borrow()) as usize, + ); + (((*(*c.borrow()).view.borrow()) + .raw_() + .reinterpret_cast::()) + .clone() as Ptr) + .to_any() + .clone() + }; + assert!( + (((((*(*(*(*c.borrow()).view.borrow()).b().upgrade().deref()) + .code + .borrow()) as i32) + == 2) as i32) + != 0) + ); + assert!( + ((((((((*(*(*c.borrow()).view.borrow()).b().upgrade().deref()) + .lo + .as_pointer()) + .reinterpret_cast::()) + .offset((0) as isize) + .read()) as i32) + == 80) as i32) + != 0) + ); + { + ((c.as_pointer()) as Ptr) + .to_any() + .memset((0) as u8, 256usize as usize); + ((c.as_pointer()) as Ptr).to_any().clone() + }; + assert!( + (((((*(*(*(*c.borrow()).view.borrow()).b().upgrade().deref()) + .code + .borrow()) as i32) + == 0) as i32) + != 0) + ); + return 0; +} diff --git a/tests/unit/union_addrof_external.c b/tests/unit/union_addrof_external.c index 59dc281b..9682f467 100644 --- a/tests/unit/union_addrof_external.c +++ b/tests/unit/union_addrof_external.c @@ -1,4 +1,4 @@ -// no-compile: refcount +// panic: refcount #include #include #include diff --git a/tests/unit/union_memset_memcpy.c b/tests/unit/union_memset_memcpy.c index b4a270b6..175a1f47 100644 --- a/tests/unit/union_memset_memcpy.c +++ b/tests/unit/union_memset_memcpy.c @@ -1,4 +1,4 @@ -// no-compile: refcount +// panic: refcount #include #include #include