diff --git a/rules/eventfd/tgt_refcount.rs b/rules/eventfd/tgt_refcount.rs new file mode 100644 index 00000000..01a5d103 --- /dev/null +++ b/rules/eventfd/tgt_refcount.rs @@ -0,0 +1,33 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +use libcc2rs::*; + +#[cfg(target_os = "linux")] +fn f1() -> i32 { + ::libc::EFD_CLOEXEC +} + +#[cfg(target_os = "linux")] +fn f2() -> i32 { + ::libc::EFD_NONBLOCK +} + +#[cfg(target_os = "linux")] +fn f3() -> i32 { + ::libc::EFD_SEMAPHORE +} + +#[cfg(target_os = "linux")] +fn f4(a0: u32, a1: i32) -> i32 { + match nix::sys::eventfd::EventFd::from_value_and_flags( + a0, + nix::sys::eventfd::EfdFlags::from_bits_truncate(a1), + ) { + Ok(__efd) => FdRegistry::register(std::os::fd::OwnedFd::from(__efd)), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } +} diff --git a/rules/socket/tgt_refcount.rs b/rules/socket/tgt_refcount.rs index aa5cbd9f..69bda802 100644 --- a/rules/socket/tgt_refcount.rs +++ b/rules/socket/tgt_refcount.rs @@ -202,3 +202,19 @@ fn f11(a0: i32, a1: i32, a2: i32, a3: Ptr) -> i32 { } } } + +fn f17(a0: i32, a1: i32) -> i32 { + match nix::sys::socket::Backlog::new(a1) { + Ok(__b) => match FdRegistry::with_fd(a0, |__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 + } + } +} diff --git a/rules/src/modules.rs b/rules/src/modules.rs index e1020d04..f2260e69 100644 --- a/rules/src/modules.rs +++ b/rules/src/modules.rs @@ -48,6 +48,8 @@ pub mod dirent_tgt_unsafe; pub mod errno_tgt_refcount; #[path = r#"../errno/tgt_unsafe.rs"#] pub mod errno_tgt_unsafe; +#[path = r#"../eventfd/tgt_refcount.rs"#] +pub mod eventfd_tgt_refcount; #[path = r#"../eventfd/tgt_unsafe.rs"#] pub mod eventfd_tgt_unsafe; #[path = r#"../fcntl/tgt_refcount.rs"#] diff --git a/rules/unistd/tgt_refcount.rs b/rules/unistd/tgt_refcount.rs index 2e58ed5c..c4d5c7ec 100644 --- a/rules/unistd/tgt_refcount.rs +++ b/rules/unistd/tgt_refcount.rs @@ -7,6 +7,22 @@ fn f1(a0: i32) -> i32 { FdRegistry::close(a0) } +fn f2(a0: i32, a1: i64, a2: i32) -> i64 { + let __whence = match a2 { + 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(a0, |__fd| nix::unistd::lseek(__fd, a1, __whence)) { + Ok(__off) => __off, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } +} + fn f3(a0: i32, a1: AnyPtr, a2: usize) -> isize { match FdRegistry::with_fd(a0, |__fd| { a1.reinterpret_cast::() @@ -45,6 +61,26 @@ fn f5(a0: Ptr) -> i32 { } } +fn f6(a0: i32, a1: i64) -> i32 { + match FdRegistry::with_fd(a0, |__fd| nix::unistd::ftruncate(__fd, a1)) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } +} + +fn f7(a0: i32) -> i32 { + match FdRegistry::with_fd(a0, |__fd| nix::unistd::isatty(__fd)) { + Ok(__tty) => __tty as i32, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + 0 + } + } +} + fn f8() -> u32 { nix::unistd::geteuid().as_raw() } diff --git a/tests/unit/fd_io.c b/tests/unit/fd_io.c index 7dcefe46..4f1fbeca 100644 --- a/tests/unit/fd_io.c +++ b/tests/unit/fd_io.c @@ -1,7 +1,6 @@ #include #include #include -#include #include int main(void) { @@ -19,18 +18,5 @@ int main(void) { assert(read(fd, buf, sizeof(buf)) == 0); assert(close(fd) == 0); assert(unlink(path) == 0); - int fds[2]; - assert(pipe(fds) == 0); - assert(write(fds[1], "ab", 2) == 2); - char buf2[4]; - memset(buf2, 0, sizeof(buf2)); - assert(read(fds[0], buf2, sizeof(buf2)) == 2); - assert(strcmp(buf2, "ab") == 0); - assert(close(fds[1]) == 0); - assert(read(fds[0], buf2, sizeof(buf2)) == 0); - assert(close(fds[0]) == 0); - int s = socket(AF_INET, SOCK_STREAM, 0); - assert(s >= 0); - assert(close(s) == 0); return 0; } diff --git a/tests/unit/isatty.c b/tests/unit/isatty.c new file mode 100644 index 00000000..e849816a --- /dev/null +++ b/tests/unit/isatty.c @@ -0,0 +1,13 @@ +#include +#include +#include + +int main(void) { + const char *path = "/tmp/cpp2rust_isatty_test.tmp"; + int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644); + assert(fd >= 0); + assert(isatty(fd) == 0); + assert(close(fd) == 0); + assert(unlink(path) == 0); + return 0; +} diff --git a/tests/unit/lseek_ftruncate.c b/tests/unit/lseek_ftruncate.c new file mode 100644 index 00000000..bd6a988a --- /dev/null +++ b/tests/unit/lseek_ftruncate.c @@ -0,0 +1,22 @@ +#include +#include +#include +#include + +int main(void) { + const char *path = "/tmp/cpp2rust_lseek_ftruncate_test.tmp"; + int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644); + assert(fd >= 0); + assert(write(fd, "hello world", 11) == 11); + assert(lseek(fd, 0, SEEK_END) == 11); + assert(lseek(fd, 6, SEEK_SET) == 6); + char buf[16]; + memset(buf, 0, sizeof(buf)); + assert(read(fd, buf, sizeof(buf)) == 5); + assert(strcmp(buf, "world") == 0); + assert(ftruncate(fd, 5) == 0); + assert(lseek(fd, 0, SEEK_END) == 5); + assert(close(fd) == 0); + assert(unlink(path) == 0); + return 0; +} diff --git a/tests/unit/out/refcount/fd_io.rs b/tests/unit/out/refcount/fd_io.rs index 1bc80d3a..a8be4c06 100644 --- a/tests/unit/out/refcount/fd_io.rs +++ b/tests/unit/out/refcount/fd_io.rs @@ -138,129 +138,5 @@ fn main_0() -> i32 { } == 0) as i32) != 0) ); - 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 buf2: Value> = Rc::new(RefCell::new( - (0..4).map(|_| ::default()).collect::>(), - )); - { - ((buf2.as_pointer() as Ptr) as Ptr) - .to_any() - .memset((0) as u8, ::std::mem::size_of::<[u8; 4]>() as usize); - ((buf2.as_pointer() as Ptr) as Ptr).to_any().clone() - }; - assert!( - (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - ((buf2.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 = (buf2.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| { - ((buf2.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)); - 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!((((FdRegistry::close((*s.borrow())) == 0) as i32) != 0)); return 0; } diff --git a/tests/unit/out/refcount/isatty.rs b/tests/unit/out/refcount/isatty.rs new file mode 100644 index 00000000..d2eeb87d --- /dev/null +++ b/tests/unit/out/refcount/isatty.rs @@ -0,0 +1,58 @@ +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 path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_isatty_test.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| 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) + ); + return 0; +} diff --git a/tests/unit/out/refcount/lseek_ftruncate.rs b/tests/unit/out/refcount/lseek_ftruncate.rs new file mode 100644 index 00000000..6e93161a --- /dev/null +++ b/tests/unit/out/refcount/lseek_ftruncate.rs @@ -0,0 +1,176 @@ +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 path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_lseek_ftruncate_test.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!( + (((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) + ); + return 0; +} diff --git a/tests/unit/out/refcount/pipe_io.rs b/tests/unit/out/refcount/pipe_io.rs new file mode 100644 index 00000000..5f51cf01 --- /dev/null +++ b/tests/unit/out/refcount/pipe_io.rs @@ -0,0 +1,109 @@ +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 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)); + return 0; +} diff --git a/tests/unit/out/refcount/socket_listen.rs b/tests/unit/out/refcount/socket_listen.rs new file mode 100644 index 00000000..1af600fa --- /dev/null +++ b/tests/unit/out/refcount/socket_listen.rs @@ -0,0 +1,61 @@ +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)); + 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)); + return 0; +} diff --git a/tests/unit/out/refcount/socket_open_close.rs b/tests/unit/out/refcount/socket_open_close.rs new file mode 100644 index 00000000..7cb8a701 --- /dev/null +++ b/tests/unit/out/refcount/socket_open_close.rs @@ -0,0 +1,43 @@ +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)); + assert!((((FdRegistry::close((*s.borrow())) == 0) as i32) != 0)); + return 0; +} diff --git a/tests/unit/out/unsafe/fd_io.rs b/tests/unit/out/unsafe/fd_io.rs index 92306d37..29c8057d 100644 --- a/tests/unit/out/unsafe/fd_io.rs +++ b/tests/unit/out/unsafe/fd_io.rs @@ -66,51 +66,5 @@ unsafe fn main_0() -> i32 { ); assert!(((((libc::close(fd)) == (0)) as i32) != 0)); assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); - 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 buf2: [libc::c_char; 4] = [(0 as libc::c_char); 4]; - { - let byte_0 = (buf2.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; - } - (buf2.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) - }; - assert!( - ((((libc::read( - fds[(0) as usize], - (buf2.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( - (buf2.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], - (buf2.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)); - let mut s: i32 = libc::socket(libc::AF_INET, libc::SOCK_STREAM, 0); - assert!(((((s) >= (0)) as i32) != 0)); - assert!(((((libc::close(s)) == (0)) as i32) != 0)); return 0; } diff --git a/tests/unit/out/unsafe/isatty.rs b/tests/unit/out/unsafe/isatty.rs new file mode 100644 index 00000000..3856611f --- /dev/null +++ b/tests/unit/out/unsafe/isatty.rs @@ -0,0 +1,29 @@ +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 path: *const libc::c_char = + (c"/tmp/cpp2rust_isatty_test.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::isatty(fd)) == (0)) as i32) != 0)); + assert!(((((libc::close(fd)) == (0)) as i32) != 0)); + assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); + return 0; +} diff --git a/tests/unit/out/unsafe/lseek_ftruncate.rs b/tests/unit/out/unsafe/lseek_ftruncate.rs new file mode 100644 index 00000000..579c1b54 --- /dev/null +++ b/tests/unit/out/unsafe/lseek_ftruncate.rs @@ -0,0 +1,65 @@ +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 path: *const libc::c_char = (c"/tmp/cpp2rust_lseek_ftruncate_test.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::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)); + return 0; +} diff --git a/tests/unit/out/unsafe/pipe_io.rs b/tests/unit/out/unsafe/pipe_io.rs new file mode 100644 index 00000000..2455e40d --- /dev/null +++ b/tests/unit/out/unsafe/pipe_io.rs @@ -0,0 +1,59 @@ +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 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)); + return 0; +} diff --git a/tests/unit/out/unsafe/socket_listen.rs b/tests/unit/out/unsafe/socket_listen.rs new file mode 100644 index 00000000..ca55cbf7 --- /dev/null +++ b/tests/unit/out/unsafe/socket_listen.rs @@ -0,0 +1,20 @@ +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)); + assert!(((((libc::listen(s, 5)) == (0)) as i32) != 0)); + assert!(((((libc::close(s)) == (0)) as i32) != 0)); + return 0; +} diff --git a/tests/unit/out/unsafe/socket_open_close.rs b/tests/unit/out/unsafe/socket_open_close.rs new file mode 100644 index 00000000..6f8ccf39 --- /dev/null +++ b/tests/unit/out/unsafe/socket_open_close.rs @@ -0,0 +1,19 @@ +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)); + assert!(((((libc::close(s)) == (0)) as i32) != 0)); + return 0; +} diff --git a/tests/unit/pipe_io.c b/tests/unit/pipe_io.c new file mode 100644 index 00000000..4d5cfa99 --- /dev/null +++ b/tests/unit/pipe_io.c @@ -0,0 +1,17 @@ +#include +#include +#include + +int main(void) { + int fds[2]; + assert(pipe(fds) == 0); + assert(write(fds[1], "ab", 2) == 2); + char buf[4]; + memset(buf, 0, sizeof(buf)); + assert(read(fds[0], buf, sizeof(buf)) == 2); + assert(strcmp(buf, "ab") == 0); + assert(close(fds[1]) == 0); + assert(read(fds[0], buf, sizeof(buf)) == 0); + assert(close(fds[0]) == 0); + return 0; +} diff --git a/tests/unit/socket_listen.c b/tests/unit/socket_listen.c new file mode 100644 index 00000000..f90d1895 --- /dev/null +++ b/tests/unit/socket_listen.c @@ -0,0 +1,11 @@ +#include +#include +#include + +int main(void) { + int s = socket(AF_INET, SOCK_STREAM, 0); + assert(s >= 0); + assert(listen(s, 5) == 0); + assert(close(s) == 0); + return 0; +} diff --git a/tests/unit/socket_open_close.c b/tests/unit/socket_open_close.c new file mode 100644 index 00000000..e9a8ddae --- /dev/null +++ b/tests/unit/socket_open_close.c @@ -0,0 +1,10 @@ +#include +#include +#include + +int main(void) { + int s = socket(AF_INET, SOCK_STREAM, 0); + assert(s >= 0); + assert(close(s) == 0); + return 0; +}