From fa1a3907dfe8f6ce98016db301bcb0ce964bd301 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 20 Jul 2026 21:25:55 +0100 Subject: [PATCH 1/9] Add setsockopt and getsockopt safe rules --- rules/socket/tgt_refcount.rs | 123 +++ tests/unit/out/refcount/fd_io.rs | 1536 +++++++++++++++++++++++++++++- tests/unit/out/unsafe/fd_io.rs | 373 +++++++- 3 files changed, 2020 insertions(+), 12 deletions(-) diff --git a/rules/socket/tgt_refcount.rs b/rules/socket/tgt_refcount.rs index 69bda802..185b1125 100644 --- a/rules/socket/tgt_refcount.rs +++ b/rules/socket/tgt_refcount.rs @@ -218,3 +218,126 @@ fn f17(a0: i32, a1: i32) -> i32 { } } } + +fn f7(a0: i32, a1: i32, a2: i32, a3: AnyPtr, a4: u32) -> i32 { + let __res = match (a1, a2) { + (::libc::IPPROTO_TCP, ::libc::TCP_NODELAY) => { + let __v = a3.reinterpret_cast::().read() != 0; + FdRegistry::with_fd(a0, |__fd| { + nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::TcpNoDelay, &__v) + }) + } + (::libc::SOL_SOCKET, ::libc::SO_KEEPALIVE) => { + let __v = a3.reinterpret_cast::().read() != 0; + FdRegistry::with_fd(a0, |__fd| { + nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::KeepAlive, &__v) + }) + } + (::libc::IPPROTO_TCP, ::libc::TCP_KEEPINTVL) => { + let __v = a3.reinterpret_cast::().read(); + FdRegistry::with_fd(a0, |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::TcpKeepInterval, + &__v, + ) + }) + } + (::libc::IPPROTO_TCP, ::libc::TCP_KEEPCNT) => { + let __v = a3.reinterpret_cast::().read(); + FdRegistry::with_fd(a0, |__fd| { + nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::TcpKeepCount, &__v) + }) + } + (::libc::IPPROTO_IP, ::libc::IP_TOS) => { + let __v = a3.reinterpret_cast::().read(); + FdRegistry::with_fd(a0, |__fd| { + nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::Ipv4Tos, &__v) + }) + } + (::libc::IPPROTO_IPV6, ::libc::IPV6_TCLASS) => { + let __v = a3.reinterpret_cast::().read(); + FdRegistry::with_fd(a0, |__fd| { + nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::Ipv6TClass, &__v) + }) + } + #[cfg(target_os = "linux")] + (::libc::IPPROTO_TCP, ::libc::TCP_KEEPIDLE) => { + let __v = a3.reinterpret_cast::().read(); + FdRegistry::with_fd(a0, |__fd| { + nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::TcpKeepIdle, &__v) + }) + } + #[cfg(target_os = "linux")] + (::libc::SOL_SOCKET, ::libc::SO_BINDTODEVICE) => { + let __v = ::std::ffi::OsString::from(a3.reinterpret_cast::().to_rust_string()); + FdRegistry::with_fd(a0, |__fd| { + nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::BindToDevice, &__v) + }) + } + #[cfg(target_os = "linux")] + (::libc::IPPROTO_IP, ::libc::IP_BIND_ADDRESS_NO_PORT) => { + let __v = a3.reinterpret_cast::().read() != 0; + FdRegistry::with_fd(a0, |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::IpBindAddressNoPort, + &__v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::IPPROTO_TCP, ::libc::TCP_FASTOPEN_CONNECT) => { + let __v = a3.reinterpret_cast::().read() != 0; + FdRegistry::with_fd(a0, |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::TcpFastOpenConnect, + &__v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::SOL_SOCKET, ::libc::SO_PRIORITY) => { + let __v = a3.reinterpret_cast::().read(); + FdRegistry::with_fd(a0, |__fd| { + nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::Priority, &__v) + }) + } + (__l, __o) => panic!( + "setsockopt: unsupported option (level={}, optname={})", + __l, __o + ), + }; + match __res { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } +} + +fn f8(a0: i32, a1: i32, a2: i32, a3: AnyPtr, a4: Ptr) -> i32 { + match (a1, a2) { + (::libc::SOL_SOCKET, ::libc::SO_ERROR) => { + match FdRegistry::with_fd(a0, |__fd| { + nix::sys::socket::getsockopt(&__fd, nix::sys::socket::sockopt::SocketError) + }) { + Ok(__err) => { + a3.reinterpret_cast::().write(__err); + a4.write(::std::mem::size_of::() as u32); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } + (__l, __o) => panic!( + "getsockopt: unsupported option (level={}, optname={})", + __l, __o + ), + } +} diff --git a/tests/unit/out/refcount/fd_io.rs b/tests/unit/out/refcount/fd_io.rs index f1272772..d1df629a 100644 --- a/tests/unit/out/refcount/fd_io.rs +++ b/tests/unit/out/refcount/fd_io.rs @@ -6,12 +6,9 @@ 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 { +pub fn test_open_read_write_0() { let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"cpp2rust_fd_io_test.tmp", + b"/tmp/cpp2rust_fd_io_rw.tmp", ))); let fd: Value = Rc::new(RefCell::new({ let __mode = match &[(420).into()].first() { @@ -138,5 +135,1534 @@ fn main_0() -> i32 { } == 0) as i32) != 0) ); +} +pub fn test_pipe_1() { + let fds: Value> = Rc::new(RefCell::new( + (0..2).map(|_| ::default()).collect::>(), + )); + assert!( + (((match nix::unistd::pipe() { + Ok((__r, __w)) => { + let __fds = (fds.as_pointer() as Ptr).clone(); + __fds.write(FdRegistry::register(__r)); + __fds.offset(1).write(FdRegistry::register(__w)); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(1) as usize], |__fd| { + Ptr::from_string_literal(b"ab") + .to_any() + .reinterpret_cast::() + .with_slice(2_usize, |__buf| nix::unistd::write(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 2_isize) as i32) + != 0) + ); + let buf: Value> = Rc::new(RefCell::new( + (0..4).map(|_| ::default()).collect::>(), + )); + { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .memset((0) as u8, ::std::mem::size_of::<[u8; 4]>() as usize); + ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() + }; + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(::std::mem::size_of::<[u8; 4]>(), |__buf| { + nix::unistd::read(__fd, __buf) + }) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 2_isize) as i32) + != 0) + ); + assert!( + ((({ + let mut __it1 = (buf.as_pointer() as Ptr).to_c_string_iterator(); + let mut __it2 = Ptr::from_string_literal(b"ab").to_c_string_iterator(); + loop { + let __c1 = __it1.next(); + let __c2 = __it2.next(); + if __c1 != __c2 { + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); + } + if __c1.is_none() { + break 0; + } + } + } == 0) as i32) + != 0) + ); + assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(::std::mem::size_of::<[u8; 4]>(), |__buf| { + nix::unistd::read(__fd, __buf) + }) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0_isize) as i32) + != 0) + ); + assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); +} +pub fn test_socket_listen_2() { + let s: Value = Rc::new(RefCell::new({ + let __family = match libc::AF_INET { + ::libc::AF_INET => nix::sys::socket::AddressFamily::Inet, + ::libc::AF_INET6 => nix::sys::socket::AddressFamily::Inet6, + ::libc::AF_UNIX => nix::sys::socket::AddressFamily::Unix, + __d => panic!("socket: unsupported domain {__d}"), + }; + let __flags = nix::sys::socket::SockFlag::from_bits_truncate(libc::SOCK_STREAM); + let __ty = match libc::SOCK_STREAM & !nix::sys::socket::SockFlag::all().bits() { + ::libc::SOCK_STREAM => nix::sys::socket::SockType::Stream, + ::libc::SOCK_DGRAM => nix::sys::socket::SockType::Datagram, + __t => panic!("socket: unsupported type {__t}"), + }; + let __proto = match 0 { + 0 => None, + ::libc::IPPROTO_TCP => Some(nix::sys::socket::SockProtocol::Tcp), + ::libc::IPPROTO_UDP => Some(nix::sys::socket::SockProtocol::Udp), + __p => panic!("socket: unsupported protocol {__p}"), + }; + match nix::sys::socket::socket(__family, __ty, __flags, __proto) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*s.borrow()) >= 0) as i32) != 0)); + assert!( + (((match nix::sys::socket::Backlog::new(5) { + Ok(__b) => match FdRegistry::with_fd((*s.borrow()), |__fd| nix::sys::socket::listen( + &__fd, __b + )) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + }, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + assert!((((FdRegistry::close((*s.borrow())) == 0) as i32) != 0)); +} +pub fn test_sockopt_3() { + let s: Value = Rc::new(RefCell::new({ + let __family = match libc::AF_INET { + ::libc::AF_INET => nix::sys::socket::AddressFamily::Inet, + ::libc::AF_INET6 => nix::sys::socket::AddressFamily::Inet6, + ::libc::AF_UNIX => nix::sys::socket::AddressFamily::Unix, + __d => panic!("socket: unsupported domain {__d}"), + }; + let __flags = nix::sys::socket::SockFlag::from_bits_truncate(libc::SOCK_STREAM); + let __ty = match libc::SOCK_STREAM & !nix::sys::socket::SockFlag::all().bits() { + ::libc::SOCK_STREAM => nix::sys::socket::SockType::Stream, + ::libc::SOCK_DGRAM => nix::sys::socket::SockType::Datagram, + __t => panic!("socket: unsupported type {__t}"), + }; + let __proto = match 0 { + 0 => None, + ::libc::IPPROTO_TCP => Some(nix::sys::socket::SockProtocol::Tcp), + ::libc::IPPROTO_UDP => Some(nix::sys::socket::SockProtocol::Udp), + __p => panic!("socket: unsupported protocol {__p}"), + }; + match nix::sys::socket::socket(__family, __ty, __flags, __proto) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*s.borrow()) >= 0) as i32) != 0)); + let on: Value = Rc::new(RefCell::new(1)); + assert!( + ((({ + let __res = match (1, 9) { + (::libc::IPPROTO_TCP, ::libc::TCP_NODELAY) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read() + != 0; + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::TcpNoDelay, + &__v, + ) + }) + } + (::libc::SOL_SOCKET, ::libc::SO_KEEPALIVE) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read() + != 0; + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::KeepAlive, + &__v, + ) + }) + } + (::libc::IPPROTO_TCP, ::libc::TCP_KEEPINTVL) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::TcpKeepInterval, + &__v, + ) + }) + } + (::libc::IPPROTO_TCP, ::libc::TCP_KEEPCNT) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::TcpKeepCount, + &__v, + ) + }) + } + (::libc::IPPROTO_IP, ::libc::IP_TOS) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::Ipv4Tos, + &__v, + ) + }) + } + (::libc::IPPROTO_IPV6, ::libc::IPV6_TCLASS) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::Ipv6TClass, + &__v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::IPPROTO_TCP, ::libc::TCP_KEEPIDLE) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::TcpKeepIdle, + &__v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::SOL_SOCKET, ::libc::SO_BINDTODEVICE) => { + let __v = ::std::ffi::OsString::from( + ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .to_rust_string(), + ); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::BindToDevice, + &__v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::IPPROTO_IP, ::libc::IP_BIND_ADDRESS_NO_PORT) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read() + != 0; + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::IpBindAddressNoPort, + &__v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::IPPROTO_TCP, ::libc::TCP_FASTOPEN_CONNECT) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read() + != 0; + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::TcpFastOpenConnect, + &__v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::SOL_SOCKET, ::libc::SO_PRIORITY) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::Priority, + &__v, + ) + }) + } + (__l, __o) => panic!( + "setsockopt: unsupported option (level={}, optname={})", + __l, __o + ), + }; + match __res { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == 0) as i32) + != 0) + ); + assert!( + ((({ + let __res = match (libc::IPPROTO_TCP, 1) { + (::libc::IPPROTO_TCP, ::libc::TCP_NODELAY) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read() + != 0; + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::TcpNoDelay, + &__v, + ) + }) + } + (::libc::SOL_SOCKET, ::libc::SO_KEEPALIVE) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read() + != 0; + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::KeepAlive, + &__v, + ) + }) + } + (::libc::IPPROTO_TCP, ::libc::TCP_KEEPINTVL) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::TcpKeepInterval, + &__v, + ) + }) + } + (::libc::IPPROTO_TCP, ::libc::TCP_KEEPCNT) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::TcpKeepCount, + &__v, + ) + }) + } + (::libc::IPPROTO_IP, ::libc::IP_TOS) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::Ipv4Tos, + &__v, + ) + }) + } + (::libc::IPPROTO_IPV6, ::libc::IPV6_TCLASS) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::Ipv6TClass, + &__v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::IPPROTO_TCP, ::libc::TCP_KEEPIDLE) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::TcpKeepIdle, + &__v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::SOL_SOCKET, ::libc::SO_BINDTODEVICE) => { + let __v = ::std::ffi::OsString::from( + ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .to_rust_string(), + ); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::BindToDevice, + &__v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::IPPROTO_IP, ::libc::IP_BIND_ADDRESS_NO_PORT) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read() + != 0; + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::IpBindAddressNoPort, + &__v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::IPPROTO_TCP, ::libc::TCP_FASTOPEN_CONNECT) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read() + != 0; + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::TcpFastOpenConnect, + &__v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::SOL_SOCKET, ::libc::SO_PRIORITY) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::Priority, + &__v, + ) + }) + } + (__l, __o) => panic!( + "setsockopt: unsupported option (level={}, optname={})", + __l, __o + ), + }; + match __res { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == 0) as i32) + != 0) + ); + let err: Value = Rc::new(RefCell::new(-1_i32)); + let len: Value = Rc::new(RefCell::new((::std::mem::size_of::() as u32))); + assert!( + (((match (1, 4) { + (::libc::SOL_SOCKET, ::libc::SO_ERROR) => { + match FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::getsockopt(&__fd, nix::sys::socket::sockopt::SocketError) + }) { + Ok(__err) => { + ((err.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .write(__err); + (len.as_pointer()).write(::std::mem::size_of::() as u32); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } + (__l, __o) => panic!( + "getsockopt: unsupported option (level={}, optname={})", + __l, __o + ), + } == 0) as i32) + != 0) + ); + assert!(((((*err.borrow()) == 0) as i32) != 0)); + assert!((((FdRegistry::close((*s.borrow())) == 0) as i32) != 0)); +} +pub fn test_lseek_4() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_fd_io_lseek.tmp", + ))); + let fd: Value = Rc::new(RefCell::new({ + let __mode = match &[(420).into()].first() { + Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), + None => nix::sys::stat::Mode::empty(), + }; + match nix::fcntl::open( + (*path.borrow()).to_rust_string().as_str(), + nix::fcntl::OFlag::from_bits_retain( + ((::libc::O_RDWR | ::libc::O_CREAT) | ::libc::O_TRUNC), + ), + __mode, + ) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*fd.borrow()) >= 0) as i32) != 0)); + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { + Ptr::from_string_literal(b"hello world") + .to_any() + .reinterpret_cast::() + .with_slice(11_usize, |__buf| nix::unistd::write(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 11_isize) as i32) + != 0) + ); + assert!( + ((({ + let __whence = match 2 { + 0 => nix::unistd::Whence::SeekSet, + 1 => nix::unistd::Whence::SeekCur, + 2 => nix::unistd::Whence::SeekEnd, + __w => panic!("lseek: unsupported whence {__w}"), + }; + match FdRegistry::with_fd((*fd.borrow()), |__fd| { + nix::unistd::lseek(__fd, 0_i64, __whence) + }) { + Ok(__off) => __off, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == 11_i64) as i32) + != 0) + ); + assert!( + ((({ + let __whence = match 0 { + 0 => nix::unistd::Whence::SeekSet, + 1 => nix::unistd::Whence::SeekCur, + 2 => nix::unistd::Whence::SeekEnd, + __w => panic!("lseek: unsupported whence {__w}"), + }; + match FdRegistry::with_fd((*fd.borrow()), |__fd| { + nix::unistd::lseek(__fd, 6_i64, __whence) + }) { + Ok(__off) => __off, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == 6_i64) as i32) + != 0) + ); + let buf: Value> = Rc::new(RefCell::new( + (0..16).map(|_| ::default()).collect::>(), + )); + { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .memset((0) as u8, ::std::mem::size_of::<[u8; 16]>() as usize); + ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() + }; + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(::std::mem::size_of::<[u8; 16]>(), |__buf| { + nix::unistd::read(__fd, __buf) + }) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 5_isize) as i32) + != 0) + ); + assert!( + ((({ + let mut __it1 = (buf.as_pointer() as Ptr).to_c_string_iterator(); + let mut __it2 = Ptr::from_string_literal(b"world").to_c_string_iterator(); + loop { + let __c1 = __it1.next(); + let __c2 = __it2.next(); + if __c1 != __c2 { + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); + } + if __c1.is_none() { + break 0; + } + } + } == 0) as i32) + != 0) + ); + assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); + assert!( + (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); +} +pub fn test_ftruncate_5() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_fd_io_trunc.tmp", + ))); + let fd: Value = Rc::new(RefCell::new({ + let __mode = match &[(420).into()].first() { + Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), + None => nix::sys::stat::Mode::empty(), + }; + match nix::fcntl::open( + (*path.borrow()).to_rust_string().as_str(), + nix::fcntl::OFlag::from_bits_retain( + ((::libc::O_RDWR | ::libc::O_CREAT) | ::libc::O_TRUNC), + ), + __mode, + ) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*fd.borrow()) >= 0) as i32) != 0)); + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { + Ptr::from_string_literal(b"hello world") + .to_any() + .reinterpret_cast::() + .with_slice(11_usize, |__buf| nix::unistd::write(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 11_isize) as i32) + != 0) + ); + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| nix::unistd::ftruncate(__fd, 5_i64)) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + assert!( + ((({ + let __whence = match 2 { + 0 => nix::unistd::Whence::SeekSet, + 1 => nix::unistd::Whence::SeekCur, + 2 => nix::unistd::Whence::SeekEnd, + __w => panic!("lseek: unsupported whence {__w}"), + }; + match FdRegistry::with_fd((*fd.borrow()), |__fd| { + nix::unistd::lseek(__fd, 0_i64, __whence) + }) { + Ok(__off) => __off, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == 5_i64) as i32) + != 0) + ); + assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); + assert!( + (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); +} +pub fn test_fstat_6() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_fd_io_fstat.tmp", + ))); + let fd: Value = Rc::new(RefCell::new({ + let __mode = match &[(420).into()].first() { + Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), + None => nix::sys::stat::Mode::empty(), + }; + match nix::fcntl::open( + (*path.borrow()).to_rust_string().as_str(), + nix::fcntl::OFlag::from_bits_retain( + ((::libc::O_WRONLY | ::libc::O_CREAT) | ::libc::O_TRUNC), + ), + __mode, + ) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*fd.borrow()) >= 0) as i32) != 0)); + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { + Ptr::from_string_literal(b"hello") + .to_any() + .reinterpret_cast::() + .with_slice(5_usize, |__buf| nix::unistd::write(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 5_isize) as i32) + != 0) + ); + let st: Value = Rc::new(RefCell::new(Default::default())); + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| nix::sys::stat::fstat(__fd)) { + Ok(__s) => { + (st.as_pointer()).with_mut(|__st| *__st = Stat::from_libc(&__s)); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + assert!(((((*(*st.borrow()).st_size.borrow()) == 5_i64) as i32) != 0)); + assert!((((((*(*st.borrow()).st_mode.borrow()) & 61440_u32) == 32768_u32) as i32) != 0)); + assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); + assert!( + (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); +} +pub fn test_isatty_7() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_fd_io_tty.tmp", + ))); + let fd: Value = Rc::new(RefCell::new({ + let __mode = match &[(420).into()].first() { + Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), + None => nix::sys::stat::Mode::empty(), + }; + match nix::fcntl::open( + (*path.borrow()).to_rust_string().as_str(), + nix::fcntl::OFlag::from_bits_retain((::libc::O_RDONLY | ::libc::O_CREAT)), + __mode, + ) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*fd.borrow()) >= 0) as i32) != 0)); + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| nix::unistd::isatty(__fd)) { + Ok(__tty) => __tty as i32, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + 0 + } + } == 0) as i32) + != 0) + ); + assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); + assert!( + (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); +} +pub fn test_tcgetattr_8() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_fd_io_termios.tmp", + ))); + let fd: Value = Rc::new(RefCell::new({ + let __mode = match &[(420).into()].first() { + Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), + None => nix::sys::stat::Mode::empty(), + }; + match nix::fcntl::open( + (*path.borrow()).to_rust_string().as_str(), + nix::fcntl::OFlag::from_bits_retain((::libc::O_RDONLY | ::libc::O_CREAT)), + __mode, + ) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*fd.borrow()) >= 0) as i32) != 0)); + let tio: Value = Rc::new(RefCell::new(Default::default())); + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| nix::sys::termios::tcgetattr(__fd)) { + Ok(__t) => { + (tio.as_pointer()).with_mut(|__dst| *__dst = Termios::from_libc(&__t.into())); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == -1_i32) as i32) + != 0) + ); + assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); + assert!( + (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); +} +pub fn test_fcntl_9() { + let fds: Value> = Rc::new(RefCell::new( + (0..2).map(|_| ::default()).collect::>(), + )); + assert!( + (((match nix::unistd::pipe() { + Ok((__r, __w)) => { + let __fds = (fds.as_pointer() as Ptr).clone(); + __fds.write(FdRegistry::register(__r)); + __fds.offset(1).write(FdRegistry::register(__w)); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + let flags: Value = Rc::new(RefCell::new({ + let __res = match 3 { + ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) + }) + } + ::libc::F_SETFD => { + let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) + }) + } + __cmd => panic!("fcntl: unsupported cmd {}", __cmd), + }; + match __res { + Ok(__r) => __r, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*flags.borrow()) >= 0) as i32) != 0)); + assert!((((((*flags.borrow()) & ::libc::O_NONBLOCK) == 0) as i32) != 0)); + assert!( + ((({ + let __res = match 4 { + ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get( + &&[((*flags.borrow()) | ::libc::O_NONBLOCK).into()][0], + )); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) + }) + } + ::libc::F_SETFD => { + let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get( + &&[((*flags.borrow()) | ::libc::O_NONBLOCK).into()][0], + )); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) + }) + } + __cmd => panic!("fcntl: unsupported cmd {}", __cmd), + }; + match __res { + Ok(__r) => __r, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == 0) as i32) + != 0) + ); + (*flags.borrow_mut()) = { + let __res = match 3 { + ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) + }) + } + ::libc::F_SETFD => { + let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) + }) + } + __cmd => panic!("fcntl: unsupported cmd {}", __cmd), + }; + match __res { + Ok(__r) => __r, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + }; + assert!((((((*flags.borrow()) & ::libc::O_NONBLOCK) != 0) as i32) != 0)); + let b: Value = >::default(); + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + ((b.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(1_usize, |__buf| nix::unistd::read(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == (-1_i32 as isize)) as i32) + != 0) + ); + assert!( + ((({ + let __res = match 2 { + ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(1).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) + }) + } + ::libc::F_SETFD => { + let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(1).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) + }) + } + __cmd => panic!("fcntl: unsupported cmd {}", __cmd), + }; + match __res { + Ok(__r) => __r, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == 0) as i32) + != 0) + ); + assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); + assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); +} +pub fn test_select_10() { + let fds: Value> = Rc::new(RefCell::new( + (0..2).map(|_| ::default()).collect::>(), + )); + assert!( + (((match nix::unistd::pipe() { + Ok((__r, __w)) => { + let __fds = (fds.as_pointer() as Ptr).clone(); + __fds.write(FdRegistry::register(__r)); + __fds.offset(1).write(FdRegistry::register(__w)); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + let rset: Value = Rc::new(RefCell::new(Default::default())); + (rset.as_pointer()).with_mut(|__s| __s.zero()); + (rset.as_pointer()).with_mut(|__s| __s.set((*fds.borrow())[(0) as usize])); + let tv: Value = Rc::new(RefCell::new(Default::default())); + (*(*tv.borrow()).tv_sec.borrow_mut()) = 0_i64; + (*(*tv.borrow()).tv_usec.borrow_mut()) = 0_i64; + assert!( + ((({ + let __rp = (rset.as_pointer()).clone(); + let __wp = Ptr::::null().clone(); + let __ep = Ptr::::null().clone(); + let __tp = (tv.as_pointer()).clone(); + let __r_fds: Vec = match __rp.is_null() { + true => Vec::new(), + false => __rp.with(|__s| { + (0..((*fds.borrow())[(0) as usize] + 1)) + .filter(|&__fd| __s.isset(__fd)) + .collect() + }), + }; + let __w_fds: Vec = match __wp.is_null() { + true => Vec::new(), + false => __wp.with(|__s| { + (0..((*fds.borrow())[(0) as usize] + 1)) + .filter(|&__fd| __s.isset(__fd)) + .collect() + }), + }; + let __e_fds: Vec = match __ep.is_null() { + true => Vec::new(), + false => __ep.with(|__s| { + (0..((*fds.borrow())[(0) as usize] + 1)) + .filter(|&__fd| __s.isset(__fd)) + .collect() + }), + }; + let mut __wanted = Vec::new(); + __wanted.extend(&__r_fds); + __wanted.extend(&__w_fds); + __wanted.extend(&__e_fds); + FdRegistry::with_fds(&__wanted, |__b| { + let __rn = __r_fds.len(); + let __wn = __w_fds.len(); + let mut __rs = nix::sys::select::FdSet::new(); + let mut __ws = nix::sys::select::FdSet::new(); + let mut __es = nix::sys::select::FdSet::new(); + for __bfd in &__b[..__rn] { + __rs.insert(*__bfd); + } + for __bfd in &__b[__rn..__rn + __wn] { + __ws.insert(*__bfd); + } + for __bfd in &__b[__rn + __wn..] { + __es.insert(*__bfd); + } + let mut __tv = match __tp.is_null() { + true => None, + false => Some(__tp.with(|__t| { + nix::sys::time::TimeVal::new( + *__t.tv_sec.borrow() as _, + *__t.tv_usec.borrow() as _, + ) + })), + }; + match nix::sys::select::select( + ((*fds.borrow())[(0) as usize] + 1), + match __rp.is_null() { + true => None, + false => Some(&mut __rs), + }, + match __wp.is_null() { + true => None, + false => Some(&mut __ws), + }, + match __ep.is_null() { + true => None, + false => Some(&mut __es), + }, + __tv.as_mut(), + ) { + Ok(__n) => { + if !__rp.is_null() { + __rp.with_mut(|__s| { + __s.zero(); + for (__fd, __bfd) in __r_fds.iter().zip(&__b[..__rn]) { + if __rs.contains(*__bfd) { + __s.set(*__fd); + } + } + }); + } + if !__wp.is_null() { + __wp.with_mut(|__s| { + __s.zero(); + for (__fd, __bfd) in __w_fds.iter().zip(&__b[__rn..__rn + __wn]) { + if __ws.contains(*__bfd) { + __s.set(*__fd); + } + } + }); + } + if !__ep.is_null() { + __ep.with_mut(|__s| { + __s.zero(); + for (__fd, __bfd) in __e_fds.iter().zip(&__b[__rn + __wn..]) { + if __es.contains(*__bfd) { + __s.set(*__fd); + } + } + }); + } + match (__tp.is_null(), __tv.as_ref()) { + (false, Some(__t)) => __tp.with_mut(|__dst| { + *__dst.tv_sec.borrow_mut() = __t.tv_sec() as i64; + *__dst.tv_usec.borrow_mut() = __t.tv_usec() as i64; + }), + _ => {} + } + __n + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + }) + } == 0) as i32) + != 0) + ); + assert!( + ((!(if (rset.as_pointer()).with(|__s| __s.isset((*fds.borrow())[(0) as usize])) { + 1 + } else { + 0 + } != 0) as i32) + != 0) + ); + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(1) as usize], |__fd| { + Ptr::from_string_literal(b"x") + .to_any() + .reinterpret_cast::() + .with_slice(1_usize, |__buf| nix::unistd::write(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 1_isize) as i32) + != 0) + ); + (rset.as_pointer()).with_mut(|__s| __s.zero()); + (rset.as_pointer()).with_mut(|__s| __s.set((*fds.borrow())[(0) as usize])); + (*(*tv.borrow()).tv_sec.borrow_mut()) = 1_i64; + (*(*tv.borrow()).tv_usec.borrow_mut()) = 0_i64; + assert!( + ((({ + let __rp = (rset.as_pointer()).clone(); + let __wp = Ptr::::null().clone(); + let __ep = Ptr::::null().clone(); + let __tp = (tv.as_pointer()).clone(); + let __r_fds: Vec = match __rp.is_null() { + true => Vec::new(), + false => __rp.with(|__s| { + (0..((*fds.borrow())[(0) as usize] + 1)) + .filter(|&__fd| __s.isset(__fd)) + .collect() + }), + }; + let __w_fds: Vec = match __wp.is_null() { + true => Vec::new(), + false => __wp.with(|__s| { + (0..((*fds.borrow())[(0) as usize] + 1)) + .filter(|&__fd| __s.isset(__fd)) + .collect() + }), + }; + let __e_fds: Vec = match __ep.is_null() { + true => Vec::new(), + false => __ep.with(|__s| { + (0..((*fds.borrow())[(0) as usize] + 1)) + .filter(|&__fd| __s.isset(__fd)) + .collect() + }), + }; + let mut __wanted = Vec::new(); + __wanted.extend(&__r_fds); + __wanted.extend(&__w_fds); + __wanted.extend(&__e_fds); + FdRegistry::with_fds(&__wanted, |__b| { + let __rn = __r_fds.len(); + let __wn = __w_fds.len(); + let mut __rs = nix::sys::select::FdSet::new(); + let mut __ws = nix::sys::select::FdSet::new(); + let mut __es = nix::sys::select::FdSet::new(); + for __bfd in &__b[..__rn] { + __rs.insert(*__bfd); + } + for __bfd in &__b[__rn..__rn + __wn] { + __ws.insert(*__bfd); + } + for __bfd in &__b[__rn + __wn..] { + __es.insert(*__bfd); + } + let mut __tv = match __tp.is_null() { + true => None, + false => Some(__tp.with(|__t| { + nix::sys::time::TimeVal::new( + *__t.tv_sec.borrow() as _, + *__t.tv_usec.borrow() as _, + ) + })), + }; + match nix::sys::select::select( + ((*fds.borrow())[(0) as usize] + 1), + match __rp.is_null() { + true => None, + false => Some(&mut __rs), + }, + match __wp.is_null() { + true => None, + false => Some(&mut __ws), + }, + match __ep.is_null() { + true => None, + false => Some(&mut __es), + }, + __tv.as_mut(), + ) { + Ok(__n) => { + if !__rp.is_null() { + __rp.with_mut(|__s| { + __s.zero(); + for (__fd, __bfd) in __r_fds.iter().zip(&__b[..__rn]) { + if __rs.contains(*__bfd) { + __s.set(*__fd); + } + } + }); + } + if !__wp.is_null() { + __wp.with_mut(|__s| { + __s.zero(); + for (__fd, __bfd) in __w_fds.iter().zip(&__b[__rn..__rn + __wn]) { + if __ws.contains(*__bfd) { + __s.set(*__fd); + } + } + }); + } + if !__ep.is_null() { + __ep.with_mut(|__s| { + __s.zero(); + for (__fd, __bfd) in __e_fds.iter().zip(&__b[__rn + __wn..]) { + if __es.contains(*__bfd) { + __s.set(*__fd); + } + } + }); + } + match (__tp.is_null(), __tv.as_ref()) { + (false, Some(__t)) => __tp.with_mut(|__dst| { + *__dst.tv_sec.borrow_mut() = __t.tv_sec() as i64; + *__dst.tv_usec.borrow_mut() = __t.tv_usec() as i64; + }), + _ => {} + } + __n + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + }) + } == 1) as i32) + != 0) + ); + assert!( + (if (rset.as_pointer()).with(|__s| __s.isset((*fds.borrow())[(0) as usize])) { + 1 + } else { + 0 + } != 0) + ); + assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); + assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); +} +pub fn test_poll_11() { + let fds: Value> = Rc::new(RefCell::new( + (0..2).map(|_| ::default()).collect::>(), + )); + assert!( + (((match nix::unistd::pipe() { + Ok((__r, __w)) => { + let __fds = (fds.as_pointer() as Ptr).clone(); + __fds.write(FdRegistry::register(__r)); + __fds.offset(1).write(FdRegistry::register(__w)); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(1) as usize], |__fd| { + Ptr::from_string_literal(b"x") + .to_any() + .reinterpret_cast::() + .with_slice(1_usize, |__buf| nix::unistd::write(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 1_isize) as i32) + != 0) + ); + let pfd: Value> = Rc::new(RefCell::new( + (0..2) + .map(|_| Default::default()) + .collect::>(), + )); + (*(*pfd.borrow())[(0) as usize].fd.borrow_mut()) = (*fds.borrow())[(0) as usize]; + (*(*pfd.borrow())[(0) as usize].events.borrow_mut()) = 1_i16; + (*(*pfd.borrow())[(0) as usize].revents.borrow_mut()) = 0_i16; + (*(*pfd.borrow())[(1) as usize].fd.borrow_mut()) = -1_i32; + (*(*pfd.borrow())[(1) as usize].events.borrow_mut()) = 1_i16; + (*(*pfd.borrow())[(1) as usize].revents.borrow_mut()) = 42_i16; + assert!( + ((({ + let __p = (pfd.as_pointer() as Ptr).clone(); + let __timeout = match nix::poll::PollTimeout::try_from(0) { + Ok(__t) => __t, + Err(_) => panic!("poll: unsupported timeout {}", 0), + }; + let mut __idx = Vec::new(); + let mut __wanted = Vec::new(); + let mut __events = Vec::new(); + for __i in 0..(2_u64 as usize) { + let (__fd, __ev) = __p + .offset(__i) + .with(|__e| (*__e.fd.borrow(), *__e.events.borrow())); + __p.offset(__i) + .with_mut(|__e| *__e.revents.borrow_mut() = 0); + if __fd >= 0 { + __idx.push(__i); + __wanted.push(__fd); + __events.push(__ev); + } + } + FdRegistry::with_fds(&__wanted, |__b| { + let mut __pfds: Vec = __b + .iter() + .zip(&__events) + .map(|(&__fd, &__ev)| { + nix::poll::PollFd::new(__fd, nix::poll::PollFlags::from_bits_truncate(__ev)) + }) + .collect(); + match nix::poll::poll(&mut __pfds, __timeout) { + Ok(__count) => { + for (__slot, &__i) in __pfds.iter().zip(&__idx) { + let __rev = match __slot.revents() { + Some(__r) => __r.bits(), + None => 0, + }; + __p.offset(__i) + .with_mut(|__e| *__e.revents.borrow_mut() = __rev); + } + __count + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + }) + } == 1) as i32) + != 0) + ); + assert!( + ((((((*(*pfd.borrow())[(0) as usize].revents.borrow()) as i32) & 1) != 0) as i32) != 0) + ); + assert!((((((*(*pfd.borrow())[(1) as usize].revents.borrow()) as i32) == 0) as i32) != 0)); + let ch: Value = >::default(); + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + ((ch.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(1_usize, |__buf| nix::unistd::read(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 1_isize) as i32) + != 0) + ); + assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); + assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); +} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_open_read_write_0() }); + ({ test_pipe_1() }); + ({ test_socket_listen_2() }); + ({ test_sockopt_3() }); + ({ test_lseek_4() }); + ({ test_ftruncate_5() }); + ({ test_fstat_6() }); + ({ test_isatty_7() }); + ({ test_tcgetattr_8() }); + ({ test_fcntl_9() }); + ({ test_select_10() }); + ({ test_poll_11() }); return 0; } diff --git a/tests/unit/out/unsafe/fd_io.rs b/tests/unit/out/unsafe/fd_io.rs index e629e621..626d6aa8 100644 --- a/tests/unit/out/unsafe/fd_io.rs +++ b/tests/unit/out/unsafe/fd_io.rs @@ -6,14 +6,9 @@ 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 { +pub unsafe fn test_open_read_write_0() { let mut path: *const libc::c_char = - (c"cpp2rust_fd_io_test.tmp".as_ptr().cast_mut()).cast_const(); + (c"/tmp/cpp2rust_fd_io_rw.tmp".as_ptr().cast_mut()).cast_const(); let mut fd: i32 = (unsafe { libc::open( path as *const i8, @@ -66,5 +61,369 @@ unsafe fn main_0() -> i32 { ); assert!(((((libc::close(fd)) == (0)) as i32) != 0)); assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); +} +pub unsafe fn test_pipe_1() { + let mut fds: [i32; 2] = [0_i32; 2]; + assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0)); + assert!( + ((((libc::write( + fds[(1) as usize], + (c"ab".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), + 2_usize + )) == (2_isize)) as i32) + != 0) + ); + let mut buf: [libc::c_char; 4] = [(0 as libc::c_char); 4]; + { + let byte_0 = (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::<[libc::c_char; 4]>() { + *byte_0.offset(offset as isize) = 0 as u8; + } + (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) + }; + assert!( + ((((libc::read( + fds[(0) as usize], + (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), + ::std::mem::size_of::<[libc::c_char; 4]>() + )) == (2_isize)) as i32) + != 0) + ); + assert!( + ((((libc::strcmp( + (buf.as_mut_ptr()).cast_const(), + (c"ab".as_ptr().cast_mut()).cast_const() + )) == (0)) as i32) + != 0) + ); + assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0)); + assert!( + ((((libc::read( + fds[(0) as usize], + (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), + ::std::mem::size_of::<[libc::c_char; 4]>() + )) == (0_isize)) as i32) + != 0) + ); + assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0)); +} +pub unsafe fn test_socket_listen_2() { + let mut s: i32 = libc::socket(libc::AF_INET, libc::SOCK_STREAM, 0); + assert!(((((s) >= (0)) as i32) != 0)); + assert!(((((libc::listen(s, 5)) == (0)) as i32) != 0)); + assert!(((((libc::close(s)) == (0)) as i32) != 0)); +} +pub unsafe fn test_sockopt_3() { + let mut s: i32 = libc::socket(libc::AF_INET, libc::SOCK_STREAM, 0); + assert!(((((s) >= (0)) as i32) != 0)); + let mut on: i32 = 1; + assert!( + ((((libc::setsockopt( + s, + 1, + 9, + ((&mut on as *mut i32) as *const i32 as *const ::libc::c_void), + (::std::mem::size_of::() as u32) + )) == (0)) as i32) + != 0) + ); + assert!( + ((((libc::setsockopt( + s, + libc::IPPROTO_TCP, + 1, + ((&mut on as *mut i32) as *const i32 as *const ::libc::c_void), + (::std::mem::size_of::() as u32) + )) == (0)) as i32) + != 0) + ); + let mut err: i32 = -1_i32; + let mut len: u32 = (::std::mem::size_of::() as u32); + assert!( + ((((libc::getsockopt( + s, + 1, + 4, + ((&mut err as *mut i32) as *mut i32 as *mut ::libc::c_void), + (&mut len as *mut u32) + )) == (0)) as i32) + != 0) + ); + assert!(((((err) == (0)) as i32) != 0)); + assert!(((((libc::close(s)) == (0)) as i32) != 0)); +} +pub unsafe fn test_lseek_4() { + let mut path: *const libc::c_char = + (c"/tmp/cpp2rust_fd_io_lseek.tmp".as_ptr().cast_mut()).cast_const(); + let mut fd: i32 = (unsafe { + libc::open( + path as *const i8, + (((::libc::O_RDWR) | (::libc::O_CREAT)) | (::libc::O_TRUNC)) as i32, + (420), + ) + }); + assert!(((((fd) >= (0)) as i32) != 0)); + assert!( + ((((libc::write( + fd, + (c"hello world".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), + 11_usize + )) == (11_isize)) as i32) + != 0) + ); + assert!(((((libc::lseek(fd, 0_i64, 2)) == (11_i64)) as i32) != 0)); + assert!(((((libc::lseek(fd, 6_i64, 0)) == (6_i64)) as i32) != 0)); + let mut buf: [libc::c_char; 16] = [(0 as libc::c_char); 16]; + { + let byte_0 = (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::<[libc::c_char; 16]>() { + *byte_0.offset(offset as isize) = 0 as u8; + } + (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) + }; + assert!( + ((((libc::read( + fd, + (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), + ::std::mem::size_of::<[libc::c_char; 16]>() + )) == (5_isize)) as i32) + != 0) + ); + assert!( + ((((libc::strcmp( + (buf.as_mut_ptr()).cast_const(), + (c"world".as_ptr().cast_mut()).cast_const() + )) == (0)) as i32) + != 0) + ); + assert!(((((libc::close(fd)) == (0)) as i32) != 0)); + assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); +} +pub unsafe fn test_ftruncate_5() { + let mut path: *const libc::c_char = + (c"/tmp/cpp2rust_fd_io_trunc.tmp".as_ptr().cast_mut()).cast_const(); + let mut fd: i32 = (unsafe { + libc::open( + path as *const i8, + (((::libc::O_RDWR) | (::libc::O_CREAT)) | (::libc::O_TRUNC)) as i32, + (420), + ) + }); + assert!(((((fd) >= (0)) as i32) != 0)); + assert!( + ((((libc::write( + fd, + (c"hello world".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), + 11_usize + )) == (11_isize)) as i32) + != 0) + ); + assert!(((((libc::ftruncate(fd, 5_i64)) == (0)) as i32) != 0)); + assert!(((((libc::lseek(fd, 0_i64, 2)) == (5_i64)) as i32) != 0)); + assert!(((((libc::close(fd)) == (0)) as i32) != 0)); + assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); +} +pub unsafe fn test_fstat_6() { + let mut path: *const libc::c_char = + (c"/tmp/cpp2rust_fd_io_fstat.tmp".as_ptr().cast_mut()).cast_const(); + let mut fd: i32 = (unsafe { + libc::open( + path as *const i8, + (((::libc::O_WRONLY) | (::libc::O_CREAT)) | (::libc::O_TRUNC)) as i32, + (420), + ) + }); + assert!(((((fd) >= (0)) as i32) != 0)); + assert!( + ((((libc::write( + fd, + (c"hello".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), + 5_usize + )) == (5_isize)) as i32) + != 0) + ); + let mut st: ::libc::stat = unsafe { std::mem::zeroed() }; + assert!(((((libc::fstat(fd, (&mut st as *mut ::libc::stat))) == (0)) as i32) != 0)); + assert!(((((st.st_size) == (5_i64)) as i32) != 0)); + assert!((((((st.st_mode) & (61440_u32)) == (32768_u32)) as i32) != 0)); + assert!(((((libc::close(fd)) == (0)) as i32) != 0)); + assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); +} +pub unsafe fn test_isatty_7() { + let mut path: *const libc::c_char = + (c"/tmp/cpp2rust_fd_io_tty.tmp".as_ptr().cast_mut()).cast_const(); + let mut fd: i32 = (unsafe { + libc::open( + path as *const i8, + ((::libc::O_RDONLY) | (::libc::O_CREAT)) as i32, + (420), + ) + }); + assert!(((((fd) >= (0)) as i32) != 0)); + assert!(((((libc::isatty(fd)) == (0)) as i32) != 0)); + assert!(((((libc::close(fd)) == (0)) as i32) != 0)); + assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); +} +pub unsafe fn test_tcgetattr_8() { + let mut path: *const libc::c_char = + (c"/tmp/cpp2rust_fd_io_termios.tmp".as_ptr().cast_mut()).cast_const(); + let mut fd: i32 = (unsafe { + libc::open( + path as *const i8, + ((::libc::O_RDONLY) | (::libc::O_CREAT)) as i32, + (420), + ) + }); + assert!(((((fd) >= (0)) as i32) != 0)); + let mut tio: ::libc::termios = unsafe { std::mem::zeroed() }; + assert!( + ((((libc::tcgetattr(fd, (&mut tio as *mut ::libc::termios))) == (-1_i32)) as i32) != 0) + ); + assert!(((((libc::close(fd)) == (0)) as i32) != 0)); + assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); +} +pub unsafe fn test_fcntl_9() { + let mut fds: [i32; 2] = [0_i32; 2]; + assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0)); + let mut flags: i32 = (unsafe { libc::fcntl(fds[(0) as usize] as i32, 3 as i32, (0)) }); + assert!(((((flags) >= (0)) as i32) != 0)); + assert!((((((flags) & (::libc::O_NONBLOCK)) == (0)) as i32) != 0)); + assert!( + ((((unsafe { + libc::fcntl( + fds[(0) as usize] as i32, + 4 as i32, + ((flags) | (::libc::O_NONBLOCK)), + ) + }) == (0)) as i32) + != 0) + ); + flags = (unsafe { libc::fcntl(fds[(0) as usize] as i32, 3 as i32, (0)) }); + assert!((((((flags) & (::libc::O_NONBLOCK)) != (0)) as i32) != 0)); + let mut b: libc::c_char = (0 as libc::c_char); + assert!( + ((((libc::read( + fds[(0) as usize], + ((&mut b as *mut libc::c_char) as *mut libc::c_char as *mut ::libc::c_void), + 1_usize + )) == (-1_i32 as isize)) as i32) + != 0) + ); + assert!( + ((((unsafe { libc::fcntl(fds[(0) as usize] as i32, 2 as i32, (1),) }) == (0)) as i32) != 0) + ); + assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0)); + assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0)); +} +pub unsafe fn test_select_10() { + let mut fds: [i32; 2] = [0_i32; 2]; + assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0)); + let mut rset: ::libc::fd_set = std::mem::zeroed::<::libc::fd_set>(); + libc::FD_ZERO((&mut rset as *mut ::libc::fd_set)); + libc::FD_SET(fds[(0) as usize], (&mut rset as *mut ::libc::fd_set)); + let mut tv: ::libc::timeval = unsafe { std::mem::zeroed() }; + tv.tv_sec = 0_i64; + tv.tv_usec = 0_i64; + assert!( + ((((libc::select( + ((fds[(0) as usize]) + (1)), + (&mut rset as *mut ::libc::fd_set), + std::ptr::null_mut(), + std::ptr::null_mut(), + (&mut tv as *mut ::libc::timeval) + )) == (0)) as i32) + != 0) + ); + assert!( + ((!(libc::FD_ISSET( + fds[(0) as usize], + (&mut rset as *mut ::libc::fd_set).cast_const() + ) as i32 + != 0) as i32) + != 0) + ); + assert!( + ((((libc::write( + fds[(1) as usize], + (c"x".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), + 1_usize + )) == (1_isize)) as i32) + != 0) + ); + libc::FD_ZERO((&mut rset as *mut ::libc::fd_set)); + libc::FD_SET(fds[(0) as usize], (&mut rset as *mut ::libc::fd_set)); + tv.tv_sec = 1_i64; + tv.tv_usec = 0_i64; + assert!( + ((((libc::select( + ((fds[(0) as usize]) + (1)), + (&mut rset as *mut ::libc::fd_set), + std::ptr::null_mut(), + std::ptr::null_mut(), + (&mut tv as *mut ::libc::timeval) + )) == (1)) as i32) + != 0) + ); + assert!( + (libc::FD_ISSET( + fds[(0) as usize], + (&mut rset as *mut ::libc::fd_set).cast_const() + ) as i32 + != 0) + ); + assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0)); + assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0)); +} +pub unsafe fn test_poll_11() { + let mut fds: [i32; 2] = [0_i32; 2]; + assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0)); + assert!( + ((((libc::write( + fds[(1) as usize], + (c"x".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), + 1_usize + )) == (1_isize)) as i32) + != 0) + ); + let mut pfd: [::libc::pollfd; 2] = [unsafe { std::mem::zeroed() }; 2]; + pfd[(0) as usize].fd = fds[(0) as usize]; + pfd[(0) as usize].events = 1_i16; + pfd[(0) as usize].revents = 0_i16; + pfd[(1) as usize].fd = -1_i32; + pfd[(1) as usize].events = 1_i16; + pfd[(1) as usize].revents = 42_i16; + assert!(((((libc::poll(pfd.as_mut_ptr(), 2_u64 as ::libc::nfds_t, 0)) == (1)) as i32) != 0)); + assert!((((((pfd[(0) as usize].revents as i32) & (1)) != (0)) as i32) != 0)); + assert!(((((pfd[(1) as usize].revents as i32) == (0)) as i32) != 0)); + let mut ch: libc::c_char = (0 as libc::c_char); + assert!( + ((((libc::read( + fds[(0) as usize], + ((&mut ch as *mut libc::c_char) as *mut libc::c_char as *mut ::libc::c_void), + 1_usize + )) == (1_isize)) as i32) + != 0) + ); + assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0)); + assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0)); +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_open_read_write_0() }); + (unsafe { test_pipe_1() }); + (unsafe { test_socket_listen_2() }); + (unsafe { test_sockopt_3() }); + (unsafe { test_lseek_4() }); + (unsafe { test_ftruncate_5() }); + (unsafe { test_fstat_6() }); + (unsafe { test_isatty_7() }); + (unsafe { test_tcgetattr_8() }); + (unsafe { test_fcntl_9() }); + (unsafe { test_select_10() }); + (unsafe { test_poll_11() }); return 0; } From 3ba18d55a95fe49c4f63113905f60105fbf22f07 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 23 Jul 2026 15:45:30 +0100 Subject: [PATCH 2/9] Update tests --- tests/unit/out/refcount/fd_io.rs | 1536 +----------------------------- tests/unit/out/unsafe/fd_io.rs | 373 +------- 2 files changed, 12 insertions(+), 1897 deletions(-) diff --git a/tests/unit/out/refcount/fd_io.rs b/tests/unit/out/refcount/fd_io.rs index d1df629a..a8be4c06 100644 --- a/tests/unit/out/refcount/fd_io.rs +++ b/tests/unit/out/refcount/fd_io.rs @@ -6,9 +6,12 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -pub fn test_open_read_write_0() { +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_fd_io_rw.tmp", + b"/tmp/cpp2rust_fd_io_test.tmp", ))); let fd: Value = Rc::new(RefCell::new({ let __mode = match &[(420).into()].first() { @@ -135,1534 +138,5 @@ pub fn test_open_read_write_0() { } == 0) as i32) != 0) ); -} -pub fn test_pipe_1() { - let fds: Value> = Rc::new(RefCell::new( - (0..2).map(|_| ::default()).collect::>(), - )); - assert!( - (((match nix::unistd::pipe() { - Ok((__r, __w)) => { - let __fds = (fds.as_pointer() as Ptr).clone(); - __fds.write(FdRegistry::register(__r)); - __fds.offset(1).write(FdRegistry::register(__w)); - 0 - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); - assert!( - (((match FdRegistry::with_fd((*fds.borrow())[(1) as usize], |__fd| { - Ptr::from_string_literal(b"ab") - .to_any() - .reinterpret_cast::() - .with_slice(2_usize, |__buf| nix::unistd::write(__fd, __buf)) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 2_isize) as i32) - != 0) - ); - let buf: Value> = Rc::new(RefCell::new( - (0..4).map(|_| ::default()).collect::>(), - )); - { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .memset((0) as u8, ::std::mem::size_of::<[u8; 4]>() as usize); - ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() - }; - assert!( - (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .with_slice_mut(::std::mem::size_of::<[u8; 4]>(), |__buf| { - nix::unistd::read(__fd, __buf) - }) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 2_isize) as i32) - != 0) - ); - assert!( - ((({ - let mut __it1 = (buf.as_pointer() as Ptr).to_c_string_iterator(); - let mut __it2 = Ptr::from_string_literal(b"ab").to_c_string_iterator(); - loop { - let __c1 = __it1.next(); - let __c2 = __it2.next(); - if __c1 != __c2 { - break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); - } - if __c1.is_none() { - break 0; - } - } - } == 0) as i32) - != 0) - ); - assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); - assert!( - (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .with_slice_mut(::std::mem::size_of::<[u8; 4]>(), |__buf| { - nix::unistd::read(__fd, __buf) - }) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0_isize) as i32) - != 0) - ); - assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); -} -pub fn test_socket_listen_2() { - let s: Value = Rc::new(RefCell::new({ - let __family = match libc::AF_INET { - ::libc::AF_INET => nix::sys::socket::AddressFamily::Inet, - ::libc::AF_INET6 => nix::sys::socket::AddressFamily::Inet6, - ::libc::AF_UNIX => nix::sys::socket::AddressFamily::Unix, - __d => panic!("socket: unsupported domain {__d}"), - }; - let __flags = nix::sys::socket::SockFlag::from_bits_truncate(libc::SOCK_STREAM); - let __ty = match libc::SOCK_STREAM & !nix::sys::socket::SockFlag::all().bits() { - ::libc::SOCK_STREAM => nix::sys::socket::SockType::Stream, - ::libc::SOCK_DGRAM => nix::sys::socket::SockType::Datagram, - __t => panic!("socket: unsupported type {__t}"), - }; - let __proto = match 0 { - 0 => None, - ::libc::IPPROTO_TCP => Some(nix::sys::socket::SockProtocol::Tcp), - ::libc::IPPROTO_UDP => Some(nix::sys::socket::SockProtocol::Udp), - __p => panic!("socket: unsupported protocol {__p}"), - }; - match nix::sys::socket::socket(__family, __ty, __flags, __proto) { - Ok(__ofd) => FdRegistry::register(__ofd), - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - })); - assert!(((((*s.borrow()) >= 0) as i32) != 0)); - assert!( - (((match nix::sys::socket::Backlog::new(5) { - Ok(__b) => match FdRegistry::with_fd((*s.borrow()), |__fd| nix::sys::socket::listen( - &__fd, __b - )) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - }, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); - assert!((((FdRegistry::close((*s.borrow())) == 0) as i32) != 0)); -} -pub fn test_sockopt_3() { - let s: Value = Rc::new(RefCell::new({ - let __family = match libc::AF_INET { - ::libc::AF_INET => nix::sys::socket::AddressFamily::Inet, - ::libc::AF_INET6 => nix::sys::socket::AddressFamily::Inet6, - ::libc::AF_UNIX => nix::sys::socket::AddressFamily::Unix, - __d => panic!("socket: unsupported domain {__d}"), - }; - let __flags = nix::sys::socket::SockFlag::from_bits_truncate(libc::SOCK_STREAM); - let __ty = match libc::SOCK_STREAM & !nix::sys::socket::SockFlag::all().bits() { - ::libc::SOCK_STREAM => nix::sys::socket::SockType::Stream, - ::libc::SOCK_DGRAM => nix::sys::socket::SockType::Datagram, - __t => panic!("socket: unsupported type {__t}"), - }; - let __proto = match 0 { - 0 => None, - ::libc::IPPROTO_TCP => Some(nix::sys::socket::SockProtocol::Tcp), - ::libc::IPPROTO_UDP => Some(nix::sys::socket::SockProtocol::Udp), - __p => panic!("socket: unsupported protocol {__p}"), - }; - match nix::sys::socket::socket(__family, __ty, __flags, __proto) { - Ok(__ofd) => FdRegistry::register(__ofd), - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - })); - assert!(((((*s.borrow()) >= 0) as i32) != 0)); - let on: Value = Rc::new(RefCell::new(1)); - assert!( - ((({ - let __res = match (1, 9) { - (::libc::IPPROTO_TCP, ::libc::TCP_NODELAY) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read() - != 0; - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::TcpNoDelay, - &__v, - ) - }) - } - (::libc::SOL_SOCKET, ::libc::SO_KEEPALIVE) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read() - != 0; - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::KeepAlive, - &__v, - ) - }) - } - (::libc::IPPROTO_TCP, ::libc::TCP_KEEPINTVL) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::TcpKeepInterval, - &__v, - ) - }) - } - (::libc::IPPROTO_TCP, ::libc::TCP_KEEPCNT) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::TcpKeepCount, - &__v, - ) - }) - } - (::libc::IPPROTO_IP, ::libc::IP_TOS) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::Ipv4Tos, - &__v, - ) - }) - } - (::libc::IPPROTO_IPV6, ::libc::IPV6_TCLASS) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::Ipv6TClass, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::IPPROTO_TCP, ::libc::TCP_KEEPIDLE) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::TcpKeepIdle, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::SOL_SOCKET, ::libc::SO_BINDTODEVICE) => { - let __v = ::std::ffi::OsString::from( - ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .to_rust_string(), - ); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::BindToDevice, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::IPPROTO_IP, ::libc::IP_BIND_ADDRESS_NO_PORT) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read() - != 0; - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::IpBindAddressNoPort, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::IPPROTO_TCP, ::libc::TCP_FASTOPEN_CONNECT) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read() - != 0; - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::TcpFastOpenConnect, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::SOL_SOCKET, ::libc::SO_PRIORITY) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::Priority, - &__v, - ) - }) - } - (__l, __o) => panic!( - "setsockopt: unsupported option (level={}, optname={})", - __l, __o - ), - }; - match __res { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } == 0) as i32) - != 0) - ); - assert!( - ((({ - let __res = match (libc::IPPROTO_TCP, 1) { - (::libc::IPPROTO_TCP, ::libc::TCP_NODELAY) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read() - != 0; - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::TcpNoDelay, - &__v, - ) - }) - } - (::libc::SOL_SOCKET, ::libc::SO_KEEPALIVE) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read() - != 0; - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::KeepAlive, - &__v, - ) - }) - } - (::libc::IPPROTO_TCP, ::libc::TCP_KEEPINTVL) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::TcpKeepInterval, - &__v, - ) - }) - } - (::libc::IPPROTO_TCP, ::libc::TCP_KEEPCNT) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::TcpKeepCount, - &__v, - ) - }) - } - (::libc::IPPROTO_IP, ::libc::IP_TOS) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::Ipv4Tos, - &__v, - ) - }) - } - (::libc::IPPROTO_IPV6, ::libc::IPV6_TCLASS) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::Ipv6TClass, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::IPPROTO_TCP, ::libc::TCP_KEEPIDLE) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::TcpKeepIdle, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::SOL_SOCKET, ::libc::SO_BINDTODEVICE) => { - let __v = ::std::ffi::OsString::from( - ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .to_rust_string(), - ); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::BindToDevice, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::IPPROTO_IP, ::libc::IP_BIND_ADDRESS_NO_PORT) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read() - != 0; - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::IpBindAddressNoPort, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::IPPROTO_TCP, ::libc::TCP_FASTOPEN_CONNECT) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read() - != 0; - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::TcpFastOpenConnect, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::SOL_SOCKET, ::libc::SO_PRIORITY) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::Priority, - &__v, - ) - }) - } - (__l, __o) => panic!( - "setsockopt: unsupported option (level={}, optname={})", - __l, __o - ), - }; - match __res { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } == 0) as i32) - != 0) - ); - let err: Value = Rc::new(RefCell::new(-1_i32)); - let len: Value = Rc::new(RefCell::new((::std::mem::size_of::() as u32))); - assert!( - (((match (1, 4) { - (::libc::SOL_SOCKET, ::libc::SO_ERROR) => { - match FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::getsockopt(&__fd, nix::sys::socket::sockopt::SocketError) - }) { - Ok(__err) => { - ((err.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .write(__err); - (len.as_pointer()).write(::std::mem::size_of::() as u32); - 0 - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } - (__l, __o) => panic!( - "getsockopt: unsupported option (level={}, optname={})", - __l, __o - ), - } == 0) as i32) - != 0) - ); - assert!(((((*err.borrow()) == 0) as i32) != 0)); - assert!((((FdRegistry::close((*s.borrow())) == 0) as i32) != 0)); -} -pub fn test_lseek_4() { - let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_fd_io_lseek.tmp", - ))); - let fd: Value = Rc::new(RefCell::new({ - let __mode = match &[(420).into()].first() { - Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), - None => nix::sys::stat::Mode::empty(), - }; - match nix::fcntl::open( - (*path.borrow()).to_rust_string().as_str(), - nix::fcntl::OFlag::from_bits_retain( - ((::libc::O_RDWR | ::libc::O_CREAT) | ::libc::O_TRUNC), - ), - __mode, - ) { - Ok(__ofd) => FdRegistry::register(__ofd), - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - })); - assert!(((((*fd.borrow()) >= 0) as i32) != 0)); - assert!( - (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { - Ptr::from_string_literal(b"hello world") - .to_any() - .reinterpret_cast::() - .with_slice(11_usize, |__buf| nix::unistd::write(__fd, __buf)) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 11_isize) as i32) - != 0) - ); - assert!( - ((({ - let __whence = match 2 { - 0 => nix::unistd::Whence::SeekSet, - 1 => nix::unistd::Whence::SeekCur, - 2 => nix::unistd::Whence::SeekEnd, - __w => panic!("lseek: unsupported whence {__w}"), - }; - match FdRegistry::with_fd((*fd.borrow()), |__fd| { - nix::unistd::lseek(__fd, 0_i64, __whence) - }) { - Ok(__off) => __off, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } == 11_i64) as i32) - != 0) - ); - assert!( - ((({ - let __whence = match 0 { - 0 => nix::unistd::Whence::SeekSet, - 1 => nix::unistd::Whence::SeekCur, - 2 => nix::unistd::Whence::SeekEnd, - __w => panic!("lseek: unsupported whence {__w}"), - }; - match FdRegistry::with_fd((*fd.borrow()), |__fd| { - nix::unistd::lseek(__fd, 6_i64, __whence) - }) { - Ok(__off) => __off, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } == 6_i64) as i32) - != 0) - ); - let buf: Value> = Rc::new(RefCell::new( - (0..16).map(|_| ::default()).collect::>(), - )); - { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .memset((0) as u8, ::std::mem::size_of::<[u8; 16]>() as usize); - ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() - }; - assert!( - (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .with_slice_mut(::std::mem::size_of::<[u8; 16]>(), |__buf| { - nix::unistd::read(__fd, __buf) - }) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 5_isize) as i32) - != 0) - ); - assert!( - ((({ - let mut __it1 = (buf.as_pointer() as Ptr).to_c_string_iterator(); - let mut __it2 = Ptr::from_string_literal(b"world").to_c_string_iterator(); - loop { - let __c1 = __it1.next(); - let __c2 = __it2.next(); - if __c1 != __c2 { - break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); - } - if __c1.is_none() { - break 0; - } - } - } == 0) as i32) - != 0) - ); - assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); - assert!( - (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); -} -pub fn test_ftruncate_5() { - let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_fd_io_trunc.tmp", - ))); - let fd: Value = Rc::new(RefCell::new({ - let __mode = match &[(420).into()].first() { - Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), - None => nix::sys::stat::Mode::empty(), - }; - match nix::fcntl::open( - (*path.borrow()).to_rust_string().as_str(), - nix::fcntl::OFlag::from_bits_retain( - ((::libc::O_RDWR | ::libc::O_CREAT) | ::libc::O_TRUNC), - ), - __mode, - ) { - Ok(__ofd) => FdRegistry::register(__ofd), - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - })); - assert!(((((*fd.borrow()) >= 0) as i32) != 0)); - assert!( - (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { - Ptr::from_string_literal(b"hello world") - .to_any() - .reinterpret_cast::() - .with_slice(11_usize, |__buf| nix::unistd::write(__fd, __buf)) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 11_isize) as i32) - != 0) - ); - assert!( - (((match FdRegistry::with_fd((*fd.borrow()), |__fd| nix::unistd::ftruncate(__fd, 5_i64)) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); - assert!( - ((({ - let __whence = match 2 { - 0 => nix::unistd::Whence::SeekSet, - 1 => nix::unistd::Whence::SeekCur, - 2 => nix::unistd::Whence::SeekEnd, - __w => panic!("lseek: unsupported whence {__w}"), - }; - match FdRegistry::with_fd((*fd.borrow()), |__fd| { - nix::unistd::lseek(__fd, 0_i64, __whence) - }) { - Ok(__off) => __off, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } == 5_i64) as i32) - != 0) - ); - assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); - assert!( - (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); -} -pub fn test_fstat_6() { - let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_fd_io_fstat.tmp", - ))); - let fd: Value = Rc::new(RefCell::new({ - let __mode = match &[(420).into()].first() { - Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), - None => nix::sys::stat::Mode::empty(), - }; - match nix::fcntl::open( - (*path.borrow()).to_rust_string().as_str(), - nix::fcntl::OFlag::from_bits_retain( - ((::libc::O_WRONLY | ::libc::O_CREAT) | ::libc::O_TRUNC), - ), - __mode, - ) { - Ok(__ofd) => FdRegistry::register(__ofd), - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - })); - assert!(((((*fd.borrow()) >= 0) as i32) != 0)); - assert!( - (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { - Ptr::from_string_literal(b"hello") - .to_any() - .reinterpret_cast::() - .with_slice(5_usize, |__buf| nix::unistd::write(__fd, __buf)) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 5_isize) as i32) - != 0) - ); - let st: Value = Rc::new(RefCell::new(Default::default())); - assert!( - (((match FdRegistry::with_fd((*fd.borrow()), |__fd| nix::sys::stat::fstat(__fd)) { - Ok(__s) => { - (st.as_pointer()).with_mut(|__st| *__st = Stat::from_libc(&__s)); - 0 - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); - assert!(((((*(*st.borrow()).st_size.borrow()) == 5_i64) as i32) != 0)); - assert!((((((*(*st.borrow()).st_mode.borrow()) & 61440_u32) == 32768_u32) as i32) != 0)); - assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); - assert!( - (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); -} -pub fn test_isatty_7() { - let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_fd_io_tty.tmp", - ))); - let fd: Value = Rc::new(RefCell::new({ - let __mode = match &[(420).into()].first() { - Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), - None => nix::sys::stat::Mode::empty(), - }; - match nix::fcntl::open( - (*path.borrow()).to_rust_string().as_str(), - nix::fcntl::OFlag::from_bits_retain((::libc::O_RDONLY | ::libc::O_CREAT)), - __mode, - ) { - Ok(__ofd) => FdRegistry::register(__ofd), - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - })); - assert!(((((*fd.borrow()) >= 0) as i32) != 0)); - assert!( - (((match FdRegistry::with_fd((*fd.borrow()), |__fd| nix::unistd::isatty(__fd)) { - Ok(__tty) => __tty as i32, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - 0 - } - } == 0) as i32) - != 0) - ); - assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); - assert!( - (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); -} -pub fn test_tcgetattr_8() { - let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_fd_io_termios.tmp", - ))); - let fd: Value = Rc::new(RefCell::new({ - let __mode = match &[(420).into()].first() { - Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), - None => nix::sys::stat::Mode::empty(), - }; - match nix::fcntl::open( - (*path.borrow()).to_rust_string().as_str(), - nix::fcntl::OFlag::from_bits_retain((::libc::O_RDONLY | ::libc::O_CREAT)), - __mode, - ) { - Ok(__ofd) => FdRegistry::register(__ofd), - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - })); - assert!(((((*fd.borrow()) >= 0) as i32) != 0)); - let tio: Value = Rc::new(RefCell::new(Default::default())); - assert!( - (((match FdRegistry::with_fd((*fd.borrow()), |__fd| nix::sys::termios::tcgetattr(__fd)) { - Ok(__t) => { - (tio.as_pointer()).with_mut(|__dst| *__dst = Termios::from_libc(&__t.into())); - 0 - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == -1_i32) as i32) - != 0) - ); - assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); - assert!( - (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); -} -pub fn test_fcntl_9() { - let fds: Value> = Rc::new(RefCell::new( - (0..2).map(|_| ::default()).collect::>(), - )); - assert!( - (((match nix::unistd::pipe() { - Ok((__r, __w)) => { - let __fds = (fds.as_pointer() as Ptr).clone(); - __fds.write(FdRegistry::register(__r)); - __fds.offset(1).write(FdRegistry::register(__w)); - 0 - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); - let flags: Value = Rc::new(RefCell::new({ - let __res = match 3 { - ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) - }), - ::libc::F_SETFL => { - let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(0).into()][0])); - FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) - }) - } - ::libc::F_SETFD => { - let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(0).into()][0])); - FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) - }) - } - __cmd => panic!("fcntl: unsupported cmd {}", __cmd), - }; - match __res { - Ok(__r) => __r, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - })); - assert!(((((*flags.borrow()) >= 0) as i32) != 0)); - assert!((((((*flags.borrow()) & ::libc::O_NONBLOCK) == 0) as i32) != 0)); - assert!( - ((({ - let __res = match 4 { - ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) - }), - ::libc::F_SETFL => { - let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get( - &&[((*flags.borrow()) | ::libc::O_NONBLOCK).into()][0], - )); - FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) - }) - } - ::libc::F_SETFD => { - let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get( - &&[((*flags.borrow()) | ::libc::O_NONBLOCK).into()][0], - )); - FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) - }) - } - __cmd => panic!("fcntl: unsupported cmd {}", __cmd), - }; - match __res { - Ok(__r) => __r, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } == 0) as i32) - != 0) - ); - (*flags.borrow_mut()) = { - let __res = match 3 { - ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) - }), - ::libc::F_SETFL => { - let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(0).into()][0])); - FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) - }) - } - ::libc::F_SETFD => { - let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(0).into()][0])); - FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) - }) - } - __cmd => panic!("fcntl: unsupported cmd {}", __cmd), - }; - match __res { - Ok(__r) => __r, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - }; - assert!((((((*flags.borrow()) & ::libc::O_NONBLOCK) != 0) as i32) != 0)); - let b: Value = >::default(); - assert!( - (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - ((b.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .with_slice_mut(1_usize, |__buf| nix::unistd::read(__fd, __buf)) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == (-1_i32 as isize)) as i32) - != 0) - ); - assert!( - ((({ - let __res = match 2 { - ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) - }), - ::libc::F_SETFL => { - let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(1).into()][0])); - FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) - }) - } - ::libc::F_SETFD => { - let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(1).into()][0])); - FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) - }) - } - __cmd => panic!("fcntl: unsupported cmd {}", __cmd), - }; - match __res { - Ok(__r) => __r, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } == 0) as i32) - != 0) - ); - assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); - assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); -} -pub fn test_select_10() { - let fds: Value> = Rc::new(RefCell::new( - (0..2).map(|_| ::default()).collect::>(), - )); - assert!( - (((match nix::unistd::pipe() { - Ok((__r, __w)) => { - let __fds = (fds.as_pointer() as Ptr).clone(); - __fds.write(FdRegistry::register(__r)); - __fds.offset(1).write(FdRegistry::register(__w)); - 0 - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); - let rset: Value = Rc::new(RefCell::new(Default::default())); - (rset.as_pointer()).with_mut(|__s| __s.zero()); - (rset.as_pointer()).with_mut(|__s| __s.set((*fds.borrow())[(0) as usize])); - let tv: Value = Rc::new(RefCell::new(Default::default())); - (*(*tv.borrow()).tv_sec.borrow_mut()) = 0_i64; - (*(*tv.borrow()).tv_usec.borrow_mut()) = 0_i64; - assert!( - ((({ - let __rp = (rset.as_pointer()).clone(); - let __wp = Ptr::::null().clone(); - let __ep = Ptr::::null().clone(); - let __tp = (tv.as_pointer()).clone(); - let __r_fds: Vec = match __rp.is_null() { - true => Vec::new(), - false => __rp.with(|__s| { - (0..((*fds.borrow())[(0) as usize] + 1)) - .filter(|&__fd| __s.isset(__fd)) - .collect() - }), - }; - let __w_fds: Vec = match __wp.is_null() { - true => Vec::new(), - false => __wp.with(|__s| { - (0..((*fds.borrow())[(0) as usize] + 1)) - .filter(|&__fd| __s.isset(__fd)) - .collect() - }), - }; - let __e_fds: Vec = match __ep.is_null() { - true => Vec::new(), - false => __ep.with(|__s| { - (0..((*fds.borrow())[(0) as usize] + 1)) - .filter(|&__fd| __s.isset(__fd)) - .collect() - }), - }; - let mut __wanted = Vec::new(); - __wanted.extend(&__r_fds); - __wanted.extend(&__w_fds); - __wanted.extend(&__e_fds); - FdRegistry::with_fds(&__wanted, |__b| { - let __rn = __r_fds.len(); - let __wn = __w_fds.len(); - let mut __rs = nix::sys::select::FdSet::new(); - let mut __ws = nix::sys::select::FdSet::new(); - let mut __es = nix::sys::select::FdSet::new(); - for __bfd in &__b[..__rn] { - __rs.insert(*__bfd); - } - for __bfd in &__b[__rn..__rn + __wn] { - __ws.insert(*__bfd); - } - for __bfd in &__b[__rn + __wn..] { - __es.insert(*__bfd); - } - let mut __tv = match __tp.is_null() { - true => None, - false => Some(__tp.with(|__t| { - nix::sys::time::TimeVal::new( - *__t.tv_sec.borrow() as _, - *__t.tv_usec.borrow() as _, - ) - })), - }; - match nix::sys::select::select( - ((*fds.borrow())[(0) as usize] + 1), - match __rp.is_null() { - true => None, - false => Some(&mut __rs), - }, - match __wp.is_null() { - true => None, - false => Some(&mut __ws), - }, - match __ep.is_null() { - true => None, - false => Some(&mut __es), - }, - __tv.as_mut(), - ) { - Ok(__n) => { - if !__rp.is_null() { - __rp.with_mut(|__s| { - __s.zero(); - for (__fd, __bfd) in __r_fds.iter().zip(&__b[..__rn]) { - if __rs.contains(*__bfd) { - __s.set(*__fd); - } - } - }); - } - if !__wp.is_null() { - __wp.with_mut(|__s| { - __s.zero(); - for (__fd, __bfd) in __w_fds.iter().zip(&__b[__rn..__rn + __wn]) { - if __ws.contains(*__bfd) { - __s.set(*__fd); - } - } - }); - } - if !__ep.is_null() { - __ep.with_mut(|__s| { - __s.zero(); - for (__fd, __bfd) in __e_fds.iter().zip(&__b[__rn + __wn..]) { - if __es.contains(*__bfd) { - __s.set(*__fd); - } - } - }); - } - match (__tp.is_null(), __tv.as_ref()) { - (false, Some(__t)) => __tp.with_mut(|__dst| { - *__dst.tv_sec.borrow_mut() = __t.tv_sec() as i64; - *__dst.tv_usec.borrow_mut() = __t.tv_usec() as i64; - }), - _ => {} - } - __n - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - }) - } == 0) as i32) - != 0) - ); - assert!( - ((!(if (rset.as_pointer()).with(|__s| __s.isset((*fds.borrow())[(0) as usize])) { - 1 - } else { - 0 - } != 0) as i32) - != 0) - ); - assert!( - (((match FdRegistry::with_fd((*fds.borrow())[(1) as usize], |__fd| { - Ptr::from_string_literal(b"x") - .to_any() - .reinterpret_cast::() - .with_slice(1_usize, |__buf| nix::unistd::write(__fd, __buf)) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 1_isize) as i32) - != 0) - ); - (rset.as_pointer()).with_mut(|__s| __s.zero()); - (rset.as_pointer()).with_mut(|__s| __s.set((*fds.borrow())[(0) as usize])); - (*(*tv.borrow()).tv_sec.borrow_mut()) = 1_i64; - (*(*tv.borrow()).tv_usec.borrow_mut()) = 0_i64; - assert!( - ((({ - let __rp = (rset.as_pointer()).clone(); - let __wp = Ptr::::null().clone(); - let __ep = Ptr::::null().clone(); - let __tp = (tv.as_pointer()).clone(); - let __r_fds: Vec = match __rp.is_null() { - true => Vec::new(), - false => __rp.with(|__s| { - (0..((*fds.borrow())[(0) as usize] + 1)) - .filter(|&__fd| __s.isset(__fd)) - .collect() - }), - }; - let __w_fds: Vec = match __wp.is_null() { - true => Vec::new(), - false => __wp.with(|__s| { - (0..((*fds.borrow())[(0) as usize] + 1)) - .filter(|&__fd| __s.isset(__fd)) - .collect() - }), - }; - let __e_fds: Vec = match __ep.is_null() { - true => Vec::new(), - false => __ep.with(|__s| { - (0..((*fds.borrow())[(0) as usize] + 1)) - .filter(|&__fd| __s.isset(__fd)) - .collect() - }), - }; - let mut __wanted = Vec::new(); - __wanted.extend(&__r_fds); - __wanted.extend(&__w_fds); - __wanted.extend(&__e_fds); - FdRegistry::with_fds(&__wanted, |__b| { - let __rn = __r_fds.len(); - let __wn = __w_fds.len(); - let mut __rs = nix::sys::select::FdSet::new(); - let mut __ws = nix::sys::select::FdSet::new(); - let mut __es = nix::sys::select::FdSet::new(); - for __bfd in &__b[..__rn] { - __rs.insert(*__bfd); - } - for __bfd in &__b[__rn..__rn + __wn] { - __ws.insert(*__bfd); - } - for __bfd in &__b[__rn + __wn..] { - __es.insert(*__bfd); - } - let mut __tv = match __tp.is_null() { - true => None, - false => Some(__tp.with(|__t| { - nix::sys::time::TimeVal::new( - *__t.tv_sec.borrow() as _, - *__t.tv_usec.borrow() as _, - ) - })), - }; - match nix::sys::select::select( - ((*fds.borrow())[(0) as usize] + 1), - match __rp.is_null() { - true => None, - false => Some(&mut __rs), - }, - match __wp.is_null() { - true => None, - false => Some(&mut __ws), - }, - match __ep.is_null() { - true => None, - false => Some(&mut __es), - }, - __tv.as_mut(), - ) { - Ok(__n) => { - if !__rp.is_null() { - __rp.with_mut(|__s| { - __s.zero(); - for (__fd, __bfd) in __r_fds.iter().zip(&__b[..__rn]) { - if __rs.contains(*__bfd) { - __s.set(*__fd); - } - } - }); - } - if !__wp.is_null() { - __wp.with_mut(|__s| { - __s.zero(); - for (__fd, __bfd) in __w_fds.iter().zip(&__b[__rn..__rn + __wn]) { - if __ws.contains(*__bfd) { - __s.set(*__fd); - } - } - }); - } - if !__ep.is_null() { - __ep.with_mut(|__s| { - __s.zero(); - for (__fd, __bfd) in __e_fds.iter().zip(&__b[__rn + __wn..]) { - if __es.contains(*__bfd) { - __s.set(*__fd); - } - } - }); - } - match (__tp.is_null(), __tv.as_ref()) { - (false, Some(__t)) => __tp.with_mut(|__dst| { - *__dst.tv_sec.borrow_mut() = __t.tv_sec() as i64; - *__dst.tv_usec.borrow_mut() = __t.tv_usec() as i64; - }), - _ => {} - } - __n - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - }) - } == 1) as i32) - != 0) - ); - assert!( - (if (rset.as_pointer()).with(|__s| __s.isset((*fds.borrow())[(0) as usize])) { - 1 - } else { - 0 - } != 0) - ); - assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); - assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); -} -pub fn test_poll_11() { - let fds: Value> = Rc::new(RefCell::new( - (0..2).map(|_| ::default()).collect::>(), - )); - assert!( - (((match nix::unistd::pipe() { - Ok((__r, __w)) => { - let __fds = (fds.as_pointer() as Ptr).clone(); - __fds.write(FdRegistry::register(__r)); - __fds.offset(1).write(FdRegistry::register(__w)); - 0 - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); - assert!( - (((match FdRegistry::with_fd((*fds.borrow())[(1) as usize], |__fd| { - Ptr::from_string_literal(b"x") - .to_any() - .reinterpret_cast::() - .with_slice(1_usize, |__buf| nix::unistd::write(__fd, __buf)) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 1_isize) as i32) - != 0) - ); - let pfd: Value> = Rc::new(RefCell::new( - (0..2) - .map(|_| Default::default()) - .collect::>(), - )); - (*(*pfd.borrow())[(0) as usize].fd.borrow_mut()) = (*fds.borrow())[(0) as usize]; - (*(*pfd.borrow())[(0) as usize].events.borrow_mut()) = 1_i16; - (*(*pfd.borrow())[(0) as usize].revents.borrow_mut()) = 0_i16; - (*(*pfd.borrow())[(1) as usize].fd.borrow_mut()) = -1_i32; - (*(*pfd.borrow())[(1) as usize].events.borrow_mut()) = 1_i16; - (*(*pfd.borrow())[(1) as usize].revents.borrow_mut()) = 42_i16; - assert!( - ((({ - let __p = (pfd.as_pointer() as Ptr).clone(); - let __timeout = match nix::poll::PollTimeout::try_from(0) { - Ok(__t) => __t, - Err(_) => panic!("poll: unsupported timeout {}", 0), - }; - let mut __idx = Vec::new(); - let mut __wanted = Vec::new(); - let mut __events = Vec::new(); - for __i in 0..(2_u64 as usize) { - let (__fd, __ev) = __p - .offset(__i) - .with(|__e| (*__e.fd.borrow(), *__e.events.borrow())); - __p.offset(__i) - .with_mut(|__e| *__e.revents.borrow_mut() = 0); - if __fd >= 0 { - __idx.push(__i); - __wanted.push(__fd); - __events.push(__ev); - } - } - FdRegistry::with_fds(&__wanted, |__b| { - let mut __pfds: Vec = __b - .iter() - .zip(&__events) - .map(|(&__fd, &__ev)| { - nix::poll::PollFd::new(__fd, nix::poll::PollFlags::from_bits_truncate(__ev)) - }) - .collect(); - match nix::poll::poll(&mut __pfds, __timeout) { - Ok(__count) => { - for (__slot, &__i) in __pfds.iter().zip(&__idx) { - let __rev = match __slot.revents() { - Some(__r) => __r.bits(), - None => 0, - }; - __p.offset(__i) - .with_mut(|__e| *__e.revents.borrow_mut() = __rev); - } - __count - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - }) - } == 1) as i32) - != 0) - ); - assert!( - ((((((*(*pfd.borrow())[(0) as usize].revents.borrow()) as i32) & 1) != 0) as i32) != 0) - ); - assert!((((((*(*pfd.borrow())[(1) as usize].revents.borrow()) as i32) == 0) as i32) != 0)); - let ch: Value = >::default(); - assert!( - (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - ((ch.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .with_slice_mut(1_usize, |__buf| nix::unistd::read(__fd, __buf)) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 1_isize) as i32) - != 0) - ); - assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); - assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); -} -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - ({ test_open_read_write_0() }); - ({ test_pipe_1() }); - ({ test_socket_listen_2() }); - ({ test_sockopt_3() }); - ({ test_lseek_4() }); - ({ test_ftruncate_5() }); - ({ test_fstat_6() }); - ({ test_isatty_7() }); - ({ test_tcgetattr_8() }); - ({ test_fcntl_9() }); - ({ test_select_10() }); - ({ test_poll_11() }); return 0; } diff --git a/tests/unit/out/unsafe/fd_io.rs b/tests/unit/out/unsafe/fd_io.rs index 626d6aa8..29c8057d 100644 --- a/tests/unit/out/unsafe/fd_io.rs +++ b/tests/unit/out/unsafe/fd_io.rs @@ -6,9 +6,14 @@ 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_open_read_write_0() { +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { let mut path: *const libc::c_char = - (c"/tmp/cpp2rust_fd_io_rw.tmp".as_ptr().cast_mut()).cast_const(); + (c"/tmp/cpp2rust_fd_io_test.tmp".as_ptr().cast_mut()).cast_const(); let mut fd: i32 = (unsafe { libc::open( path as *const i8, @@ -61,369 +66,5 @@ pub unsafe fn test_open_read_write_0() { ); assert!(((((libc::close(fd)) == (0)) as i32) != 0)); assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); -} -pub unsafe fn test_pipe_1() { - let mut fds: [i32; 2] = [0_i32; 2]; - assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0)); - assert!( - ((((libc::write( - fds[(1) as usize], - (c"ab".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), - 2_usize - )) == (2_isize)) as i32) - != 0) - ); - let mut buf: [libc::c_char; 4] = [(0 as libc::c_char); 4]; - { - let byte_0 = (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) as *mut u8; - for offset in 0..::std::mem::size_of::<[libc::c_char; 4]>() { - *byte_0.offset(offset as isize) = 0 as u8; - } - (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) - }; - assert!( - ((((libc::read( - fds[(0) as usize], - (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), - ::std::mem::size_of::<[libc::c_char; 4]>() - )) == (2_isize)) as i32) - != 0) - ); - assert!( - ((((libc::strcmp( - (buf.as_mut_ptr()).cast_const(), - (c"ab".as_ptr().cast_mut()).cast_const() - )) == (0)) as i32) - != 0) - ); - assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0)); - assert!( - ((((libc::read( - fds[(0) as usize], - (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), - ::std::mem::size_of::<[libc::c_char; 4]>() - )) == (0_isize)) as i32) - != 0) - ); - assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0)); -} -pub unsafe fn test_socket_listen_2() { - let mut s: i32 = libc::socket(libc::AF_INET, libc::SOCK_STREAM, 0); - assert!(((((s) >= (0)) as i32) != 0)); - assert!(((((libc::listen(s, 5)) == (0)) as i32) != 0)); - assert!(((((libc::close(s)) == (0)) as i32) != 0)); -} -pub unsafe fn test_sockopt_3() { - let mut s: i32 = libc::socket(libc::AF_INET, libc::SOCK_STREAM, 0); - assert!(((((s) >= (0)) as i32) != 0)); - let mut on: i32 = 1; - assert!( - ((((libc::setsockopt( - s, - 1, - 9, - ((&mut on as *mut i32) as *const i32 as *const ::libc::c_void), - (::std::mem::size_of::() as u32) - )) == (0)) as i32) - != 0) - ); - assert!( - ((((libc::setsockopt( - s, - libc::IPPROTO_TCP, - 1, - ((&mut on as *mut i32) as *const i32 as *const ::libc::c_void), - (::std::mem::size_of::() as u32) - )) == (0)) as i32) - != 0) - ); - let mut err: i32 = -1_i32; - let mut len: u32 = (::std::mem::size_of::() as u32); - assert!( - ((((libc::getsockopt( - s, - 1, - 4, - ((&mut err as *mut i32) as *mut i32 as *mut ::libc::c_void), - (&mut len as *mut u32) - )) == (0)) as i32) - != 0) - ); - assert!(((((err) == (0)) as i32) != 0)); - assert!(((((libc::close(s)) == (0)) as i32) != 0)); -} -pub unsafe fn test_lseek_4() { - let mut path: *const libc::c_char = - (c"/tmp/cpp2rust_fd_io_lseek.tmp".as_ptr().cast_mut()).cast_const(); - let mut fd: i32 = (unsafe { - libc::open( - path as *const i8, - (((::libc::O_RDWR) | (::libc::O_CREAT)) | (::libc::O_TRUNC)) as i32, - (420), - ) - }); - assert!(((((fd) >= (0)) as i32) != 0)); - assert!( - ((((libc::write( - fd, - (c"hello world".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), - 11_usize - )) == (11_isize)) as i32) - != 0) - ); - assert!(((((libc::lseek(fd, 0_i64, 2)) == (11_i64)) as i32) != 0)); - assert!(((((libc::lseek(fd, 6_i64, 0)) == (6_i64)) as i32) != 0)); - let mut buf: [libc::c_char; 16] = [(0 as libc::c_char); 16]; - { - let byte_0 = (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) as *mut u8; - for offset in 0..::std::mem::size_of::<[libc::c_char; 16]>() { - *byte_0.offset(offset as isize) = 0 as u8; - } - (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) - }; - assert!( - ((((libc::read( - fd, - (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), - ::std::mem::size_of::<[libc::c_char; 16]>() - )) == (5_isize)) as i32) - != 0) - ); - assert!( - ((((libc::strcmp( - (buf.as_mut_ptr()).cast_const(), - (c"world".as_ptr().cast_mut()).cast_const() - )) == (0)) as i32) - != 0) - ); - assert!(((((libc::close(fd)) == (0)) as i32) != 0)); - assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); -} -pub unsafe fn test_ftruncate_5() { - let mut path: *const libc::c_char = - (c"/tmp/cpp2rust_fd_io_trunc.tmp".as_ptr().cast_mut()).cast_const(); - let mut fd: i32 = (unsafe { - libc::open( - path as *const i8, - (((::libc::O_RDWR) | (::libc::O_CREAT)) | (::libc::O_TRUNC)) as i32, - (420), - ) - }); - assert!(((((fd) >= (0)) as i32) != 0)); - assert!( - ((((libc::write( - fd, - (c"hello world".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), - 11_usize - )) == (11_isize)) as i32) - != 0) - ); - assert!(((((libc::ftruncate(fd, 5_i64)) == (0)) as i32) != 0)); - assert!(((((libc::lseek(fd, 0_i64, 2)) == (5_i64)) as i32) != 0)); - assert!(((((libc::close(fd)) == (0)) as i32) != 0)); - assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); -} -pub unsafe fn test_fstat_6() { - let mut path: *const libc::c_char = - (c"/tmp/cpp2rust_fd_io_fstat.tmp".as_ptr().cast_mut()).cast_const(); - let mut fd: i32 = (unsafe { - libc::open( - path as *const i8, - (((::libc::O_WRONLY) | (::libc::O_CREAT)) | (::libc::O_TRUNC)) as i32, - (420), - ) - }); - assert!(((((fd) >= (0)) as i32) != 0)); - assert!( - ((((libc::write( - fd, - (c"hello".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), - 5_usize - )) == (5_isize)) as i32) - != 0) - ); - let mut st: ::libc::stat = unsafe { std::mem::zeroed() }; - assert!(((((libc::fstat(fd, (&mut st as *mut ::libc::stat))) == (0)) as i32) != 0)); - assert!(((((st.st_size) == (5_i64)) as i32) != 0)); - assert!((((((st.st_mode) & (61440_u32)) == (32768_u32)) as i32) != 0)); - assert!(((((libc::close(fd)) == (0)) as i32) != 0)); - assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); -} -pub unsafe fn test_isatty_7() { - let mut path: *const libc::c_char = - (c"/tmp/cpp2rust_fd_io_tty.tmp".as_ptr().cast_mut()).cast_const(); - let mut fd: i32 = (unsafe { - libc::open( - path as *const i8, - ((::libc::O_RDONLY) | (::libc::O_CREAT)) as i32, - (420), - ) - }); - assert!(((((fd) >= (0)) as i32) != 0)); - assert!(((((libc::isatty(fd)) == (0)) as i32) != 0)); - assert!(((((libc::close(fd)) == (0)) as i32) != 0)); - assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); -} -pub unsafe fn test_tcgetattr_8() { - let mut path: *const libc::c_char = - (c"/tmp/cpp2rust_fd_io_termios.tmp".as_ptr().cast_mut()).cast_const(); - let mut fd: i32 = (unsafe { - libc::open( - path as *const i8, - ((::libc::O_RDONLY) | (::libc::O_CREAT)) as i32, - (420), - ) - }); - assert!(((((fd) >= (0)) as i32) != 0)); - let mut tio: ::libc::termios = unsafe { std::mem::zeroed() }; - assert!( - ((((libc::tcgetattr(fd, (&mut tio as *mut ::libc::termios))) == (-1_i32)) as i32) != 0) - ); - assert!(((((libc::close(fd)) == (0)) as i32) != 0)); - assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); -} -pub unsafe fn test_fcntl_9() { - let mut fds: [i32; 2] = [0_i32; 2]; - assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0)); - let mut flags: i32 = (unsafe { libc::fcntl(fds[(0) as usize] as i32, 3 as i32, (0)) }); - assert!(((((flags) >= (0)) as i32) != 0)); - assert!((((((flags) & (::libc::O_NONBLOCK)) == (0)) as i32) != 0)); - assert!( - ((((unsafe { - libc::fcntl( - fds[(0) as usize] as i32, - 4 as i32, - ((flags) | (::libc::O_NONBLOCK)), - ) - }) == (0)) as i32) - != 0) - ); - flags = (unsafe { libc::fcntl(fds[(0) as usize] as i32, 3 as i32, (0)) }); - assert!((((((flags) & (::libc::O_NONBLOCK)) != (0)) as i32) != 0)); - let mut b: libc::c_char = (0 as libc::c_char); - assert!( - ((((libc::read( - fds[(0) as usize], - ((&mut b as *mut libc::c_char) as *mut libc::c_char as *mut ::libc::c_void), - 1_usize - )) == (-1_i32 as isize)) as i32) - != 0) - ); - assert!( - ((((unsafe { libc::fcntl(fds[(0) as usize] as i32, 2 as i32, (1),) }) == (0)) as i32) != 0) - ); - assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0)); - assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0)); -} -pub unsafe fn test_select_10() { - let mut fds: [i32; 2] = [0_i32; 2]; - assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0)); - let mut rset: ::libc::fd_set = std::mem::zeroed::<::libc::fd_set>(); - libc::FD_ZERO((&mut rset as *mut ::libc::fd_set)); - libc::FD_SET(fds[(0) as usize], (&mut rset as *mut ::libc::fd_set)); - let mut tv: ::libc::timeval = unsafe { std::mem::zeroed() }; - tv.tv_sec = 0_i64; - tv.tv_usec = 0_i64; - assert!( - ((((libc::select( - ((fds[(0) as usize]) + (1)), - (&mut rset as *mut ::libc::fd_set), - std::ptr::null_mut(), - std::ptr::null_mut(), - (&mut tv as *mut ::libc::timeval) - )) == (0)) as i32) - != 0) - ); - assert!( - ((!(libc::FD_ISSET( - fds[(0) as usize], - (&mut rset as *mut ::libc::fd_set).cast_const() - ) as i32 - != 0) as i32) - != 0) - ); - assert!( - ((((libc::write( - fds[(1) as usize], - (c"x".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), - 1_usize - )) == (1_isize)) as i32) - != 0) - ); - libc::FD_ZERO((&mut rset as *mut ::libc::fd_set)); - libc::FD_SET(fds[(0) as usize], (&mut rset as *mut ::libc::fd_set)); - tv.tv_sec = 1_i64; - tv.tv_usec = 0_i64; - assert!( - ((((libc::select( - ((fds[(0) as usize]) + (1)), - (&mut rset as *mut ::libc::fd_set), - std::ptr::null_mut(), - std::ptr::null_mut(), - (&mut tv as *mut ::libc::timeval) - )) == (1)) as i32) - != 0) - ); - assert!( - (libc::FD_ISSET( - fds[(0) as usize], - (&mut rset as *mut ::libc::fd_set).cast_const() - ) as i32 - != 0) - ); - assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0)); - assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0)); -} -pub unsafe fn test_poll_11() { - let mut fds: [i32; 2] = [0_i32; 2]; - assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0)); - assert!( - ((((libc::write( - fds[(1) as usize], - (c"x".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), - 1_usize - )) == (1_isize)) as i32) - != 0) - ); - let mut pfd: [::libc::pollfd; 2] = [unsafe { std::mem::zeroed() }; 2]; - pfd[(0) as usize].fd = fds[(0) as usize]; - pfd[(0) as usize].events = 1_i16; - pfd[(0) as usize].revents = 0_i16; - pfd[(1) as usize].fd = -1_i32; - pfd[(1) as usize].events = 1_i16; - pfd[(1) as usize].revents = 42_i16; - assert!(((((libc::poll(pfd.as_mut_ptr(), 2_u64 as ::libc::nfds_t, 0)) == (1)) as i32) != 0)); - assert!((((((pfd[(0) as usize].revents as i32) & (1)) != (0)) as i32) != 0)); - assert!(((((pfd[(1) as usize].revents as i32) == (0)) as i32) != 0)); - let mut ch: libc::c_char = (0 as libc::c_char); - assert!( - ((((libc::read( - fds[(0) as usize], - ((&mut ch as *mut libc::c_char) as *mut libc::c_char as *mut ::libc::c_void), - 1_usize - )) == (1_isize)) as i32) - != 0) - ); - assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0)); - assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0)); -} -pub fn main() { - unsafe { - std::process::exit(main_0() as i32); - } -} -unsafe fn main_0() -> i32 { - (unsafe { test_open_read_write_0() }); - (unsafe { test_pipe_1() }); - (unsafe { test_socket_listen_2() }); - (unsafe { test_sockopt_3() }); - (unsafe { test_lseek_4() }); - (unsafe { test_ftruncate_5() }); - (unsafe { test_fstat_6() }); - (unsafe { test_isatty_7() }); - (unsafe { test_tcgetattr_8() }); - (unsafe { test_fcntl_9() }); - (unsafe { test_select_10() }); - (unsafe { test_poll_11() }); return 0; } From 53819f78448fbe12a649b07bf51627ac7573988c Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 23 Jul 2026 15:45:46 +0100 Subject: [PATCH 3/9] Add sockopt test --- tests/unit/out/refcount/sockopt.rs | 418 +++++++++++++++++++++++++++++ tests/unit/out/unsafe/sockopt.rs | 53 ++++ tests/unit/sockopt.c | 19 ++ 3 files changed, 490 insertions(+) create mode 100644 tests/unit/out/refcount/sockopt.rs create mode 100644 tests/unit/out/unsafe/sockopt.rs create mode 100644 tests/unit/sockopt.c diff --git a/tests/unit/out/refcount/sockopt.rs b/tests/unit/out/refcount/sockopt.rs new file mode 100644 index 00000000..71d96af8 --- /dev/null +++ b/tests/unit/out/refcount/sockopt.rs @@ -0,0 +1,418 @@ +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 s: Value = Rc::new(RefCell::new({ + let __family = match libc::AF_INET { + ::libc::AF_INET => nix::sys::socket::AddressFamily::Inet, + ::libc::AF_INET6 => nix::sys::socket::AddressFamily::Inet6, + ::libc::AF_UNIX => nix::sys::socket::AddressFamily::Unix, + __d => panic!("socket: unsupported domain {__d}"), + }; + let __flags = nix::sys::socket::SockFlag::from_bits_truncate(libc::SOCK_STREAM); + let __ty = match libc::SOCK_STREAM & !nix::sys::socket::SockFlag::all().bits() { + ::libc::SOCK_STREAM => nix::sys::socket::SockType::Stream, + ::libc::SOCK_DGRAM => nix::sys::socket::SockType::Datagram, + __t => panic!("socket: unsupported type {__t}"), + }; + let __proto = match 0 { + 0 => None, + ::libc::IPPROTO_TCP => Some(nix::sys::socket::SockProtocol::Tcp), + ::libc::IPPROTO_UDP => Some(nix::sys::socket::SockProtocol::Udp), + __p => panic!("socket: unsupported protocol {__p}"), + }; + match nix::sys::socket::socket(__family, __ty, __flags, __proto) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*s.borrow()) >= 0) as i32) != 0)); + let on: Value = Rc::new(RefCell::new(1)); + assert!( + ((({ + let __res = match (1, 9) { + (::libc::IPPROTO_TCP, ::libc::TCP_NODELAY) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read() + != 0; + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::TcpNoDelay, + &__v, + ) + }) + } + (::libc::SOL_SOCKET, ::libc::SO_KEEPALIVE) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read() + != 0; + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::KeepAlive, + &__v, + ) + }) + } + (::libc::IPPROTO_TCP, ::libc::TCP_KEEPINTVL) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::TcpKeepInterval, + &__v, + ) + }) + } + (::libc::IPPROTO_TCP, ::libc::TCP_KEEPCNT) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::TcpKeepCount, + &__v, + ) + }) + } + (::libc::IPPROTO_IP, ::libc::IP_TOS) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::Ipv4Tos, + &__v, + ) + }) + } + (::libc::IPPROTO_IPV6, ::libc::IPV6_TCLASS) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::Ipv6TClass, + &__v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::IPPROTO_TCP, ::libc::TCP_KEEPIDLE) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::TcpKeepIdle, + &__v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::SOL_SOCKET, ::libc::SO_BINDTODEVICE) => { + let __v = ::std::ffi::OsString::from( + ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .to_rust_string(), + ); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::BindToDevice, + &__v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::IPPROTO_IP, ::libc::IP_BIND_ADDRESS_NO_PORT) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read() + != 0; + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::IpBindAddressNoPort, + &__v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::IPPROTO_TCP, ::libc::TCP_FASTOPEN_CONNECT) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read() + != 0; + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::TcpFastOpenConnect, + &__v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::SOL_SOCKET, ::libc::SO_PRIORITY) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::Priority, + &__v, + ) + }) + } + (__l, __o) => panic!( + "setsockopt: unsupported option (level={}, optname={})", + __l, __o + ), + }; + match __res { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == 0) as i32) + != 0) + ); + assert!( + ((({ + let __res = match (libc::IPPROTO_TCP, 1) { + (::libc::IPPROTO_TCP, ::libc::TCP_NODELAY) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read() + != 0; + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::TcpNoDelay, + &__v, + ) + }) + } + (::libc::SOL_SOCKET, ::libc::SO_KEEPALIVE) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read() + != 0; + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::KeepAlive, + &__v, + ) + }) + } + (::libc::IPPROTO_TCP, ::libc::TCP_KEEPINTVL) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::TcpKeepInterval, + &__v, + ) + }) + } + (::libc::IPPROTO_TCP, ::libc::TCP_KEEPCNT) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::TcpKeepCount, + &__v, + ) + }) + } + (::libc::IPPROTO_IP, ::libc::IP_TOS) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::Ipv4Tos, + &__v, + ) + }) + } + (::libc::IPPROTO_IPV6, ::libc::IPV6_TCLASS) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::Ipv6TClass, + &__v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::IPPROTO_TCP, ::libc::TCP_KEEPIDLE) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::TcpKeepIdle, + &__v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::SOL_SOCKET, ::libc::SO_BINDTODEVICE) => { + let __v = ::std::ffi::OsString::from( + ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .to_rust_string(), + ); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::BindToDevice, + &__v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::IPPROTO_IP, ::libc::IP_BIND_ADDRESS_NO_PORT) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read() + != 0; + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::IpBindAddressNoPort, + &__v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::IPPROTO_TCP, ::libc::TCP_FASTOPEN_CONNECT) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read() + != 0; + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::TcpFastOpenConnect, + &__v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::SOL_SOCKET, ::libc::SO_PRIORITY) => { + let __v = ((on.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .read(); + FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::setsockopt( + &__fd, + nix::sys::socket::sockopt::Priority, + &__v, + ) + }) + } + (__l, __o) => panic!( + "setsockopt: unsupported option (level={}, optname={})", + __l, __o + ), + }; + match __res { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == 0) as i32) + != 0) + ); + let err: Value = Rc::new(RefCell::new(-1_i32)); + let len: Value = Rc::new(RefCell::new((::std::mem::size_of::() as u32))); + assert!( + (((match (1, 4) { + (::libc::SOL_SOCKET, ::libc::SO_ERROR) => { + match FdRegistry::with_fd((*s.borrow()), |__fd| { + nix::sys::socket::getsockopt(&__fd, nix::sys::socket::sockopt::SocketError) + }) { + Ok(__err) => { + ((err.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .write(__err); + (len.as_pointer()).write(::std::mem::size_of::() as u32); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } + (__l, __o) => panic!( + "getsockopt: unsupported option (level={}, optname={})", + __l, __o + ), + } == 0) as i32) + != 0) + ); + assert!(((((*err.borrow()) == 0) as i32) != 0)); + assert!((((FdRegistry::close((*s.borrow())) == 0) as i32) != 0)); + return 0; +} diff --git a/tests/unit/out/unsafe/sockopt.rs b/tests/unit/out/unsafe/sockopt.rs new file mode 100644 index 00000000..7f0e1533 --- /dev/null +++ b/tests/unit/out/unsafe/sockopt.rs @@ -0,0 +1,53 @@ +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 s: i32 = libc::socket(libc::AF_INET, libc::SOCK_STREAM, 0); + assert!(((((s) >= (0)) as i32) != 0)); + let mut on: i32 = 1; + assert!( + ((((libc::setsockopt( + s, + 1, + 9, + ((&mut on as *mut i32) as *const i32 as *const ::libc::c_void), + (::std::mem::size_of::() as u32) + )) == (0)) as i32) + != 0) + ); + assert!( + ((((libc::setsockopt( + s, + libc::IPPROTO_TCP, + 1, + ((&mut on as *mut i32) as *const i32 as *const ::libc::c_void), + (::std::mem::size_of::() as u32) + )) == (0)) as i32) + != 0) + ); + let mut err: i32 = -1_i32; + let mut len: u32 = (::std::mem::size_of::() as u32); + assert!( + ((((libc::getsockopt( + s, + 1, + 4, + ((&mut err as *mut i32) as *mut i32 as *mut ::libc::c_void), + (&mut len as *mut u32) + )) == (0)) as i32) + != 0) + ); + assert!(((((err) == (0)) as i32) != 0)); + assert!(((((libc::close(s)) == (0)) as i32) != 0)); + return 0; +} diff --git a/tests/unit/sockopt.c b/tests/unit/sockopt.c new file mode 100644 index 00000000..b5d31d2d --- /dev/null +++ b/tests/unit/sockopt.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include +#include + +int main(void) { + int s = socket(AF_INET, SOCK_STREAM, 0); + assert(s >= 0); + int on = 1; + assert(setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) == 0); + assert(setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) == 0); + int err = -1; + socklen_t len = sizeof(err); + assert(getsockopt(s, SOL_SOCKET, SO_ERROR, &err, &len) == 0); + assert(err == 0); + assert(close(s) == 0); + return 0; +} From b5af7b462b63724ab1efd91fb5bf0ab67232ba54 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 23 Jul 2026 15:47:43 +0100 Subject: [PATCH 4/9] Add TCP constants rules --- rules/ip/src.cpp | 5 +++++ rules/ip/tgt_unsafe.rs | 4 ++++ rules/socket/src.c | 12 ++++++++++++ rules/socket/tgt_unsafe.rs | 12 ++++++++++++ tests/unit/out/refcount/sockopt.rs | 6 +++--- tests/unit/out/unsafe/sockopt.rs | 10 +++++----- 6 files changed, 41 insertions(+), 8 deletions(-) diff --git a/rules/ip/src.cpp b/rules/ip/src.cpp index ca5b4a4b..3b44b4e3 100644 --- a/rules/ip/src.cpp +++ b/rules/ip/src.cpp @@ -1,4 +1,5 @@ #include +#include typedef struct sockaddr_in t1; typedef struct in_addr t2; @@ -26,3 +27,7 @@ int f5() { return IPPROTO_MPTCP; } #endif + +int f6() { + return TCP_NODELAY; +} diff --git a/rules/ip/tgt_unsafe.rs b/rules/ip/tgt_unsafe.rs index 7dd3988a..caba50fa 100644 --- a/rules/ip/tgt_unsafe.rs +++ b/rules/ip/tgt_unsafe.rs @@ -34,3 +34,7 @@ unsafe fn f4() -> i32 { unsafe fn f5() -> i32 { libc::IPPROTO_MPTCP } + +unsafe fn f6() -> i32 { + libc::TCP_NODELAY +} diff --git a/rules/socket/src.c b/rules/socket/src.c index 95f63fd1..19c9ca8d 100644 --- a/rules/socket/src.c +++ b/rules/socket/src.c @@ -96,3 +96,15 @@ int f20() { int f21() { return AF_INET6; } + +int f22() { + return SOL_SOCKET; +} + +int f23() { + return SO_KEEPALIVE; +} + +int f24() { + return SO_ERROR; +} diff --git a/rules/socket/tgt_unsafe.rs b/rules/socket/tgt_unsafe.rs index 1af2d775..402a1782 100644 --- a/rules/socket/tgt_unsafe.rs +++ b/rules/socket/tgt_unsafe.rs @@ -110,3 +110,15 @@ unsafe fn f20() -> i32 { unsafe fn f21() -> i32 { libc::AF_INET6 } + +unsafe fn f22() -> i32 { + libc::SOL_SOCKET +} + +unsafe fn f23() -> i32 { + libc::SO_KEEPALIVE +} + +unsafe fn f24() -> i32 { + libc::SO_ERROR +} diff --git a/tests/unit/out/refcount/sockopt.rs b/tests/unit/out/refcount/sockopt.rs index 71d96af8..fb1bd8fe 100644 --- a/tests/unit/out/refcount/sockopt.rs +++ b/tests/unit/out/refcount/sockopt.rs @@ -41,7 +41,7 @@ fn main_0() -> i32 { let on: Value = Rc::new(RefCell::new(1)); assert!( ((({ - let __res = match (1, 9) { + let __res = match (libc::SOL_SOCKET, libc::SO_KEEPALIVE) { (::libc::IPPROTO_TCP, ::libc::TCP_NODELAY) => { let __v = ((on.as_pointer()) as Ptr) .to_any() @@ -213,7 +213,7 @@ fn main_0() -> i32 { ); assert!( ((({ - let __res = match (libc::IPPROTO_TCP, 1) { + let __res = match (libc::IPPROTO_TCP, libc::TCP_NODELAY) { (::libc::IPPROTO_TCP, ::libc::TCP_NODELAY) => { let __v = ((on.as_pointer()) as Ptr) .to_any() @@ -386,7 +386,7 @@ fn main_0() -> i32 { let err: Value = Rc::new(RefCell::new(-1_i32)); let len: Value = Rc::new(RefCell::new((::std::mem::size_of::() as u32))); assert!( - (((match (1, 4) { + (((match (libc::SOL_SOCKET, libc::SO_ERROR) { (::libc::SOL_SOCKET, ::libc::SO_ERROR) => { match FdRegistry::with_fd((*s.borrow()), |__fd| { nix::sys::socket::getsockopt(&__fd, nix::sys::socket::sockopt::SocketError) diff --git a/tests/unit/out/unsafe/sockopt.rs b/tests/unit/out/unsafe/sockopt.rs index 7f0e1533..354dbf5f 100644 --- a/tests/unit/out/unsafe/sockopt.rs +++ b/tests/unit/out/unsafe/sockopt.rs @@ -18,8 +18,8 @@ unsafe fn main_0() -> i32 { assert!( ((((libc::setsockopt( s, - 1, - 9, + libc::SOL_SOCKET, + libc::SO_KEEPALIVE, ((&mut on as *mut i32) as *const i32 as *const ::libc::c_void), (::std::mem::size_of::() as u32) )) == (0)) as i32) @@ -29,7 +29,7 @@ unsafe fn main_0() -> i32 { ((((libc::setsockopt( s, libc::IPPROTO_TCP, - 1, + libc::TCP_NODELAY, ((&mut on as *mut i32) as *const i32 as *const ::libc::c_void), (::std::mem::size_of::() as u32) )) == (0)) as i32) @@ -40,8 +40,8 @@ unsafe fn main_0() -> i32 { assert!( ((((libc::getsockopt( s, - 1, - 4, + libc::SOL_SOCKET, + libc::SO_ERROR, ((&mut err as *mut i32) as *mut i32 as *mut ::libc::c_void), (&mut len as *mut u32) )) == (0)) as i32) From 7446004c992eb287fd0fb576f640427470dd6265 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 23 Jul 2026 15:58:24 +0100 Subject: [PATCH 5/9] Guard IP_TOS and IPV6_TCLASS only for linux --- rules/socket/tgt_refcount.rs | 2 ++ tests/unit/out/refcount/sockopt.rs | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/rules/socket/tgt_refcount.rs b/rules/socket/tgt_refcount.rs index 185b1125..0de3bf80 100644 --- a/rules/socket/tgt_refcount.rs +++ b/rules/socket/tgt_refcount.rs @@ -249,12 +249,14 @@ fn f7(a0: i32, a1: i32, a2: i32, a3: AnyPtr, a4: u32) -> i32 { nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::TcpKeepCount, &__v) }) } + #[cfg(target_os = "linux")] (::libc::IPPROTO_IP, ::libc::IP_TOS) => { let __v = a3.reinterpret_cast::().read(); FdRegistry::with_fd(a0, |__fd| { nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::Ipv4Tos, &__v) }) } + #[cfg(target_os = "linux")] (::libc::IPPROTO_IPV6, ::libc::IPV6_TCLASS) => { let __v = a3.reinterpret_cast::().read(); FdRegistry::with_fd(a0, |__fd| { diff --git a/tests/unit/out/refcount/sockopt.rs b/tests/unit/out/refcount/sockopt.rs index fb1bd8fe..0861d859 100644 --- a/tests/unit/out/refcount/sockopt.rs +++ b/tests/unit/out/refcount/sockopt.rs @@ -96,6 +96,7 @@ fn main_0() -> i32 { ) }) } + #[cfg(target_os = "linux")] (::libc::IPPROTO_IP, ::libc::IP_TOS) => { let __v = ((on.as_pointer()) as Ptr) .to_any() @@ -109,6 +110,7 @@ fn main_0() -> i32 { ) }) } + #[cfg(target_os = "linux")] (::libc::IPPROTO_IPV6, ::libc::IPV6_TCLASS) => { let __v = ((on.as_pointer()) as Ptr) .to_any() @@ -268,6 +270,7 @@ fn main_0() -> i32 { ) }) } + #[cfg(target_os = "linux")] (::libc::IPPROTO_IP, ::libc::IP_TOS) => { let __v = ((on.as_pointer()) as Ptr) .to_any() @@ -281,6 +284,7 @@ fn main_0() -> i32 { ) }) } + #[cfg(target_os = "linux")] (::libc::IPPROTO_IPV6, ::libc::IPV6_TCLASS) => { let __v = ((on.as_pointer()) as Ptr) .to_any() From 4bf124ac0cdd8a09ca613199696716a249b2fc37 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 23 Jul 2026 22:16:38 +0100 Subject: [PATCH 6/9] Don't analyze platform specific blocks in syntactic phase --- rule-preprocessor/src/syntactic.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/rule-preprocessor/src/syntactic.rs b/rule-preprocessor/src/syntactic.rs index 266613a0..70932b77 100644 --- a/rule-preprocessor/src/syntactic.rs +++ b/rule-preprocessor/src/syntactic.rs @@ -54,8 +54,8 @@ fn is_va_args_type(ty: &ast::Type) -> bool { false } -fn cfg_matches_host(fn_item: &ast::Fn) -> bool { - for attr in fn_item.attrs() { +fn cfg_matches_host(attrs: impl Iterator) -> bool { + for attr in attrs { let Some(meta) = attr.meta() else { continue }; let Some(path) = meta.path() else { continue }; if path.syntax().text() != "cfg" { @@ -135,7 +135,7 @@ impl SyntacticAnalysis { let mut file_ir = FileIr::new(); for fn_item in source_file.syntax().descendants().filter_map(ast::Fn::cast) { - if !cfg_matches_host(&fn_item) { + if !cfg_matches_host(fn_item.attrs()) { continue; } @@ -202,6 +202,16 @@ impl<'a> FragmentCtx<'a> { match child { ra_ap_syntax::NodeOrToken::Token(token) => self.visit_token(&token), ra_ap_syntax::NodeOrToken::Node(node) => { + if ast::Attr::can_cast(node.kind()) { + self.text_buf.push_str(&node.text().to_string()); + return; + } + if let Some(has_attrs) = ast::AnyHasAttrs::cast(node.clone()) + && !cfg_matches_host(has_attrs.attrs()) + { + self.text_buf.push_str(&node.text().to_string()); + return; + } if let Some(call) = ast::MethodCallExpr::cast(node.clone()) { self.emit_method_call(&call); return; From 6fea27cbf73dab47a7d215634896ace58ca5f448 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 23 Jul 2026 22:47:41 +0100 Subject: [PATCH 7/9] Revert "Don't analyze platform specific blocks in syntactic phase" This reverts commit ad1837dbd627da2aa3039ba2411150f4749b1b3e. --- rule-preprocessor/src/syntactic.rs | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/rule-preprocessor/src/syntactic.rs b/rule-preprocessor/src/syntactic.rs index 70932b77..266613a0 100644 --- a/rule-preprocessor/src/syntactic.rs +++ b/rule-preprocessor/src/syntactic.rs @@ -54,8 +54,8 @@ fn is_va_args_type(ty: &ast::Type) -> bool { false } -fn cfg_matches_host(attrs: impl Iterator) -> bool { - for attr in attrs { +fn cfg_matches_host(fn_item: &ast::Fn) -> bool { + for attr in fn_item.attrs() { let Some(meta) = attr.meta() else { continue }; let Some(path) = meta.path() else { continue }; if path.syntax().text() != "cfg" { @@ -135,7 +135,7 @@ impl SyntacticAnalysis { let mut file_ir = FileIr::new(); for fn_item in source_file.syntax().descendants().filter_map(ast::Fn::cast) { - if !cfg_matches_host(fn_item.attrs()) { + if !cfg_matches_host(&fn_item) { continue; } @@ -202,16 +202,6 @@ impl<'a> FragmentCtx<'a> { match child { ra_ap_syntax::NodeOrToken::Token(token) => self.visit_token(&token), ra_ap_syntax::NodeOrToken::Node(node) => { - if ast::Attr::can_cast(node.kind()) { - self.text_buf.push_str(&node.text().to_string()); - return; - } - if let Some(has_attrs) = ast::AnyHasAttrs::cast(node.clone()) - && !cfg_matches_host(has_attrs.attrs()) - { - self.text_buf.push_str(&node.text().to_string()); - return; - } if let Some(call) = ast::MethodCallExpr::cast(node.clone()) { self.emit_method_call(&call); return; From dbf61d3e441604fe4f7465ad5860e5376b4d214d Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 23 Jul 2026 22:52:57 +0100 Subject: [PATCH 8/9] Move setsockopt in libc_shims --- libcc2rs/src/libc_shims/socket.rs | 101 +++++++++ rules/socket/tgt_refcount.rs | 103 +-------- tests/unit/out/refcount/sockopt.rs | 348 +---------------------------- 3 files changed, 116 insertions(+), 436 deletions(-) diff --git a/libcc2rs/src/libc_shims/socket.rs b/libcc2rs/src/libc_shims/socket.rs index 3b15500a..c2391558 100644 --- a/libcc2rs/src/libc_shims/socket.rs +++ b/libcc2rs/src/libc_shims/socket.rs @@ -378,3 +378,104 @@ impl Sockaddr { out_len.write(ss.len()); } } + +pub fn setsockopt_refcount(fd: i32, level: i32, optname: i32, optval: crate::AnyPtr) -> i32 { + let res = match (level, optname) { + (::libc::IPPROTO_TCP, ::libc::TCP_NODELAY) => { + let v = optval.reinterpret_cast::().read() != 0; + crate::FdRegistry::with_fd(fd, |borrowed| { + nix::sys::socket::setsockopt(&borrowed, nix::sys::socket::sockopt::TcpNoDelay, &v) + }) + } + (::libc::SOL_SOCKET, ::libc::SO_KEEPALIVE) => { + let v = optval.reinterpret_cast::().read() != 0; + crate::FdRegistry::with_fd(fd, |borrowed| { + nix::sys::socket::setsockopt(&borrowed, nix::sys::socket::sockopt::KeepAlive, &v) + }) + } + (::libc::IPPROTO_TCP, ::libc::TCP_KEEPINTVL) => { + let v = optval.reinterpret_cast::().read(); + crate::FdRegistry::with_fd(fd, |borrowed| { + nix::sys::socket::setsockopt( + &borrowed, + nix::sys::socket::sockopt::TcpKeepInterval, + &v, + ) + }) + } + (::libc::IPPROTO_TCP, ::libc::TCP_KEEPCNT) => { + let v = optval.reinterpret_cast::().read(); + crate::FdRegistry::with_fd(fd, |borrowed| { + nix::sys::socket::setsockopt(&borrowed, nix::sys::socket::sockopt::TcpKeepCount, &v) + }) + } + #[cfg(target_os = "linux")] + (::libc::IPPROTO_IP, ::libc::IP_TOS) => { + let v = optval.reinterpret_cast::().read(); + crate::FdRegistry::with_fd(fd, |borrowed| { + nix::sys::socket::setsockopt(&borrowed, nix::sys::socket::sockopt::Ipv4Tos, &v) + }) + } + #[cfg(target_os = "linux")] + (::libc::IPPROTO_IPV6, ::libc::IPV6_TCLASS) => { + let v = optval.reinterpret_cast::().read(); + crate::FdRegistry::with_fd(fd, |borrowed| { + nix::sys::socket::setsockopt(&borrowed, nix::sys::socket::sockopt::Ipv6TClass, &v) + }) + } + #[cfg(target_os = "linux")] + (::libc::IPPROTO_TCP, ::libc::TCP_KEEPIDLE) => { + let v = optval.reinterpret_cast::().read(); + crate::FdRegistry::with_fd(fd, |borrowed| { + nix::sys::socket::setsockopt(&borrowed, nix::sys::socket::sockopt::TcpKeepIdle, &v) + }) + } + #[cfg(target_os = "linux")] + (::libc::SOL_SOCKET, ::libc::SO_BINDTODEVICE) => { + let v = ::std::ffi::OsString::from(optval.reinterpret_cast::().to_rust_string()); + crate::FdRegistry::with_fd(fd, |borrowed| { + nix::sys::socket::setsockopt(&borrowed, nix::sys::socket::sockopt::BindToDevice, &v) + }) + } + #[cfg(target_os = "linux")] + (::libc::IPPROTO_IP, ::libc::IP_BIND_ADDRESS_NO_PORT) => { + let v = optval.reinterpret_cast::().read() != 0; + crate::FdRegistry::with_fd(fd, |borrowed| { + nix::sys::socket::setsockopt( + &borrowed, + nix::sys::socket::sockopt::IpBindAddressNoPort, + &v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::IPPROTO_TCP, ::libc::TCP_FASTOPEN_CONNECT) => { + let v = optval.reinterpret_cast::().read() != 0; + crate::FdRegistry::with_fd(fd, |borrowed| { + nix::sys::socket::setsockopt( + &borrowed, + nix::sys::socket::sockopt::TcpFastOpenConnect, + &v, + ) + }) + } + #[cfg(target_os = "linux")] + (::libc::SOL_SOCKET, ::libc::SO_PRIORITY) => { + let v = optval.reinterpret_cast::().read(); + crate::FdRegistry::with_fd(fd, |borrowed| { + nix::sys::socket::setsockopt(&borrowed, nix::sys::socket::sockopt::Priority, &v) + }) + } + (l, o) => panic!( + "setsockopt: unsupported option (level={}, optname={})", + l, o + ), + }; + match res { + Ok(()) => 0, + Err(e) => { + crate::cpp2rust_errno().write(e as i32); + -1 + } + } +} diff --git a/rules/socket/tgt_refcount.rs b/rules/socket/tgt_refcount.rs index 0de3bf80..b1d1722c 100644 --- a/rules/socket/tgt_refcount.rs +++ b/rules/socket/tgt_refcount.rs @@ -220,104 +220,11 @@ fn f17(a0: i32, a1: i32) -> i32 { } fn f7(a0: i32, a1: i32, a2: i32, a3: AnyPtr, a4: u32) -> i32 { - let __res = match (a1, a2) { - (::libc::IPPROTO_TCP, ::libc::TCP_NODELAY) => { - let __v = a3.reinterpret_cast::().read() != 0; - FdRegistry::with_fd(a0, |__fd| { - nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::TcpNoDelay, &__v) - }) - } - (::libc::SOL_SOCKET, ::libc::SO_KEEPALIVE) => { - let __v = a3.reinterpret_cast::().read() != 0; - FdRegistry::with_fd(a0, |__fd| { - nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::KeepAlive, &__v) - }) - } - (::libc::IPPROTO_TCP, ::libc::TCP_KEEPINTVL) => { - let __v = a3.reinterpret_cast::().read(); - FdRegistry::with_fd(a0, |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::TcpKeepInterval, - &__v, - ) - }) - } - (::libc::IPPROTO_TCP, ::libc::TCP_KEEPCNT) => { - let __v = a3.reinterpret_cast::().read(); - FdRegistry::with_fd(a0, |__fd| { - nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::TcpKeepCount, &__v) - }) - } - #[cfg(target_os = "linux")] - (::libc::IPPROTO_IP, ::libc::IP_TOS) => { - let __v = a3.reinterpret_cast::().read(); - FdRegistry::with_fd(a0, |__fd| { - nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::Ipv4Tos, &__v) - }) - } - #[cfg(target_os = "linux")] - (::libc::IPPROTO_IPV6, ::libc::IPV6_TCLASS) => { - let __v = a3.reinterpret_cast::().read(); - FdRegistry::with_fd(a0, |__fd| { - nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::Ipv6TClass, &__v) - }) - } - #[cfg(target_os = "linux")] - (::libc::IPPROTO_TCP, ::libc::TCP_KEEPIDLE) => { - let __v = a3.reinterpret_cast::().read(); - FdRegistry::with_fd(a0, |__fd| { - nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::TcpKeepIdle, &__v) - }) - } - #[cfg(target_os = "linux")] - (::libc::SOL_SOCKET, ::libc::SO_BINDTODEVICE) => { - let __v = ::std::ffi::OsString::from(a3.reinterpret_cast::().to_rust_string()); - FdRegistry::with_fd(a0, |__fd| { - nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::BindToDevice, &__v) - }) - } - #[cfg(target_os = "linux")] - (::libc::IPPROTO_IP, ::libc::IP_BIND_ADDRESS_NO_PORT) => { - let __v = a3.reinterpret_cast::().read() != 0; - FdRegistry::with_fd(a0, |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::IpBindAddressNoPort, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::IPPROTO_TCP, ::libc::TCP_FASTOPEN_CONNECT) => { - let __v = a3.reinterpret_cast::().read() != 0; - FdRegistry::with_fd(a0, |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::TcpFastOpenConnect, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::SOL_SOCKET, ::libc::SO_PRIORITY) => { - let __v = a3.reinterpret_cast::().read(); - FdRegistry::with_fd(a0, |__fd| { - nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::Priority, &__v) - }) - } - (__l, __o) => panic!( - "setsockopt: unsupported option (level={}, optname={})", - __l, __o - ), - }; - match __res { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } + let __a0 = a0; + let __a1 = a1; + let __a2 = a2; + let __a3 = a3; + libcc2rs::setsockopt_refcount(__a0, __a1, __a2, __a3) } fn f8(a0: i32, a1: i32, a2: i32, a3: AnyPtr, a4: Ptr) -> i32 { diff --git a/tests/unit/out/refcount/sockopt.rs b/tests/unit/out/refcount/sockopt.rs index 0861d859..1afb88fa 100644 --- a/tests/unit/out/refcount/sockopt.rs +++ b/tests/unit/out/refcount/sockopt.rs @@ -41,349 +41,21 @@ fn main_0() -> i32 { let on: Value = Rc::new(RefCell::new(1)); assert!( ((({ - let __res = match (libc::SOL_SOCKET, libc::SO_KEEPALIVE) { - (::libc::IPPROTO_TCP, ::libc::TCP_NODELAY) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read() - != 0; - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::TcpNoDelay, - &__v, - ) - }) - } - (::libc::SOL_SOCKET, ::libc::SO_KEEPALIVE) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read() - != 0; - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::KeepAlive, - &__v, - ) - }) - } - (::libc::IPPROTO_TCP, ::libc::TCP_KEEPINTVL) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::TcpKeepInterval, - &__v, - ) - }) - } - (::libc::IPPROTO_TCP, ::libc::TCP_KEEPCNT) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::TcpKeepCount, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::IPPROTO_IP, ::libc::IP_TOS) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::Ipv4Tos, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::IPPROTO_IPV6, ::libc::IPV6_TCLASS) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::Ipv6TClass, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::IPPROTO_TCP, ::libc::TCP_KEEPIDLE) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::TcpKeepIdle, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::SOL_SOCKET, ::libc::SO_BINDTODEVICE) => { - let __v = ::std::ffi::OsString::from( - ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .to_rust_string(), - ); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::BindToDevice, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::IPPROTO_IP, ::libc::IP_BIND_ADDRESS_NO_PORT) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read() - != 0; - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::IpBindAddressNoPort, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::IPPROTO_TCP, ::libc::TCP_FASTOPEN_CONNECT) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read() - != 0; - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::TcpFastOpenConnect, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::SOL_SOCKET, ::libc::SO_PRIORITY) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::Priority, - &__v, - ) - }) - } - (__l, __o) => panic!( - "setsockopt: unsupported option (level={}, optname={})", - __l, __o - ), - }; - match __res { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } + let __a0 = (*s.borrow()); + let __a1 = libc::SOL_SOCKET; + let __a2 = libc::SO_KEEPALIVE; + let __a3 = ((on.as_pointer()) as Ptr).to_any(); + libcc2rs::setsockopt_refcount(__a0, __a1, __a2, __a3) } == 0) as i32) != 0) ); assert!( ((({ - let __res = match (libc::IPPROTO_TCP, libc::TCP_NODELAY) { - (::libc::IPPROTO_TCP, ::libc::TCP_NODELAY) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read() - != 0; - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::TcpNoDelay, - &__v, - ) - }) - } - (::libc::SOL_SOCKET, ::libc::SO_KEEPALIVE) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read() - != 0; - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::KeepAlive, - &__v, - ) - }) - } - (::libc::IPPROTO_TCP, ::libc::TCP_KEEPINTVL) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::TcpKeepInterval, - &__v, - ) - }) - } - (::libc::IPPROTO_TCP, ::libc::TCP_KEEPCNT) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::TcpKeepCount, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::IPPROTO_IP, ::libc::IP_TOS) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::Ipv4Tos, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::IPPROTO_IPV6, ::libc::IPV6_TCLASS) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::Ipv6TClass, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::IPPROTO_TCP, ::libc::TCP_KEEPIDLE) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::TcpKeepIdle, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::SOL_SOCKET, ::libc::SO_BINDTODEVICE) => { - let __v = ::std::ffi::OsString::from( - ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .to_rust_string(), - ); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::BindToDevice, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::IPPROTO_IP, ::libc::IP_BIND_ADDRESS_NO_PORT) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read() - != 0; - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::IpBindAddressNoPort, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::IPPROTO_TCP, ::libc::TCP_FASTOPEN_CONNECT) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read() - != 0; - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::TcpFastOpenConnect, - &__v, - ) - }) - } - #[cfg(target_os = "linux")] - (::libc::SOL_SOCKET, ::libc::SO_PRIORITY) => { - let __v = ((on.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .read(); - FdRegistry::with_fd((*s.borrow()), |__fd| { - nix::sys::socket::setsockopt( - &__fd, - nix::sys::socket::sockopt::Priority, - &__v, - ) - }) - } - (__l, __o) => panic!( - "setsockopt: unsupported option (level={}, optname={})", - __l, __o - ), - }; - match __res { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } + let __a0 = (*s.borrow()); + let __a1 = libc::IPPROTO_TCP; + let __a2 = libc::TCP_NODELAY; + let __a3 = ((on.as_pointer()) as Ptr).to_any(); + libcc2rs::setsockopt_refcount(__a0, __a1, __a2, __a3) } == 0) as i32) != 0) ); From a76baf5ab3aaaa3797c415d46ebec1dd677e7a2f Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Sun, 26 Jul 2026 11:51:26 +0100 Subject: [PATCH 9/9] Update tests --- tests/unit/out/refcount/fd_io.rs | 2 +- tests/unit/out/unsafe/fd_io.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/out/refcount/fd_io.rs b/tests/unit/out/refcount/fd_io.rs index a8be4c06..f1272772 100644 --- a/tests/unit/out/refcount/fd_io.rs +++ b/tests/unit/out/refcount/fd_io.rs @@ -11,7 +11,7 @@ pub fn main() { } fn main_0() -> i32 { let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_fd_io_test.tmp", + b"cpp2rust_fd_io_test.tmp", ))); let fd: Value = Rc::new(RefCell::new({ let __mode = match &[(420).into()].first() { diff --git a/tests/unit/out/unsafe/fd_io.rs b/tests/unit/out/unsafe/fd_io.rs index 29c8057d..e629e621 100644 --- a/tests/unit/out/unsafe/fd_io.rs +++ b/tests/unit/out/unsafe/fd_io.rs @@ -13,7 +13,7 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut path: *const libc::c_char = - (c"/tmp/cpp2rust_fd_io_test.tmp".as_ptr().cast_mut()).cast_const(); + (c"cpp2rust_fd_io_test.tmp".as_ptr().cast_mut()).cast_const(); let mut fd: i32 = (unsafe { libc::open( path as *const i8,