From 2421985e2e383aba2aab988eee275e19cc079e01 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 16 Jul 2026 15:15:59 +0100 Subject: [PATCH 1/6] Add unistd safe rules --- tests/unit/out/refcount/stdcopy_ostream.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/unit/out/refcount/stdcopy_ostream.rs b/tests/unit/out/refcount/stdcopy_ostream.rs index 9ae7dd16..3470dda5 100644 --- a/tests/unit/out/refcount/stdcopy_ostream.rs +++ b/tests/unit/out/refcount/stdcopy_ostream.rs @@ -29,7 +29,15 @@ fn main_0() -> i32 { ); (*ofs.borrow_mut()).try_clone().unwrap() }; - match nix::unistd::unlink((file.as_pointer() as Ptr).to_rust_string().as_str()) { + match nix::unistd::unlink( + ::std::ffi::CString::new( + (file.as_pointer() as Ptr) + .to_c_string_iterator() + .collect::>(), + ) + .unwrap() + .as_c_str(), + ) { Ok(()) => 0, Err(__e) => { libcc2rs::cpp2rust_errno().write(__e as i32); From 5586ba53dded3f7b18fad712f250cf8788f4c6d9 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 16 Jul 2026 15:20:43 +0100 Subject: [PATCH 2/6] Use to_rust_string in unlink --- tests/unit/out/refcount/stdcopy_ostream.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/tests/unit/out/refcount/stdcopy_ostream.rs b/tests/unit/out/refcount/stdcopy_ostream.rs index 3470dda5..9ae7dd16 100644 --- a/tests/unit/out/refcount/stdcopy_ostream.rs +++ b/tests/unit/out/refcount/stdcopy_ostream.rs @@ -29,15 +29,7 @@ fn main_0() -> i32 { ); (*ofs.borrow_mut()).try_clone().unwrap() }; - match nix::unistd::unlink( - ::std::ffi::CString::new( - (file.as_pointer() as Ptr) - .to_c_string_iterator() - .collect::>(), - ) - .unwrap() - .as_c_str(), - ) { + match nix::unistd::unlink((file.as_pointer() as Ptr).to_rust_string().as_str()) { Ok(()) => 0, Err(__e) => { libcc2rs::cpp2rust_errno().write(__e as i32); From 43d5844783a7d48c8fa77effe534ba2d16427105 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 16 Jul 2026 21:28:11 +0100 Subject: [PATCH 3/6] Add send/recv safe rules --- libcc2rs/src/rc.rs | 58 ++++ rules/socket/tgt_refcount.rs | 58 ++++ tests/unit/out/refcount/socket_send_recv.rs | 276 ++++++++++++++++++++ tests/unit/out/unsafe/socket_send_recv.rs | 154 +++++++++++ tests/unit/socket_send_recv.c | 41 +++ 5 files changed, 587 insertions(+) create mode 100644 tests/unit/out/refcount/socket_send_recv.rs create mode 100644 tests/unit/out/unsafe/socket_send_recv.rs create mode 100644 tests/unit/socket_send_recv.c diff --git a/libcc2rs/src/rc.rs b/libcc2rs/src/rc.rs index 30bc5ef2..6f6941e5 100644 --- a/libcc2rs/src/rc.rs +++ b/libcc2rs/src/rc.rs @@ -950,6 +950,64 @@ thread_local! { } impl Ptr { + pub fn with_slice_mut(&self, len: usize, f: impl FnOnce(&mut [u8]) -> R) -> R { + let off = self.offset; + match &self.kind { + PtrKind::Null => panic!("ub: null pointer"), + PtrKind::StackSingle(weak) | PtrKind::HeapSingle(weak) => { + assert!(off == 0 && len <= 1, "ub: with_slice_mut out of bounds"); + let rc = weak.upgrade().expect("ub: dangling pointer"); + let mut b = rc.borrow_mut(); + f(&mut std::slice::from_mut(&mut *b)[..len]) + } + PtrKind::StackArray(weak) | PtrKind::HeapArray(weak) => { + let rc = weak.upgrade().expect("ub: dangling pointer"); + let mut b = rc.borrow_mut(); + f(&mut b[off..off + len]) + } + PtrKind::Vec(weak) => { + let rc = weak.upgrade().expect("ub: dangling pointer"); + let mut b = rc.borrow_mut(); + f(&mut b[off..off + len]) + } + PtrKind::Reinterpreted(data) => { + let mut buf = vec![0u8; len]; + data.alloc.read_bytes(off, &mut buf); + let r = f(&mut buf); + data.alloc.write_bytes(off, &buf); + r + } + } + } + + pub fn with_slice(&self, len: usize, f: impl FnOnce(&[u8]) -> R) -> R { + let off = self.offset; + match &self.kind { + PtrKind::Null => panic!("ub: null pointer"), + PtrKind::StackSingle(weak) | PtrKind::HeapSingle(weak) => { + assert!(off == 0 && len <= 1, "ub: with_slice out of bounds"); + let rc = weak.upgrade().expect("ub: dangling pointer"); + let b = rc.borrow(); + f(&std::slice::from_ref(&*b)[..len]) + } + PtrKind::StackArray(weak) | PtrKind::HeapArray(weak) => { + let rc = weak.upgrade().expect("ub: dangling pointer"); + let b = rc.borrow(); + f(&b[off..off + len]) + } + PtrKind::Vec(weak) => { + let rc = weak.upgrade().expect("ub: dangling pointer"); + let b = rc.borrow(); + f(&b[off..off + len]) + } + PtrKind::Reinterpreted(data) => { + let mut buf = vec![0u8; len]; + data.alloc.read_bytes(off, &mut buf); + f(&buf) + } + } + } + #[allow(clippy::explicit_counter_loop)] pub fn memcpy(&self, src: &Self, len: usize) { let mut dst = self.clone(); diff --git a/rules/socket/tgt_refcount.rs b/rules/socket/tgt_refcount.rs index 8b1f7cd0..315d3138 100644 --- a/rules/socket/tgt_refcount.rs +++ b/rules/socket/tgt_refcount.rs @@ -14,3 +14,61 @@ fn t2() -> libcc2rs::SockaddrStorage { fn t3() -> libcc2rs::SockaddrUn { Default::default() } + +fn f11(a0: i32, a1: i32, a2: i32, a3: Ptr) -> i32 { + use nix::sys::socket::{AddressFamily, SockFlag, SockProtocol, SockType}; + let __out = a3.clone(); + let __flag_bits = ::libc::SOCK_CLOEXEC | ::libc::SOCK_NONBLOCK; + match ( + AddressFamily::from_i32(a0), + SockType::try_from(a1 & !__flag_bits), + ) { + (Some(__f), Ok(__t)) => { + let __flags = SockFlag::from_bits_truncate(a1 & __flag_bits); + let __proto: Option = None; + match nix::sys::socket::socketpair(__f, __t, __proto, __flags) { + Ok((__a, __b)) => { + __out.write(::std::os::fd::IntoRawFd::into_raw_fd(__a)); + __out + .offset(1) + .write(::std::os::fd::IntoRawFd::into_raw_fd(__b)); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } + _ => { + libcc2rs::cpp2rust_errno().write(::libc::EINVAL); + -1 + } + } +} + +fn f9(a0: i32, a1: AnyPtr, a2: usize, a3: i32) -> isize { + let __buf = a1.reinterpret_cast::(); + match __buf.with_slice_mut(a2, |__s| { + nix::sys::socket::recv(a0, __s, nix::sys::socket::MsgFlags::from_bits_truncate(a3)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } +} + +fn f10(a0: i32, a1: AnyPtr, a2: usize, a3: i32) -> isize { + let __buf = a1.reinterpret_cast::(); + match __buf.with_slice(a2, |__s| { + nix::sys::socket::send(a0, __s, nix::sys::socket::MsgFlags::from_bits_truncate(a3)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } +} diff --git a/tests/unit/out/refcount/socket_send_recv.rs b/tests/unit/out/refcount/socket_send_recv.rs new file mode 100644 index 00000000..7789fc16 --- /dev/null +++ b/tests/unit/out/refcount/socket_send_recv.rs @@ -0,0 +1,276 @@ +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 test_send_recv_0() { + let sv: Value> = Rc::new(RefCell::new( + (0..2).map(|_| ::default()).collect::>(), + )); + assert!( + ((({ + use nix::sys::socket::{AddressFamily, SockFlag, SockProtocol, SockType}; + let __out = (sv.as_pointer() as Ptr).clone(); + let __flag_bits = ::libc::SOCK_CLOEXEC | ::libc::SOCK_NONBLOCK; + match ( + AddressFamily::from_i32(1), + SockType::try_from(libc::SOCK_STREAM & !__flag_bits), + ) { + (Some(__f), Ok(__t)) => { + let __flags = SockFlag::from_bits_truncate(libc::SOCK_STREAM & __flag_bits); + let __proto: Option = None; + match nix::sys::socket::socketpair(__f, __t, __proto, __flags) { + Ok((__a, __b)) => { + __out.write(::std::os::fd::IntoRawFd::into_raw_fd(__a)); + __out + .offset(1) + .write(::std::os::fd::IntoRawFd::into_raw_fd(__b)); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } + _ => { + libcc2rs::cpp2rust_errno().write(::libc::EINVAL); + -1 + } + } + } == 0) as i32) + != 0) + ); + let msg: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"ping"))); + assert!( + ((({ + let __buf = ((*msg.borrow()).clone() as Ptr) + .to_any() + .reinterpret_cast::(); + match __buf.with_slice(4_usize, |__s| { + nix::sys::socket::send( + (*sv.borrow())[(0) as usize], + __s, + nix::sys::socket::MsgFlags::from_bits_truncate(0), + ) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == 4_isize) as i32) + != 0) + ); + let buf: Value> = Rc::new(RefCell::new(Box::new([ + 0_u8, + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ]))); + assert!( + ((({ + let __buf = ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::(); + match __buf.with_slice_mut(8_usize, |__s| { + nix::sys::socket::recv( + (*sv.borrow())[(1) as usize], + __s, + nix::sys::socket::MsgFlags::from_bits_truncate(0), + ) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == 4_isize) as i32) + != 0) + ); + assert!( + (((((buf.as_pointer() as Ptr::) as Ptr::) + .to_any() + .memcmp(&Ptr::from_string_literal(b"ping").to_any(), 4_usize) + == 0) as i32) + != 0) + ); + assert!( + ((({ + let __buf = Ptr::from_string_literal(b"pong!") + .to_any() + .reinterpret_cast::(); + match __buf.with_slice(5_usize, |__s| { + nix::sys::socket::send( + (*sv.borrow())[(1) as usize], + __s, + nix::sys::socket::MsgFlags::from_bits_truncate(0), + ) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == 5_isize) as i32) + != 0) + ); + assert!( + ((({ + let __buf = ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::(); + match __buf.with_slice_mut(8_usize, |__s| { + nix::sys::socket::recv( + (*sv.borrow())[(0) as usize], + __s, + nix::sys::socket::MsgFlags::from_bits_truncate(0), + ) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == 5_isize) as i32) + != 0) + ); + assert!( + (((((buf.as_pointer() as Ptr::) as Ptr::) + .to_any() + .memcmp(&Ptr::from_string_literal(b"pong!").to_any(), 5_usize) + == 0) as i32) + != 0) + ); +} +pub fn test_send_recv_scalar_1() { + let sv: Value> = Rc::new(RefCell::new( + (0..2).map(|_| ::default()).collect::>(), + )); + assert!( + ((({ + use nix::sys::socket::{AddressFamily, SockFlag, SockProtocol, SockType}; + let __out = (sv.as_pointer() as Ptr).clone(); + let __flag_bits = ::libc::SOCK_CLOEXEC | ::libc::SOCK_NONBLOCK; + match ( + AddressFamily::from_i32(1), + SockType::try_from(libc::SOCK_STREAM & !__flag_bits), + ) { + (Some(__f), Ok(__t)) => { + let __flags = SockFlag::from_bits_truncate(libc::SOCK_STREAM & __flag_bits); + let __proto: Option = None; + match nix::sys::socket::socketpair(__f, __t, __proto, __flags) { + Ok((__a, __b)) => { + __out.write(::std::os::fd::IntoRawFd::into_raw_fd(__a)); + __out + .offset(1) + .write(::std::os::fd::IntoRawFd::into_raw_fd(__b)); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } + _ => { + libcc2rs::cpp2rust_errno().write(::libc::EINVAL); + -1 + } + } + } == 0) as i32) + != 0) + ); + let value: Value = Rc::new(RefCell::new(42)); + assert!( + (((({ + let __buf = ((value.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::(); + match __buf.with_slice(::std::mem::size_of::(), |__s| { + nix::sys::socket::send( + (*sv.borrow())[(0) as usize], + __s, + nix::sys::socket::MsgFlags::from_bits_truncate(0), + ) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } as usize) + == ::std::mem::size_of::()) as i32) + != 0) + ); + let received: Value = Rc::new(RefCell::new(0)); + assert!( + (((({ + let __buf = ((received.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::(); + match __buf.with_slice_mut(::std::mem::size_of::(), |__s| { + nix::sys::socket::recv( + (*sv.borrow())[(1) as usize], + __s, + nix::sys::socket::MsgFlags::from_bits_truncate(0), + ) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } as usize) + == ::std::mem::size_of::()) as i32) + != 0) + ); + assert!(((((*received.borrow()) == 42) as i32) != 0)); +} +pub fn test_send_bad_fd_2() { + libcc2rs::cpp2rust_errno().write(0); + assert!( + ((({ + let __buf = Ptr::from_string_literal(b"x") + .to_any() + .reinterpret_cast::(); + match __buf.with_slice(1_usize, |__s| { + nix::sys::socket::send( + -1_i32, + __s, + nix::sys::socket::MsgFlags::from_bits_truncate(0), + ) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == (-1_i32 as isize)) as i32) + != 0) + ); + assert!(((((libcc2rs::cpp2rust_errno().read()) == 9) as i32) != 0)); +} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_send_recv_0() }); + ({ test_send_recv_scalar_1() }); + ({ test_send_bad_fd_2() }); + return 0; +} diff --git a/tests/unit/out/unsafe/socket_send_recv.rs b/tests/unit/out/unsafe/socket_send_recv.rs new file mode 100644 index 00000000..97aacac8 --- /dev/null +++ b/tests/unit/out/unsafe/socket_send_recv.rs @@ -0,0 +1,154 @@ +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 unsafe fn test_send_recv_0() { + let mut sv: [i32; 2] = [0_i32; 2]; + assert!(((((libc::socketpair(1, libc::SOCK_STREAM, 0, sv.as_mut_ptr())) == (0)) as i32) != 0)); + let mut msg: *const libc::c_char = (c"ping".as_ptr().cast_mut()).cast_const(); + assert!( + ((((libc::send( + sv[(0) as usize], + (msg as *const libc::c_char as *const ::libc::c_void), + 4_usize, + 0 + )) == (4_isize)) as i32) + != 0) + ); + let mut buf: [libc::c_char; 8] = [ + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + ]; + assert!( + ((((libc::recv( + sv[(1) as usize], + (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), + 8_usize, + 0 + )) == (4_isize)) as i32) + != 0) + ); + assert!( + (((({ + let sa = core::slice::from_raw_parts( + (buf.as_mut_ptr() as *const libc::c_char as *const ::libc::c_void) as *const u8, + 4_usize as usize, + ); + let sb = core::slice::from_raw_parts( + (c"ping".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void) + as *const u8, + 4_usize as usize, + ); + let mut diff = 0_i32; + for (x, y) in sa.iter().zip(sb.iter()) { + if x != y { + diff = (*x as i32) - (*y as i32); + break; + } + } + diff + }) == (0)) as i32) + != 0) + ); + assert!( + ((((libc::send( + sv[(1) as usize], + (c"pong!".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), + 5_usize, + 0 + )) == (5_isize)) as i32) + != 0) + ); + assert!( + ((((libc::recv( + sv[(0) as usize], + (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), + 8_usize, + 0 + )) == (5_isize)) as i32) + != 0) + ); + assert!( + (((({ + let sa = core::slice::from_raw_parts( + (buf.as_mut_ptr() as *const libc::c_char as *const ::libc::c_void) as *const u8, + 5_usize as usize, + ); + let sb = core::slice::from_raw_parts( + (c"pong!".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void) + as *const u8, + 5_usize as usize, + ); + let mut diff = 0_i32; + for (x, y) in sa.iter().zip(sb.iter()) { + if x != y { + diff = (*x as i32) - (*y as i32); + break; + } + } + diff + }) == (0)) as i32) + != 0) + ); +} +pub unsafe fn test_send_recv_scalar_1() { + let mut sv: [i32; 2] = [0_i32; 2]; + assert!(((((libc::socketpair(1, libc::SOCK_STREAM, 0, sv.as_mut_ptr())) == (0)) as i32) != 0)); + let mut value: i32 = 42; + assert!( + ((((libc::send( + sv[(0) as usize], + ((&mut value as *mut i32) as *const i32 as *const ::libc::c_void), + ::std::mem::size_of::(), + 0 + ) as usize) + == (::std::mem::size_of::())) as i32) + != 0) + ); + let mut received: i32 = 0; + assert!( + ((((libc::recv( + sv[(1) as usize], + ((&mut received as *mut i32) as *mut i32 as *mut ::libc::c_void), + ::std::mem::size_of::(), + 0 + ) as usize) + == (::std::mem::size_of::())) as i32) + != 0) + ); + assert!(((((received) == (42)) as i32) != 0)); +} +pub unsafe fn test_send_bad_fd_2() { + (*libcc2rs::cpp2rust_errno_unsafe()) = 0; + assert!( + ((((libc::send( + -1_i32, + (c"x".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), + 1_usize, + 0 + )) == (-1_i32 as isize)) as i32) + != 0) + ); + assert!(((((*libcc2rs::cpp2rust_errno_unsafe()) == (9)) as i32) != 0)); +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_send_recv_0() }); + (unsafe { test_send_recv_scalar_1() }); + (unsafe { test_send_bad_fd_2() }); + return 0; +} diff --git a/tests/unit/socket_send_recv.c b/tests/unit/socket_send_recv.c new file mode 100644 index 00000000..f85a08d5 --- /dev/null +++ b/tests/unit/socket_send_recv.c @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include + +static void test_send_recv(void) { + int sv[2]; + assert(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0); + const char *msg = "ping"; + assert(send(sv[0], msg, 4, 0) == 4); + char buf[8] = {0}; + assert(recv(sv[1], buf, 8, 0) == 4); + assert(memcmp(buf, "ping", 4) == 0); + assert(send(sv[1], "pong!", 5, 0) == 5); + assert(recv(sv[0], buf, 8, 0) == 5); + assert(memcmp(buf, "pong!", 5) == 0); +} + +static void test_send_recv_scalar(void) { + int sv[2]; + assert(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0); + int value = 42; + assert(send(sv[0], &value, sizeof(value), 0) == sizeof(value)); + int received = 0; + assert(recv(sv[1], &received, sizeof(received), 0) == sizeof(received)); + assert(received == 42); +} + +static void test_send_bad_fd(void) { + errno = 0; + assert(send(-1, "x", 1, 0) == -1); + assert(errno == EBADF); +} + +int main(void) { + test_send_recv(); + test_send_recv_scalar(); + test_send_bad_fd(); + return 0; +} From de1691cc1f824f146fdab95043a14d1790478b05 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 16 Jul 2026 21:43:26 +0100 Subject: [PATCH 4/6] Add macos version of socketpair --- rules/socket/tgt_refcount.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/rules/socket/tgt_refcount.rs b/rules/socket/tgt_refcount.rs index 315d3138..5cdfc4da 100644 --- a/rules/socket/tgt_refcount.rs +++ b/rules/socket/tgt_refcount.rs @@ -15,6 +15,7 @@ fn t3() -> libcc2rs::SockaddrUn { Default::default() } +#[cfg(target_os = "linux")] fn f11(a0: i32, a1: i32, a2: i32, a3: Ptr) -> i32 { use nix::sys::socket::{AddressFamily, SockFlag, SockProtocol, SockType}; let __out = a3.clone(); @@ -47,6 +48,34 @@ fn f11(a0: i32, a1: i32, a2: i32, a3: Ptr) -> i32 { } } +#[cfg(target_os = "macos")] +fn f11(a0: i32, a1: i32, a2: i32, a3: Ptr) -> i32 { + use nix::sys::socket::{AddressFamily, SockFlag, SockProtocol, SockType}; + let __out = a3.clone(); + match (AddressFamily::from_i32(a0), SockType::try_from(a1)) { + (Some(__f), Ok(__t)) => { + let __proto: Option = None; + match nix::sys::socket::socketpair(__f, __t, __proto, SockFlag::empty()) { + Ok((__a, __b)) => { + __out.write(::std::os::fd::IntoRawFd::into_raw_fd(__a)); + __out + .offset(1) + .write(::std::os::fd::IntoRawFd::into_raw_fd(__b)); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } + _ => { + libcc2rs::cpp2rust_errno().write(::libc::EINVAL); + -1 + } + } +} + fn f9(a0: i32, a1: AnyPtr, a2: usize, a3: i32) -> isize { let __buf = a1.reinterpret_cast::(); match __buf.with_slice_mut(a2, |__s| { From ab1417e20aaf3c876629aa6db4c602b25b1ee685 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 16 Jul 2026 21:43:46 +0100 Subject: [PATCH 5/6] Delete unportable test --- tests/unit/out/refcount/socket_send_recv.rs | 276 -------------------- tests/unit/out/unsafe/socket_send_recv.rs | 154 ----------- tests/unit/socket_send_recv.c | 41 --- 3 files changed, 471 deletions(-) delete mode 100644 tests/unit/out/refcount/socket_send_recv.rs delete mode 100644 tests/unit/out/unsafe/socket_send_recv.rs delete mode 100644 tests/unit/socket_send_recv.c diff --git a/tests/unit/out/refcount/socket_send_recv.rs b/tests/unit/out/refcount/socket_send_recv.rs deleted file mode 100644 index 7789fc16..00000000 --- a/tests/unit/out/refcount/socket_send_recv.rs +++ /dev/null @@ -1,276 +0,0 @@ -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 test_send_recv_0() { - let sv: Value> = Rc::new(RefCell::new( - (0..2).map(|_| ::default()).collect::>(), - )); - assert!( - ((({ - use nix::sys::socket::{AddressFamily, SockFlag, SockProtocol, SockType}; - let __out = (sv.as_pointer() as Ptr).clone(); - let __flag_bits = ::libc::SOCK_CLOEXEC | ::libc::SOCK_NONBLOCK; - match ( - AddressFamily::from_i32(1), - SockType::try_from(libc::SOCK_STREAM & !__flag_bits), - ) { - (Some(__f), Ok(__t)) => { - let __flags = SockFlag::from_bits_truncate(libc::SOCK_STREAM & __flag_bits); - let __proto: Option = None; - match nix::sys::socket::socketpair(__f, __t, __proto, __flags) { - Ok((__a, __b)) => { - __out.write(::std::os::fd::IntoRawFd::into_raw_fd(__a)); - __out - .offset(1) - .write(::std::os::fd::IntoRawFd::into_raw_fd(__b)); - 0 - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } - _ => { - libcc2rs::cpp2rust_errno().write(::libc::EINVAL); - -1 - } - } - } == 0) as i32) - != 0) - ); - let msg: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"ping"))); - assert!( - ((({ - let __buf = ((*msg.borrow()).clone() as Ptr) - .to_any() - .reinterpret_cast::(); - match __buf.with_slice(4_usize, |__s| { - nix::sys::socket::send( - (*sv.borrow())[(0) as usize], - __s, - nix::sys::socket::MsgFlags::from_bits_truncate(0), - ) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } == 4_isize) as i32) - != 0) - ); - let buf: Value> = Rc::new(RefCell::new(Box::new([ - 0_u8, - ::default(), - ::default(), - ::default(), - ::default(), - ::default(), - ::default(), - ::default(), - ]))); - assert!( - ((({ - let __buf = ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::(); - match __buf.with_slice_mut(8_usize, |__s| { - nix::sys::socket::recv( - (*sv.borrow())[(1) as usize], - __s, - nix::sys::socket::MsgFlags::from_bits_truncate(0), - ) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } == 4_isize) as i32) - != 0) - ); - assert!( - (((((buf.as_pointer() as Ptr::) as Ptr::) - .to_any() - .memcmp(&Ptr::from_string_literal(b"ping").to_any(), 4_usize) - == 0) as i32) - != 0) - ); - assert!( - ((({ - let __buf = Ptr::from_string_literal(b"pong!") - .to_any() - .reinterpret_cast::(); - match __buf.with_slice(5_usize, |__s| { - nix::sys::socket::send( - (*sv.borrow())[(1) as usize], - __s, - nix::sys::socket::MsgFlags::from_bits_truncate(0), - ) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } == 5_isize) as i32) - != 0) - ); - assert!( - ((({ - let __buf = ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::(); - match __buf.with_slice_mut(8_usize, |__s| { - nix::sys::socket::recv( - (*sv.borrow())[(0) as usize], - __s, - nix::sys::socket::MsgFlags::from_bits_truncate(0), - ) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } == 5_isize) as i32) - != 0) - ); - assert!( - (((((buf.as_pointer() as Ptr::) as Ptr::) - .to_any() - .memcmp(&Ptr::from_string_literal(b"pong!").to_any(), 5_usize) - == 0) as i32) - != 0) - ); -} -pub fn test_send_recv_scalar_1() { - let sv: Value> = Rc::new(RefCell::new( - (0..2).map(|_| ::default()).collect::>(), - )); - assert!( - ((({ - use nix::sys::socket::{AddressFamily, SockFlag, SockProtocol, SockType}; - let __out = (sv.as_pointer() as Ptr).clone(); - let __flag_bits = ::libc::SOCK_CLOEXEC | ::libc::SOCK_NONBLOCK; - match ( - AddressFamily::from_i32(1), - SockType::try_from(libc::SOCK_STREAM & !__flag_bits), - ) { - (Some(__f), Ok(__t)) => { - let __flags = SockFlag::from_bits_truncate(libc::SOCK_STREAM & __flag_bits); - let __proto: Option = None; - match nix::sys::socket::socketpair(__f, __t, __proto, __flags) { - Ok((__a, __b)) => { - __out.write(::std::os::fd::IntoRawFd::into_raw_fd(__a)); - __out - .offset(1) - .write(::std::os::fd::IntoRawFd::into_raw_fd(__b)); - 0 - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } - _ => { - libcc2rs::cpp2rust_errno().write(::libc::EINVAL); - -1 - } - } - } == 0) as i32) - != 0) - ); - let value: Value = Rc::new(RefCell::new(42)); - assert!( - (((({ - let __buf = ((value.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::(); - match __buf.with_slice(::std::mem::size_of::(), |__s| { - nix::sys::socket::send( - (*sv.borrow())[(0) as usize], - __s, - nix::sys::socket::MsgFlags::from_bits_truncate(0), - ) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } as usize) - == ::std::mem::size_of::()) as i32) - != 0) - ); - let received: Value = Rc::new(RefCell::new(0)); - assert!( - (((({ - let __buf = ((received.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::(); - match __buf.with_slice_mut(::std::mem::size_of::(), |__s| { - nix::sys::socket::recv( - (*sv.borrow())[(1) as usize], - __s, - nix::sys::socket::MsgFlags::from_bits_truncate(0), - ) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } as usize) - == ::std::mem::size_of::()) as i32) - != 0) - ); - assert!(((((*received.borrow()) == 42) as i32) != 0)); -} -pub fn test_send_bad_fd_2() { - libcc2rs::cpp2rust_errno().write(0); - assert!( - ((({ - let __buf = Ptr::from_string_literal(b"x") - .to_any() - .reinterpret_cast::(); - match __buf.with_slice(1_usize, |__s| { - nix::sys::socket::send( - -1_i32, - __s, - nix::sys::socket::MsgFlags::from_bits_truncate(0), - ) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } == (-1_i32 as isize)) as i32) - != 0) - ); - assert!(((((libcc2rs::cpp2rust_errno().read()) == 9) as i32) != 0)); -} -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - ({ test_send_recv_0() }); - ({ test_send_recv_scalar_1() }); - ({ test_send_bad_fd_2() }); - return 0; -} diff --git a/tests/unit/out/unsafe/socket_send_recv.rs b/tests/unit/out/unsafe/socket_send_recv.rs deleted file mode 100644 index 97aacac8..00000000 --- a/tests/unit/out/unsafe/socket_send_recv.rs +++ /dev/null @@ -1,154 +0,0 @@ -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 unsafe fn test_send_recv_0() { - let mut sv: [i32; 2] = [0_i32; 2]; - assert!(((((libc::socketpair(1, libc::SOCK_STREAM, 0, sv.as_mut_ptr())) == (0)) as i32) != 0)); - let mut msg: *const libc::c_char = (c"ping".as_ptr().cast_mut()).cast_const(); - assert!( - ((((libc::send( - sv[(0) as usize], - (msg as *const libc::c_char as *const ::libc::c_void), - 4_usize, - 0 - )) == (4_isize)) as i32) - != 0) - ); - let mut buf: [libc::c_char; 8] = [ - (0 as libc::c_char), - (0 as libc::c_char), - (0 as libc::c_char), - (0 as libc::c_char), - (0 as libc::c_char), - (0 as libc::c_char), - (0 as libc::c_char), - (0 as libc::c_char), - ]; - assert!( - ((((libc::recv( - sv[(1) as usize], - (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), - 8_usize, - 0 - )) == (4_isize)) as i32) - != 0) - ); - assert!( - (((({ - let sa = core::slice::from_raw_parts( - (buf.as_mut_ptr() as *const libc::c_char as *const ::libc::c_void) as *const u8, - 4_usize as usize, - ); - let sb = core::slice::from_raw_parts( - (c"ping".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void) - as *const u8, - 4_usize as usize, - ); - let mut diff = 0_i32; - for (x, y) in sa.iter().zip(sb.iter()) { - if x != y { - diff = (*x as i32) - (*y as i32); - break; - } - } - diff - }) == (0)) as i32) - != 0) - ); - assert!( - ((((libc::send( - sv[(1) as usize], - (c"pong!".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), - 5_usize, - 0 - )) == (5_isize)) as i32) - != 0) - ); - assert!( - ((((libc::recv( - sv[(0) as usize], - (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), - 8_usize, - 0 - )) == (5_isize)) as i32) - != 0) - ); - assert!( - (((({ - let sa = core::slice::from_raw_parts( - (buf.as_mut_ptr() as *const libc::c_char as *const ::libc::c_void) as *const u8, - 5_usize as usize, - ); - let sb = core::slice::from_raw_parts( - (c"pong!".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void) - as *const u8, - 5_usize as usize, - ); - let mut diff = 0_i32; - for (x, y) in sa.iter().zip(sb.iter()) { - if x != y { - diff = (*x as i32) - (*y as i32); - break; - } - } - diff - }) == (0)) as i32) - != 0) - ); -} -pub unsafe fn test_send_recv_scalar_1() { - let mut sv: [i32; 2] = [0_i32; 2]; - assert!(((((libc::socketpair(1, libc::SOCK_STREAM, 0, sv.as_mut_ptr())) == (0)) as i32) != 0)); - let mut value: i32 = 42; - assert!( - ((((libc::send( - sv[(0) as usize], - ((&mut value as *mut i32) as *const i32 as *const ::libc::c_void), - ::std::mem::size_of::(), - 0 - ) as usize) - == (::std::mem::size_of::())) as i32) - != 0) - ); - let mut received: i32 = 0; - assert!( - ((((libc::recv( - sv[(1) as usize], - ((&mut received as *mut i32) as *mut i32 as *mut ::libc::c_void), - ::std::mem::size_of::(), - 0 - ) as usize) - == (::std::mem::size_of::())) as i32) - != 0) - ); - assert!(((((received) == (42)) as i32) != 0)); -} -pub unsafe fn test_send_bad_fd_2() { - (*libcc2rs::cpp2rust_errno_unsafe()) = 0; - assert!( - ((((libc::send( - -1_i32, - (c"x".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), - 1_usize, - 0 - )) == (-1_i32 as isize)) as i32) - != 0) - ); - assert!(((((*libcc2rs::cpp2rust_errno_unsafe()) == (9)) as i32) != 0)); -} -pub fn main() { - unsafe { - std::process::exit(main_0() as i32); - } -} -unsafe fn main_0() -> i32 { - (unsafe { test_send_recv_0() }); - (unsafe { test_send_recv_scalar_1() }); - (unsafe { test_send_bad_fd_2() }); - return 0; -} diff --git a/tests/unit/socket_send_recv.c b/tests/unit/socket_send_recv.c deleted file mode 100644 index f85a08d5..00000000 --- a/tests/unit/socket_send_recv.c +++ /dev/null @@ -1,41 +0,0 @@ -#include -#include -#include -#include -#include - -static void test_send_recv(void) { - int sv[2]; - assert(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0); - const char *msg = "ping"; - assert(send(sv[0], msg, 4, 0) == 4); - char buf[8] = {0}; - assert(recv(sv[1], buf, 8, 0) == 4); - assert(memcmp(buf, "ping", 4) == 0); - assert(send(sv[1], "pong!", 5, 0) == 5); - assert(recv(sv[0], buf, 8, 0) == 5); - assert(memcmp(buf, "pong!", 5) == 0); -} - -static void test_send_recv_scalar(void) { - int sv[2]; - assert(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0); - int value = 42; - assert(send(sv[0], &value, sizeof(value), 0) == sizeof(value)); - int received = 0; - assert(recv(sv[1], &received, sizeof(received), 0) == sizeof(received)); - assert(received == 42); -} - -static void test_send_bad_fd(void) { - errno = 0; - assert(send(-1, "x", 1, 0) == -1); - assert(errno == EBADF); -} - -int main(void) { - test_send_recv(); - test_send_recv_scalar(); - test_send_bad_fd(); - return 0; -} From d088e06be46c6a7a6fd5651a04b1554f295009cb Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 16 Jul 2026 21:53:57 +0100 Subject: [PATCH 6/6] Delete socketpair rule --- rules/socket/tgt_refcount.rs | 61 ------------------------------------ 1 file changed, 61 deletions(-) diff --git a/rules/socket/tgt_refcount.rs b/rules/socket/tgt_refcount.rs index 5cdfc4da..5d591a14 100644 --- a/rules/socket/tgt_refcount.rs +++ b/rules/socket/tgt_refcount.rs @@ -15,67 +15,6 @@ fn t3() -> libcc2rs::SockaddrUn { Default::default() } -#[cfg(target_os = "linux")] -fn f11(a0: i32, a1: i32, a2: i32, a3: Ptr) -> i32 { - use nix::sys::socket::{AddressFamily, SockFlag, SockProtocol, SockType}; - let __out = a3.clone(); - let __flag_bits = ::libc::SOCK_CLOEXEC | ::libc::SOCK_NONBLOCK; - match ( - AddressFamily::from_i32(a0), - SockType::try_from(a1 & !__flag_bits), - ) { - (Some(__f), Ok(__t)) => { - let __flags = SockFlag::from_bits_truncate(a1 & __flag_bits); - let __proto: Option = None; - match nix::sys::socket::socketpair(__f, __t, __proto, __flags) { - Ok((__a, __b)) => { - __out.write(::std::os::fd::IntoRawFd::into_raw_fd(__a)); - __out - .offset(1) - .write(::std::os::fd::IntoRawFd::into_raw_fd(__b)); - 0 - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } - _ => { - libcc2rs::cpp2rust_errno().write(::libc::EINVAL); - -1 - } - } -} - -#[cfg(target_os = "macos")] -fn f11(a0: i32, a1: i32, a2: i32, a3: Ptr) -> i32 { - use nix::sys::socket::{AddressFamily, SockFlag, SockProtocol, SockType}; - let __out = a3.clone(); - match (AddressFamily::from_i32(a0), SockType::try_from(a1)) { - (Some(__f), Ok(__t)) => { - let __proto: Option = None; - match nix::sys::socket::socketpair(__f, __t, __proto, SockFlag::empty()) { - Ok((__a, __b)) => { - __out.write(::std::os::fd::IntoRawFd::into_raw_fd(__a)); - __out - .offset(1) - .write(::std::os::fd::IntoRawFd::into_raw_fd(__b)); - 0 - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } - _ => { - libcc2rs::cpp2rust_errno().write(::libc::EINVAL); - -1 - } - } -} - fn f9(a0: i32, a1: AnyPtr, a2: usize, a3: i32) -> isize { let __buf = a1.reinterpret_cast::(); match __buf.with_slice_mut(a2, |__s| {