From 4671d73b59c7cad4826a5693716c5a19c6538445 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 20 Jul 2026 22:38:05 +0100 Subject: [PATCH 1/7] Replace std::fs::File with libcc2rs::CFile --- libcc2rs/src/fd.rs | 23 +- libcc2rs/src/io.rs | 106 +- libcc2rs/src/libc_shims/cfile.rs | 122 ++ libcc2rs/src/libc_shims/mod.rs | 2 + rules/stdio/tgt_refcount.rs | 228 +-- tests/ub/file_use_after_fclose.c | 11 + .../ub/out/refcount/file_use_after_fclose.rs | 41 + tests/ub/out/unsafe/file_use_after_fclose.rs | 26 + tests/unit/errno.c | 1 - tests/unit/out/refcount/errno.rs | 18 +- tests/unit/out/refcount/fd_io.rs | 1819 ++++++++++++++++- tests/unit/out/refcount/fflush_null.rs | 13 +- .../out/refcount/fn_ptr_stdlib_compare.rs | 173 +- tests/unit/out/refcount/fopen.rs | 25 +- .../refcount/global_without_initializer.rs | 2 +- .../out/refcount/goto_aggregate_default.rs | 2 +- tests/unit/out/refcount/printfs.rs | 2 +- .../out/refcount/ptr_to_incomplete_struct.rs | 6 +- tests/unit/out/refcount/stdio.rs | 151 ++ tests/unit/out/refcount/stdio_nofd.rs | 631 ++---- tests/unit/out/refcount/sys_stat.rs | 128 ++ tests/unit/out/refcount/unistd.rs | 673 ++++++ .../out/refcount/user_defined_same_as_libc.rs | 4 +- .../out/refcount/va_arg_non_primitive_ptrs.rs | 8 +- tests/unit/out/unsafe/fd_io.rs | 497 ++++- tests/unit/stdio.c | 1 - tests/unit/sys_stat.c | 1 - tests/unit/unistd.c | 2 +- 28 files changed, 3898 insertions(+), 818 deletions(-) create mode 100644 libcc2rs/src/libc_shims/cfile.rs create mode 100644 tests/ub/file_use_after_fclose.c create mode 100644 tests/ub/out/refcount/file_use_after_fclose.rs create mode 100644 tests/ub/out/unsafe/file_use_after_fclose.rs create mode 100644 tests/unit/out/refcount/stdio.rs create mode 100644 tests/unit/out/refcount/sys_stat.rs create mode 100644 tests/unit/out/refcount/unistd.rs diff --git a/libcc2rs/src/fd.rs b/libcc2rs/src/fd.rs index 64bfb795..3e846624 100644 --- a/libcc2rs/src/fd.rs +++ b/libcc2rs/src/fd.rs @@ -6,10 +6,16 @@ use std::os::fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd}; pub struct FdRegistry { fds: Vec>, + seeded: bool, } thread_local! { - static FD_REGISTRY: RefCell = const { RefCell::new(FdRegistry { fds: Vec::new() }) }; + static FD_REGISTRY: RefCell = const { + RefCell::new(FdRegistry { + fds: Vec::new(), + seeded: false, + }) + }; } impl FdRegistry { @@ -27,6 +33,19 @@ impl FdRegistry { raw } + fn seed_std(&mut self) { + if self.seeded { + return; + } + self.seeded = true; + if self.fds.len() < 3 { + self.fds.resize_with(3, || None); + } + self.fds[0] = std::io::stdin().as_fd().try_clone_to_owned().ok(); + self.fds[1] = std::io::stdout().as_fd().try_clone_to_owned().ok(); + self.fds[2] = std::io::stderr().as_fd().try_clone_to_owned().ok(); + } + fn lookup(&self, fd: i32) -> BorrowedFd<'_> { self.fds .get(fd as usize) @@ -37,6 +56,7 @@ impl FdRegistry { pub fn with_fd(fd: i32, f: impl FnOnce(BorrowedFd<'_>) -> R) -> R { FD_REGISTRY.with(|r| { + r.borrow_mut().seed_std(); let reg = r.borrow(); f(reg.lookup(fd)) }) @@ -44,6 +64,7 @@ impl FdRegistry { pub fn with_fds(fds: &[i32], f: impl FnOnce(&[BorrowedFd<'_>]) -> R) -> R { FD_REGISTRY.with(|r| { + r.borrow_mut().seed_std(); let reg = r.borrow(); let borrowed: Vec> = fds.iter().map(|&fd| reg.lookup(fd)).collect(); f(&borrowed) diff --git a/libcc2rs/src/io.rs b/libcc2rs/src/io.rs index fef060ce..fe9a1718 100644 --- a/libcc2rs/src/io.rs +++ b/libcc2rs/src/io.rs @@ -1,7 +1,7 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::{AnyPtr, AsPointer, Ptr, Value}; +use crate::{AnyPtr, AsPointer, CFile, Ptr, Value}; use std::cell::{RefCell, UnsafeCell}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; @@ -48,6 +48,24 @@ thread_local! { }; } +thread_local! { + static C_STDIN: Value = Rc::new(RefCell::new(CFile::new(0))); + static C_STDOUT: Value = Rc::new(RefCell::new(CFile::new(1))); + static C_STDERR: Value = Rc::new(RefCell::new(CFile::new(2))); +} + +pub fn c_stdin() -> Ptr { + C_STDIN.with(AsPointer::as_pointer) +} + +pub fn c_stdout() -> Ptr { + C_STDOUT.with(AsPointer::as_pointer) +} + +pub fn c_stderr() -> Ptr { + C_STDERR.with(AsPointer::as_pointer) +} + pub fn cin() -> Ptr { SAFE_STDIN.with(AsPointer::as_pointer) } @@ -84,79 +102,41 @@ pub unsafe fn cerr_unsafe() -> *mut std::fs::File { UNSAFE_STDERR.with(UnsafeCell::get) } -pub fn fread_refcount(a0: AnyPtr, a1: usize, a2: usize, a3: Ptr<::std::fs::File>) -> usize { +pub fn fread_refcount(a0: AnyPtr, a1: usize, a2: usize, a3: Ptr) -> usize { let total = a1.saturating_mul(a2); + if total == 0 { + return 0; + } let mut dst = a0.reinterpret_cast::(); - - let f = (*a3.upgrade().deref()) - .try_clone() - .expect("try_clone failed"); - let mut reader = std::io::BufReader::with_capacity(64 * 1024, f); - - let mut read_bytes: usize = 0; let mut buffer: [u8; 8192] = [0; 8192]; + let mut read_bytes: usize = 0; - while read_bytes < total { - let remaining = total - read_bytes; - let to_read = std::cmp::min(buffer.len(), remaining); - - let n = match std::io::Read::read(&mut reader, &mut buffer[..to_read]) { - Ok(0) => break, - Ok(n) => n, - Err(ref e) if e.kind() == std::io::ErrorKind::Interrupted => continue, - Err(e) => panic!("Unhandled error in fread: {e}"), - }; - - for &byte in &buffer[..n] { - dst.write(byte); - dst += 1; + a3.with_mut(|f| { + while read_bytes < total { + let to_read = std::cmp::min(buffer.len(), total - read_bytes); + let n = f.read(&mut buffer[..to_read]); + if n == 0 { + break; + } + for &byte in &buffer[..n] { + dst.write(byte); + dst += 1; + } + read_bytes += n; } - - read_bytes += n; - } + }); read_bytes / a1 } -pub fn fwrite_refcount(a0: AnyPtr, a1: usize, a2: usize, a3: Ptr<::std::fs::File>) -> usize { +pub fn fwrite_refcount(a0: AnyPtr, a1: usize, a2: usize, a3: Ptr) -> usize { let total = a1.saturating_mul(a2); - let mut src = a0.reinterpret_cast::(); - - let f = (*a3.upgrade().deref()) - .try_clone() - .expect("try_clone failed"); - let mut writer = std::io::BufWriter::with_capacity(64 * 1024, f); - - let mut written_bytes: usize = 0; - let mut buffer: [u8; 8192] = [0; 8192]; - - while written_bytes < total { - let remaining = total - written_bytes; - let to_fill = std::cmp::min(buffer.len(), remaining); - - for b in buffer.iter_mut().take(to_fill) { - *b = src.read(); - src += 1; - } - - let mut off = 0; - while off < to_fill { - match std::io::Write::write(&mut writer, &buffer[off..to_fill]) { - Ok(0) => break, - Ok(n) => off += n, - Err(ref e) if e.kind() == std::io::ErrorKind::Interrupted => continue, - Err(e) => panic!("Unhandled error in fwrite: {e}"), - } - } - - if off == 0 { - break; - } - - written_bytes += off; + if total == 0 { + return 0; } - - written_bytes / a1 + let src = a0.reinterpret_cast::(); + let written = src.with_slice(total, |bytes| a3.with_mut(|f| f.write(bytes))); + written / a1 } unsafe extern "C" { diff --git a/libcc2rs/src/libc_shims/cfile.rs b/libcc2rs/src/libc_shims/cfile.rs new file mode 100644 index 00000000..914f3bdb --- /dev/null +++ b/libcc2rs/src/libc_shims/cfile.rs @@ -0,0 +1,122 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +use crate::FdRegistry; + +pub struct CFile { + pub fd: i32, + pub eof: bool, + pub err: bool, +} + +impl CFile { + pub fn new(fd: i32) -> Self { + CFile { + fd, + eof: false, + err: false, + } + } + + pub fn open(path: &str, mode: &str) -> Option { + let flags = match mode { + "rb" => nix::fcntl::OFlag::O_RDONLY, + "wb" => nix::fcntl::OFlag::O_WRONLY + .union(nix::fcntl::OFlag::O_CREAT) + .union(nix::fcntl::OFlag::O_TRUNC), + m => panic!("fopen: unsupported mode {:?}", m), + }; + match nix::fcntl::open(path, flags, nix::sys::stat::Mode::from_bits_truncate(0o666)) { + Ok(ofd) => Some(CFile::new(FdRegistry::register(ofd))), + Err(e) => { + crate::cpp2rust_errno().write(e as i32); + None + } + } + } + + pub fn read(&mut self, buf: &mut [u8]) -> usize { + let mut n = 0; + while n < buf.len() { + match FdRegistry::with_fd(self.fd, |b| nix::unistd::read(b, &mut buf[n..])) { + Ok(0) => { + self.eof = true; + break; + } + Ok(k) => n += k, + Err(nix::errno::Errno::EINTR) => {} + Err(e) => { + self.err = true; + crate::cpp2rust_errno().write(e as i32); + break; + } + } + } + n + } + + pub fn write(&mut self, buf: &[u8]) -> usize { + let mut n = 0; + while n < buf.len() { + match FdRegistry::with_fd(self.fd, |b| nix::unistd::write(b, &buf[n..])) { + Ok(0) => { + self.err = true; + break; + } + Ok(k) => n += k, + Err(nix::errno::Errno::EINTR) => {} + Err(e) => { + self.err = true; + crate::cpp2rust_errno().write(e as i32); + break; + } + } + } + n + } + + pub fn seek(&mut self, offset: i64, whence: i32) -> i64 { + let w = match whence { + 0 => nix::unistd::Whence::SeekSet, + 1 => nix::unistd::Whence::SeekCur, + 2 => nix::unistd::Whence::SeekEnd, + other => panic!("fseek: unsupported whence {}", other), + }; + match FdRegistry::with_fd(self.fd, |b| nix::unistd::lseek(b, offset, w)) { + Ok(off) => { + self.eof = false; + off + } + Err(e) => { + crate::cpp2rust_errno().write(e as i32); + -1 + } + } + } + + pub fn tell(&self) -> i64 { + match FdRegistry::with_fd(self.fd, |b| { + nix::unistd::lseek(b, 0, nix::unistd::Whence::SeekCur) + }) { + Ok(off) => off, + Err(e) => { + crate::cpp2rust_errno().write(e as i32); + -1 + } + } + } + + pub fn getc(&mut self) -> i32 { + let mut b = [0u8; 1]; + match self.read(&mut b) { + 1 => b[0] as i32, + _ => -1, + } + } + + pub fn close(&self) -> i32 { + FdRegistry::close(self.fd) + } +} + +impl crate::ByteRepr for CFile {} diff --git a/libcc2rs/src/libc_shims/mod.rs b/libcc2rs/src/libc_shims/mod.rs index be806985..016ab7f9 100644 --- a/libcc2rs/src/libc_shims/mod.rs +++ b/libcc2rs/src/libc_shims/mod.rs @@ -1,6 +1,7 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. +mod cfile; mod dirent; mod fdset; mod ifaddrs; @@ -13,6 +14,7 @@ mod stat; mod termios; mod time; +pub use cfile::*; pub use dirent::*; pub use fdset::*; pub use ifaddrs::*; diff --git a/rules/stdio/tgt_refcount.rs b/rules/stdio/tgt_refcount.rs index 94a1a879..e275508d 100644 --- a/rules/stdio/tgt_refcount.rs +++ b/rules/stdio/tgt_refcount.rs @@ -2,59 +2,36 @@ // Distributed under the MIT license that can be found in the LICENSE file. use libcc2rs::*; -use std::io::prelude::*; -use std::os::fd::AsFd; -fn t1() -> Ptr<::std::fs::File> { +fn t1() -> Ptr { Ptr::null() } -fn f1(a0: Ptr, a1: Ptr) -> Ptr<::std::fs::File> { - match a1.to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open(a0.to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open(a0.to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), +fn f1(a0: Ptr, a1: Ptr) -> Ptr { + match CFile::open(&a0.to_rust_string(), &a1.to_rust_string()) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), } } -fn f2(a0: Ptr<::std::fs::File>) -> i32 { +fn f2(a0: Ptr) -> i32 { + let __r = a0.with(|__f| __f.close()); a0.delete(); - 0 + __r } -fn f3(a0: Ptr<::std::fs::File>) -> i64 { - a0.with_mut(|v| match v.stream_position() { - Ok(pos) => pos as i64, - Err(_) => -1, - }) +fn f3(a0: Ptr) -> i64 { + a0.with(|__f| __f.tell()) } -fn f4(a0: &mut ::std::fs::File, a1: i64, a2: i32) -> i32 { - if (match a2 { - 0 => a0.seek(std::io::SeekFrom::Start(a1 as u64)), - 1 => a0.seek(std::io::SeekFrom::Current(a1)), - 2 => a0.seek(std::io::SeekFrom::End(a1)), - _ => Err(std::io::Error::other("unsupported whence for fseek.")), - }) - .is_ok() - { - 0 - } else { - -1 +fn f4(a0: &mut CFile, a1: i64, a2: i32) -> i32 { + match a0.seek(a1, a2) { + -1 => -1, + _ => 0, } } -fn f5(a0: AnyPtr, a1: usize, a2: usize, a3: Ptr<::std::fs::File>) -> usize { +fn f5(a0: AnyPtr, a1: usize, a2: usize, a3: Ptr) -> usize { let __a0 = a0; let __a1 = a1; let __a2 = a2; @@ -62,7 +39,7 @@ fn f5(a0: AnyPtr, a1: usize, a2: usize, a3: Ptr<::std::fs::File>) -> usize { libcc2rs::fread_refcount(__a0, __a1, __a2, __a3) } -fn f6(a0: AnyPtr, a1: usize, a2: usize, a3: Ptr<::std::fs::File>) -> usize { +fn f6(a0: AnyPtr, a1: usize, a2: usize, a3: Ptr) -> usize { let __a0 = a0; let __a1 = a1; let __a2 = a2; @@ -70,99 +47,83 @@ fn f6(a0: AnyPtr, a1: usize, a2: usize, a3: Ptr<::std::fs::File>) -> usize { libcc2rs::fwrite_refcount(__a0, __a1, __a2, __a3) } -fn f7(a0: Ptr<::std::fs::File>) -> i32 { - if !a0.is_null() { - match a0.with_mut(|v| v.sync_all()) { - Ok(_) => 0, - Err(_) => -1, - } - } else { - ::std::io::stdout().flush().unwrap(); - ::std::io::stderr().flush().unwrap(); - 0 - } +fn f7(a0: Ptr) -> i32 { + 0 } -fn f8() -> Ptr<::std::fs::File> { - libcc2rs::cout() +fn f8() -> Ptr { + libcc2rs::c_stdout() } -fn f9() -> Ptr<::std::fs::File> { - libcc2rs::cerr() +fn f9() -> Ptr { + libcc2rs::c_stderr() } -fn f10() -> Ptr<::std::fs::File> { - libcc2rs::cin() +fn f10() -> Ptr { + libcc2rs::c_stdin() } -fn f11(a0: i32, a1: Ptr<::std::fs::File>) -> i32 { +fn f11(a0: i32, a1: Ptr) -> i32 { let __c = a0 as u8; - match a1.with_mut(|__f| ::std::io::Write::write_all(__f, &[__c])) { - Ok(()) => __c as i32, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } + match a1.with_mut(|__f| __f.write(&[__c])) { + 1 => __c as i32, + _ => -1, } } -fn f12(a0: Ptr, a1: Ptr<::std::fs::File>) -> i32 { +fn f12(a0: Ptr, a1: Ptr) -> i32 { let __bytes: Vec = a0.to_c_string_iterator().collect(); - match a1.with_mut(|__f| ::std::io::Write::write_all(__f, &__bytes)) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } + match a1.with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, } } fn f13(a0: Ptr) -> i32 { - let __bytes: Vec = a0.to_c_string_iterator().collect(); - match libcc2rs::cout().with_mut(|__f| { - ::std::io::Write::write_all(__f, &__bytes) - .and_then(|_| ::std::io::Write::write_all(__f, b"\n")) - }) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } + let mut __bytes: Vec = a0.to_c_string_iterator().collect(); + __bytes.push(b'\n'); + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, } } -fn f17(a0: Ptr, a1: i32, a2: Ptr<::std::fs::File>) -> Ptr { +fn f14(a0: Ptr) -> i32 { + a0.with(|__f| __f.fd) +} + +fn f15(a0: Ptr) -> i32 { + a0.with(|__f| __f.err) as i32 +} + +fn f16(a0: Ptr) -> i32 { + a0.with(|__f| __f.eof) as i32 +} + +fn f17(a0: Ptr, a1: i32, a2: Ptr) -> Ptr { let __buf = a0.clone(); let __n = a1; - let __stream = a2.clone(); if __n <= 0 { Ptr::null() } else { let __max = (__n - 1) as usize; let mut __dst = __buf.clone(); let mut __count: usize = 0; - let mut __failed = false; - while __count < __max { - let mut __b: [u8; 1] = [0]; - match __stream.with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { - Ok(0) => break, - Ok(_) => { - __dst.write(__b[0]); - __dst += 1; - __count += 1; - if __b[0] == b'\n' { - break; - } + let __failed = a2.with_mut(|__f| { + while __count < __max { + let __c = __f.getc(); + if __c < 0 { + break; } - Err(__e) => { - if __e.kind() != ::std::io::ErrorKind::Interrupted { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - __failed = true; - break; - } + __dst.write(__c as u8); + __dst += 1; + __count += 1; + if __c as u8 == b'\n' { + break; } } - } + __f.err + }); if __failed || __count == 0 { Ptr::null() } else { @@ -172,47 +133,35 @@ fn f17(a0: Ptr, a1: i32, a2: Ptr<::std::fs::File>) -> Ptr { } } -fn f18(a0: Ptr, a1: Ptr, a2: Ptr<::std::fs::File>) -> Ptr<::std::fs::File> { +fn f18(a0: Ptr, a1: Ptr, a2: Ptr) -> Ptr { let __stream = a2.clone(); - let __new = match a1.to_rust_string().as_str() { - "rb" => ::std::fs::OpenOptions::new() - .read(true) - .open(a0.to_rust_string()), - "wb" => ::std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open(a0.to_rust_string()), - _ => panic!("unsupported mode"), - }; - match __new { - Ok(__f) => { + let __old = __stream.with(|__f| __f.fd); + match __old { + 0..=2 => {} + __fd => { + FdRegistry::close(__fd); + } + } + match CFile::open(&a0.to_rust_string(), &a1.to_rust_string()) { + Some(__f) => { __stream.write(__f); __stream } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - Ptr::null() - } + None => Ptr::null(), } } -fn f19(a0: Ptr<::std::fs::File>, a1: i64, a2: i32) -> i32 { - let __r = a0.with_mut(|__f| match a2 { - 0 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Start(a1 as u64)), - 1 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Current(a1)), - 2 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::End(a1)), - _ => Err(::std::io::Error::other("unsupported whence for fseeko.")), - }); - match __r { - Ok(_) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EINVAL)); - -1 - } +fn f19(a0: Ptr, a1: i64, a2: i32) -> i32 { + match a0.with_mut(|__f| __f.seek(a1, a2)) { + -1 => -1, + _ => 0, } } +fn f20(a0: i32, a1: Ptr) -> Ptr { + Ptr::alloc(CFile::new(a0)) +} + fn f21(a0: Ptr, a1: usize, a2: Ptr, va: &[VaArg]) -> i32 { let __s = libcc2rs::format_c(&a2.to_rust_string(), va); let __b = __s.as_bytes(); @@ -236,19 +185,10 @@ fn f22(a0: Ptr, a1: Ptr) -> i32 { } } -fn f23(a0: Ptr<::std::fs::File>) -> i32 { - let mut __b: [u8; 1] = [0]; - match a0.with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { - Ok(0) => -1, - Ok(_) => __b[0] as i32, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } - } +fn f23(a0: Ptr) -> i32 { + a0.with_mut(|__f| __f.getc()) } -fn f24(a0: Ptr<::std::fs::File>, a1: Ptr, a2: i32, a3: usize) -> i32 { - /* std::fs::File is unbuffered */ +fn f24(a0: Ptr, a1: Ptr, a2: i32, a3: usize) -> i32 { 0 } diff --git a/tests/ub/file_use_after_fclose.c b/tests/ub/file_use_after_fclose.c new file mode 100644 index 00000000..53114d5d --- /dev/null +++ b/tests/ub/file_use_after_fclose.c @@ -0,0 +1,11 @@ +// panic-ub: refcount +// nondet-result: unsafe +#include +#include + +int main(void) { + FILE *fp = fopen("/tmp/cpp2rust_uafc_test.tmp", "wb"); + assert(fp); + fclose(fp); + return fputc('x', fp) == 'x' ? 1 : 0; +} diff --git a/tests/ub/out/refcount/file_use_after_fclose.rs b/tests/ub/out/refcount/file_use_after_fclose.rs new file mode 100644 index 00000000..73c4f73e --- /dev/null +++ b/tests/ub/out/refcount/file_use_after_fclose.rs @@ -0,0 +1,41 @@ +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 fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &Ptr::from_string_literal(b"/tmp/cpp2rust_uafc_test.tmp").to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }, + )); + assert!(!(*fp.borrow()).is_null()); + { + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + }; + return if ((({ + let __c = ('x' as i32) as u8; + match (*fp.borrow()).with_mut(|__f| __f.write(&[__c])) { + 1 => __c as i32, + _ => -1, + } + } == ('x' as i32)) as i32) + != 0) + { + 1 + } else { + 0 + }; +} diff --git a/tests/ub/out/unsafe/file_use_after_fclose.rs b/tests/ub/out/unsafe/file_use_after_fclose.rs new file mode 100644 index 00000000..c576dd9d --- /dev/null +++ b/tests/ub/out/unsafe/file_use_after_fclose.rs @@ -0,0 +1,26 @@ +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 fp: *mut ::libc::FILE = libc::fopen( + (c"/tmp/cpp2rust_uafc_test.tmp".as_ptr().cast_mut()).cast_const(), + (c"wb".as_ptr().cast_mut()).cast_const(), + ); + assert!(!(fp).is_null()); + libc::fclose(fp); + return if ((((libc::fputc(('x' as i32), fp)) == ('x' as i32)) as i32) != 0) { + 1 + } else { + 0 + }; +} diff --git a/tests/unit/errno.c b/tests/unit/errno.c index 9f9f0e3f..ec58a397 100644 --- a/tests/unit/errno.c +++ b/tests/unit/errno.c @@ -1,4 +1,3 @@ -// panic: refcount #include #include #include diff --git a/tests/unit/out/refcount/errno.rs b/tests/unit/out/refcount/errno.rs index a326cca0..f310f414 100644 --- a/tests/unit/out/refcount/errno.rs +++ b/tests/unit/out/refcount/errno.rs @@ -28,21 +28,9 @@ pub fn test_errno_preserved_across_strdup_1() { pub fn test_errno_from_fseek_2() { libcc2rs::cpp2rust_errno().write(0); let r: Value = Rc::new(RefCell::new( - if (match 0 { - 0 => libcc2rs::cin().with_mut(|__v: &mut ::std::fs::File| { - __v.seek(std::io::SeekFrom::Start(0_i64 as u64)) - }), - 1 => libcc2rs::cin() - .with_mut(|__v: &mut ::std::fs::File| __v.seek(std::io::SeekFrom::Current(0_i64))), - 2 => libcc2rs::cin() - .with_mut(|__v: &mut ::std::fs::File| __v.seek(std::io::SeekFrom::End(0_i64))), - _ => Err(std::io::Error::other("unsupported whence for fseek.")), - }) - .is_ok() - { - 0 - } else { - -1 + match libcc2rs::c_stdin().with_mut(|__v: &mut CFile| __v.seek(0_i64, 0)) { + -1 => -1, + _ => 0, }, )); assert!(((((*r.borrow()) == -1_i32) as i32) != 0)); diff --git a/tests/unit/out/refcount/fd_io.rs b/tests/unit/out/refcount/fd_io.rs index f1272772..e63aa312 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,1817 @@ 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 test_fileno_12() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_fd_io_fileno.tmp", + ))); + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }, + )); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + assert!( + ((({ + let __a0 = Ptr::from_string_literal(b"hello").to_any(); + let __a1 = 1_usize; + let __a2 = 5_usize; + let __a3 = (*fp.borrow()).clone(); + libcc2rs::fwrite_refcount(__a0, __a1, __a2, __a3) + } == 5_usize) as i32) + != 0) + ); + assert!((((0 == 0) as i32) != 0)); + let fd: Value = Rc::new(RefCell::new((*fp.borrow()).with(|__f| __f.fd))); + assert!(((((*fd.borrow()) >= 0) as i32) != 0)); + assert!( + ((({ + let _lhs = (*fp.borrow()).with(|__f| __f.fd); + _lhs == (*fd.borrow()) + }) 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!( + ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + } == 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_fdopen_13() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_fd_io_fdopen.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)); + let fp: Value> = Rc::new(RefCell::new(Ptr::alloc(CFile::new((*fd.borrow()))))); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + assert!( + ((({ + let __a0 = Ptr::from_string_literal(b"hi").to_any(); + let __a1 = 1_usize; + let __a2 = 2_usize; + let __a3 = (*fp.borrow()).clone(); + libcc2rs::fwrite_refcount(__a0, __a1, __a2, __a3) + } == 2_usize) as i32) + != 0) + ); + assert!( + ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + } == 0) as i32) + != 0) + ); + (*fd.borrow_mut()) = { + let __mode = match &[].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), + __mode, + ) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + }; + assert!(((((*fd.borrow()) >= 0) 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((*fd.borrow()), |__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"hi").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_feof_ferror_14() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_fd_io_eof.tmp", + ))); + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }, + )); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + assert!( + ((({ + let __a0 = Ptr::from_string_literal(b"ab").to_any(); + let __a1 = 1_usize; + let __a2 = 2_usize; + let __a3 = (*fp.borrow()).clone(); + libcc2rs::fwrite_refcount(__a0, __a1, __a2, __a3) + } == 2_usize) as i32) + != 0) + ); + assert!( + ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + } == 0) as i32) + != 0) + ); + (*fp.borrow_mut()) = match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"rb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }; + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + assert!(((!((*fp.borrow()).with(|__f| __f.eof) as i32 != 0) as i32) != 0)); + assert!(((!((*fp.borrow()).with(|__f| __f.err) as i32 != 0) as i32) != 0)); + let buf: Value> = Rc::new(RefCell::new( + (0..2).map(|_| ::default()).collect::>(), + )); + assert!( + ((({ + let __a0 = ((buf.as_pointer() as Ptr) as Ptr).to_any(); + let __a1 = 1_usize; + let __a2 = 2_usize; + let __a3 = (*fp.borrow()).clone(); + libcc2rs::fread_refcount(__a0, __a1, __a2, __a3) + } == 2_usize) as i32) + != 0) + ); + assert!(((!((*fp.borrow()).with(|__f| __f.eof) as i32 != 0) as i32) != 0)); + assert!( + ((({ + let __a0 = ((buf.as_pointer() as Ptr) as Ptr).to_any(); + let __a1 = 1_usize; + let __a2 = 1_usize; + let __a3 = (*fp.borrow()).clone(); + libcc2rs::fread_refcount(__a0, __a1, __a2, __a3) + } == 0_usize) as i32) + != 0) + ); + assert!(((*fp.borrow()).with(|__f| __f.eof) as i32 != 0)); + assert!(((!((*fp.borrow()).with(|__f| __f.err) as i32 != 0) as i32) != 0)); + assert!( + (((match (*fp.borrow()).with_mut(|__v: &mut CFile| __v.seek(0_i64, 0)) { + -1 => -1, + _ => 0, + } == 0) as i32) + != 0) + ); + assert!(((!((*fp.borrow()).with(|__f| __f.eof) as i32 != 0) as i32) != 0)); + assert!( + ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + } == 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 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() }); + ({ test_fileno_12() }); + ({ test_fdopen_13() }); + ({ test_feof_ferror_14() }); return 0; } diff --git a/tests/unit/out/refcount/fflush_null.rs b/tests/unit/out/refcount/fflush_null.rs index 61c6c9c9..b9314f49 100644 --- a/tests/unit/out/refcount/fflush_null.rs +++ b/tests/unit/out/refcount/fflush_null.rs @@ -10,15 +10,6 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let file_ptr: Value> = Rc::new(RefCell::new(Ptr::null())); - return if !(*file_ptr.borrow()).is_null() { - match (*file_ptr.borrow()).with_mut(|v| v.sync_all()) { - Ok(_) => 0, - Err(_) => -1, - } - } else { - ::std::io::stdout().flush().unwrap(); - ::std::io::stderr().flush().unwrap(); - 0 - }; + let file_ptr: Value> = Rc::new(RefCell::new(Ptr::null())); + return 0; } diff --git a/tests/unit/out/refcount/fn_ptr_stdlib_compare.rs b/tests/unit/out/refcount/fn_ptr_stdlib_compare.rs index 96ed54c2..b809e187 100644 --- a/tests/unit/out/refcount/fn_ptr_stdlib_compare.rs +++ b/tests/unit/out/refcount/fn_ptr_stdlib_compare.rs @@ -24,45 +24,37 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let fn1: Value) -> usize>> = + let fn1: Value) -> usize>> = Rc::new(RefCell::new(FnPtr::< - fn(AnyPtr, usize, usize, Ptr<::std::fs::File>) -> usize, + fn(AnyPtr, usize, usize, Ptr) -> usize, >::new(libcc2rs::fread_refcount))); assert!({ let _lhs = (*fn1.borrow()).clone(); - _lhs == FnPtr::) -> usize>::new( + _lhs == FnPtr::) -> usize>::new( libcc2rs::fread_refcount, ) }); assert!(!((*fn1.borrow()).is_null())); let fn2: Value, usize, usize, AnyPtr) -> usize>> = Rc::new(RefCell::new( - FnPtr::) -> usize>::new( - libcc2rs::fread_refcount, - ) - .cast::, usize, usize, AnyPtr) -> usize>(Some( + FnPtr::) -> usize>::new(libcc2rs::fread_refcount) + .cast::, usize, usize, AnyPtr) -> usize>(Some( (|a0: Ptr, a1: usize, a2: usize, a3: AnyPtr| -> usize { - libcc2rs::fread_refcount( - a0.to_any(), - a1, - a2, - a3.reinterpret_cast::<::std::fs::File>(), - ) + libcc2rs::fread_refcount(a0.to_any(), a1, a2, a3.reinterpret_cast::()) }) as fn(Ptr, usize, usize, AnyPtr) -> usize, )), )); assert!({ let _lhs = (*fn1.borrow()).clone(); - _lhs == ((*fn2.borrow()) - .cast::) -> usize>(None)) - .clone() + _lhs == ((*fn2.borrow()).cast::) -> usize>(None)) + .clone() }); - let f3: Value) -> usize>> = + let f3: Value) -> usize>> = Rc::new(RefCell::new( FnPtr::, usize, usize, AnyPtr) -> usize>::new(my_alternative_fread_0) - .cast::) -> usize>(Some( - (|a0: AnyPtr, a1: usize, a2: usize, a3: Ptr<::std::fs::File>| -> usize { + .cast::) -> usize>(Some( + (|a0: AnyPtr, a1: usize, a2: usize, a3: Ptr| -> usize { my_alternative_fread_0(a0.reinterpret_cast::(), a1, a2, a3.to_any()) - }) as fn(AnyPtr, usize, usize, Ptr<::std::fs::File>) -> usize, + }) as fn(AnyPtr, usize, usize, Ptr) -> usize, )), )); assert!( @@ -71,21 +63,13 @@ fn main_0() -> i32 { let mut __do_while = true; 'loop_: while __do_while || (0 != 0) { __do_while = false; - let stream: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal(b"rb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open(Ptr::from_string_literal(b"/dev/zero").to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open(Ptr::from_string_literal(b"/dev/zero").to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + let stream: Value> = Rc::new(RefCell::new( + match CFile::open( + &Ptr::from_string_literal(b"/dev/zero").to_rust_string(), + &Ptr::from_string_literal(b"rb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }, )); assert!(!((*stream.borrow()).is_null())); @@ -118,28 +102,21 @@ fn main_0() -> i32 { (*i.borrow_mut()).prefix_inc(); } { + let __r = (*stream.borrow()).with(|__f| __f.close()); (*stream.borrow()).delete(); - 0 + __r }; } let mut __do_while = true; 'loop_: while __do_while || (0 != 0) { __do_while = false; - let stream: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal(b"rb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open(Ptr::from_string_literal(b"/dev/zero").to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open(Ptr::from_string_literal(b"/dev/zero").to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + let stream: Value> = Rc::new(RefCell::new( + match CFile::open( + &Ptr::from_string_literal(b"/dev/zero").to_rust_string(), + &Ptr::from_string_literal(b"rb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }, )); assert!(!((*stream.borrow()).is_null())); @@ -175,72 +152,56 @@ fn main_0() -> i32 { (*i.borrow_mut()).prefix_inc(); } { + let __r = (*stream.borrow()).with(|__f| __f.close()); (*stream.borrow()).delete(); - 0 + __r }; } - let gn1: Value) -> usize>> = + let gn1: Value) -> usize>> = Rc::new(RefCell::new(FnPtr::< - fn(AnyPtr, usize, usize, Ptr<::std::fs::File>) -> usize, + fn(AnyPtr, usize, usize, Ptr) -> usize, >::new(libcc2rs::fwrite_refcount))); assert!({ let _lhs = (*gn1.borrow()).clone(); - _lhs == FnPtr::) -> usize>::new( + _lhs == FnPtr::) -> usize>::new( libcc2rs::fwrite_refcount, ) }); assert!(!((*gn1.borrow()).is_null())); let gn2: Value, usize, usize, AnyPtr) -> usize>> = Rc::new(RefCell::new( - FnPtr::) -> usize>::new( - libcc2rs::fwrite_refcount, - ) - .cast::, usize, usize, AnyPtr) -> usize>(Some( + FnPtr::) -> usize>::new(libcc2rs::fwrite_refcount) + .cast::, usize, usize, AnyPtr) -> usize>(Some( (|a0: Ptr, a1: usize, a2: usize, a3: AnyPtr| -> usize { - libcc2rs::fwrite_refcount( - a0.to_any(), - a1, - a2, - a3.reinterpret_cast::<::std::fs::File>(), - ) + libcc2rs::fwrite_refcount(a0.to_any(), a1, a2, a3.reinterpret_cast::()) }) as fn(Ptr, usize, usize, AnyPtr) -> usize, )), )); assert!({ let _lhs = (*gn1.borrow()).clone(); - _lhs == ((*gn2.borrow()) - .cast::) -> usize>(None)) - .clone() + _lhs == ((*gn2.borrow()).cast::) -> usize>(None)) + .clone() }); - let g3: Value) -> usize>> = - Rc::new(RefCell::new( - FnPtr::, usize, usize, AnyPtr) -> usize>::new(my_alternative_fwrite_1) - .cast::) -> usize>(Some( - (|a0: AnyPtr, a1: usize, a2: usize, a3: Ptr<::std::fs::File>| -> usize { - my_alternative_fwrite_1(a0.reinterpret_cast::(), a1, a2, a3.to_any()) - }) as fn(AnyPtr, usize, usize, Ptr<::std::fs::File>) -> usize, - )), - )); + let g3: Value) -> usize>> = Rc::new(RefCell::new( + FnPtr::, usize, usize, AnyPtr) -> usize>::new(my_alternative_fwrite_1) + .cast::) -> usize>(Some( + (|a0: AnyPtr, a1: usize, a2: usize, a3: Ptr| -> usize { + my_alternative_fwrite_1(a0.reinterpret_cast::(), a1, a2, a3.to_any()) + }) as fn(AnyPtr, usize, usize, Ptr) -> usize, + )), + )); assert!( (({ (*(*g3.borrow()))(AnyPtr::default(), 0_usize, 0_usize, Ptr::null(),) }) == 33_usize) ); let mut __do_while = true; 'loop_: while __do_while || (0 != 0) { __do_while = false; - let stream: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal(b"wb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open(Ptr::from_string_literal(b"/dev/null").to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open(Ptr::from_string_literal(b"/dev/null").to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + let stream: Value> = Rc::new(RefCell::new( + match CFile::open( + &Ptr::from_string_literal(b"/dev/null").to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }, )); assert!(!((*stream.borrow()).is_null())); @@ -263,28 +224,21 @@ fn main_0() -> i32 { })); assert!(((*n.borrow()) == 10_usize)); { + let __r = (*stream.borrow()).with(|__f| __f.close()); (*stream.borrow()).delete(); - 0 + __r }; } let mut __do_while = true; 'loop_: while __do_while || (0 != 0) { __do_while = false; - let stream: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal(b"wb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open(Ptr::from_string_literal(b"/dev/null").to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open(Ptr::from_string_literal(b"/dev/null").to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + let stream: Value> = Rc::new(RefCell::new( + match CFile::open( + &Ptr::from_string_literal(b"/dev/null").to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }, )); assert!(!((*stream.borrow()).is_null())); @@ -310,8 +264,9 @@ fn main_0() -> i32 { )); assert!(((*n.borrow()) == 10_usize)); { + let __r = (*stream.borrow()).with(|__f| __f.close()); (*stream.borrow()).delete(); - 0 + __r }; } return 0; diff --git a/tests/unit/out/refcount/fopen.rs b/tests/unit/out/refcount/fopen.rs index 1bf595ef..d8c30f4f 100644 --- a/tests/unit/out/refcount/fopen.rs +++ b/tests/unit/out/refcount/fopen.rs @@ -12,21 +12,14 @@ pub fn main() { fn main_0() -> i32 { let fname: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"testfile.txt"))); let mode: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"rb"))); - let file_ptr: Value> = - Rc::new(RefCell::new(match (*mode.borrow()).to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*fname.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*fname.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), - })); + let file_ptr: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*fname.borrow()).to_rust_string(), + &(*mode.borrow()).to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }, + )); return 0; } diff --git a/tests/unit/out/refcount/global_without_initializer.rs b/tests/unit/out/refcount/global_without_initializer.rs index f1dd9e2a..331bc5ef 100644 --- a/tests/unit/out/refcount/global_without_initializer.rs +++ b/tests/unit/out/refcount/global_without_initializer.rs @@ -35,7 +35,7 @@ thread_local!( pub static s_0: Value> = Rc::new(RefCell::new(Ptr::::null())); ); thread_local!( - pub static file_1: Value> = Rc::new(RefCell::new(Ptr::null())); + pub static file_1: Value> = Rc::new(RefCell::new(Ptr::null())); ); thread_local!( pub static size_2: Value = Rc::new(RefCell::new(0_usize)); diff --git a/tests/unit/out/refcount/goto_aggregate_default.rs b/tests/unit/out/refcount/goto_aggregate_default.rs index ff5b7fa2..c12724a6 100644 --- a/tests/unit/out/refcount/goto_aggregate_default.rs +++ b/tests/unit/out/refcount/goto_aggregate_default.rs @@ -51,7 +51,7 @@ pub fn agg_0(n: i32) -> i32 { let p: Value = >::default(); let ptr: Value> = Rc::new(RefCell::new(Ptr::::null())); let fp: Value i32>> = Rc::new(RefCell::new(FnPtr::null())); - let file: Value> = Rc::new(RefCell::new(Ptr::null())); + let file: Value> = Rc::new(RefCell::new(Ptr::null())); let total: Value = >::default(); goto_block!({ '__entry: { diff --git a/tests/unit/out/refcount/printfs.rs b/tests/unit/out/refcount/printfs.rs index c6148b19..704eb21d 100644 --- a/tests/unit/out/refcount/printfs.rs +++ b/tests/unit/out/refcount/printfs.rs @@ -26,7 +26,7 @@ fn main_0() -> i32 { println!("{}", Ptr::from_string_literal(b"fprintf stdout")); println!("{} {} {}", 1, 2_u32, 3_i64); print!("hello world"); - let in_: Value> = Rc::new(RefCell::new((libcc2rs::cin()).clone())); + let in_: Value> = Rc::new(RefCell::new((libcc2rs::c_stdin()).clone())); assert!(!((*in_.borrow()).is_null())); println!("{}", Ptr::from_string_literal(b"printf")); print!("hello world"); diff --git a/tests/unit/out/refcount/ptr_to_incomplete_struct.rs b/tests/unit/out/refcount/ptr_to_incomplete_struct.rs index 8d999eac..342a9937 100644 --- a/tests/unit/out/refcount/ptr_to_incomplete_struct.rs +++ b/tests/unit/out/refcount/ptr_to_incomplete_struct.rs @@ -10,11 +10,9 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let fp: Value> = Rc::new(RefCell::new((libcc2rs::cout()).clone())); + let fp: Value> = Rc::new(RefCell::new((libcc2rs::c_stdout()).clone())); let p: Value = Rc::new(RefCell::new((*fp.borrow()).clone().to_any())); - let fp2: Value> = Rc::new(RefCell::new( - (*p.borrow()).reinterpret_cast::<::std::fs::File>(), - )); + let fp2: Value> = Rc::new(RefCell::new((*p.borrow()).reinterpret_cast::())); assert!( ((({ let _lhs = (*fp.borrow()).clone(); diff --git a/tests/unit/out/refcount/stdio.rs b/tests/unit/out/refcount/stdio.rs new file mode 100644 index 00000000..896d239e --- /dev/null +++ b/tests/unit/out/refcount/stdio.rs @@ -0,0 +1,151 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn test_fputc_0() { + { + let __c = ('H' as i32) as u8; + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&[__c])) { + 1 => __c as i32, + _ => -1, + } + }; + { + let __c = ('i' as i32) as u8; + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&[__c])) { + 1 => __c as i32, + _ => -1, + } + }; + { + let __c = ('\n' as i32) as u8; + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&[__c])) { + 1 => __c as i32, + _ => -1, + } + }; +} +pub fn test_fputs_1() { + { + let __bytes: Vec = Ptr::from_string_literal(b"hello") + .to_c_string_iterator() + .collect(); + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, + } + }; + { + let __c = ('\n' as i32) as u8; + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&[__c])) { + 1 => __c as i32, + _ => -1, + } + }; + let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"from variable"))); + { + let __bytes: Vec = (*s.borrow()).to_c_string_iterator().collect(); + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, + } + }; + { + let __c = ('\n' as i32) as u8; + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&[__c])) { + 1 => __c as i32, + _ => -1, + } + }; + let buf: Value> = Rc::new(RefCell::new(Box::new([ + (('b' as i32) as u8), + (('u' as i32) as u8), + (('f' as i32) as u8), + (('\0' as i32) as u8), + ]))); + { + let __bytes: Vec = (buf.as_pointer() as Ptr) + .to_c_string_iterator() + .collect(); + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, + } + }; + { + let __c = ('\n' as i32) as u8; + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&[__c])) { + 1 => __c as i32, + _ => -1, + } + }; +} +pub fn test_puts_2() { + { + let mut __bytes: Vec = Ptr::from_string_literal(b"puts hello") + .to_c_string_iterator() + .collect(); + __bytes.push(b'\n'); + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, + } + }; + let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"puts variable"))); + { + let mut __bytes: Vec = (*s.borrow()).to_c_string_iterator().collect(); + __bytes.push(b'\n'); + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, + } + }; +} +pub fn test_fileno_3() { + assert!((((libcc2rs::c_stdin().with(|__f| __f.fd) == 0) as i32) != 0)); + assert!((((libcc2rs::c_stdout().with(|__f| __f.fd) == 1) as i32) != 0)); + assert!((((libcc2rs::c_stderr().with(|__f| __f.fd) == 2) as i32) != 0)); + let file: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_fileno_test.tmp", + ))); + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*file.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }, + )); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + assert!(((((*fp.borrow()).with(|__f| __f.fd) > 2) as i32) != 0)); + { + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + }; + assert!( + (((match nix::unistd::unlink((*file.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); +} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_fputc_0() }); + ({ test_fputs_1() }); + ({ test_puts_2() }); + ({ test_fileno_3() }); + return 0; +} diff --git a/tests/unit/out/refcount/stdio_nofd.rs b/tests/unit/out/refcount/stdio_nofd.rs index 0914e8a5..480f7d16 100644 --- a/tests/unit/out/refcount/stdio_nofd.rs +++ b/tests/unit/out/refcount/stdio_nofd.rs @@ -8,35 +8,24 @@ use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn test_fputc_fputs_0() { let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"cpp2rust_stdio_nofd_puts.tmp", + b"/tmp/cpp2rust_stdio_nofd_puts.tmp", ))); - let fp: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal(b"wb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }, )); assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); assert!( ((({ let __c = ('A' as i32) as u8; - match (*fp.borrow()).with_mut(|__f| ::std::io::Write::write_all(__f, &[__c])) { - Ok(()) => __c as i32, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } + match (*fp.borrow()).with_mut(|__f| __f.write(&[__c])) { + 1 => __c as i32, + _ => -1, } } == ('A' as i32)) as i32) != 0) @@ -46,37 +35,27 @@ pub fn test_fputc_fputs_0() { let __bytes: Vec = Ptr::from_string_literal(b"BCD\n") .to_c_string_iterator() .collect(); - match (*fp.borrow()).with_mut(|__f| ::std::io::Write::write_all(__f, &__bytes)) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } + match (*fp.borrow()).with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, } } >= 0) as i32) != 0) ); assert!( ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); (*fp.borrow()).delete(); - 0 + __r } == 0) as i32) != 0) ); - (*fp.borrow_mut()) = match Ptr::from_string_literal(b"rb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + (*fp.borrow_mut()) = match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"rb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }; assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); let buf: Value> = Rc::new(RefCell::new(Box::new([ @@ -116,8 +95,9 @@ pub fn test_fputc_fputs_0() { ); assert!( ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); (*fp.borrow()).delete(); - 0 + __r } == 0) as i32) != 0) ); @@ -135,18 +115,13 @@ pub fn test_fputc_fputs_0() { pub fn test_puts_1() { assert!( ((({ - let __bytes: Vec = Ptr::from_string_literal(b"hello from puts") + let mut __bytes: Vec = Ptr::from_string_literal(b"hello from puts") .to_c_string_iterator() .collect(); - match libcc2rs::cout().with_mut(|__f| { - ::std::io::Write::write_all(__f, &__bytes) - .and_then(|_| ::std::io::Write::write_all(__f, b"\n")) - }) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } + __bytes.push(b'\n'); + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, } } >= 0) as i32) != 0) @@ -154,23 +129,15 @@ pub fn test_puts_1() { } pub fn test_fgets_getc_2() { let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"cpp2rust_stdio_nofd_gets.tmp", + b"/tmp/cpp2rust_stdio_nofd_gets.tmp", ))); - let fp: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal(b"wb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }, )); assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); @@ -179,37 +146,27 @@ pub fn test_fgets_getc_2() { let __bytes: Vec = Ptr::from_string_literal(b"line1\nline2\n") .to_c_string_iterator() .collect(); - match (*fp.borrow()).with_mut(|__f| ::std::io::Write::write_all(__f, &__bytes)) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } + match (*fp.borrow()).with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, } } >= 0) as i32) != 0) ); assert!( ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); (*fp.borrow()).delete(); - 0 + __r } == 0) as i32) != 0) ); - (*fp.borrow_mut()) = match Ptr::from_string_literal(b"rb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + (*fp.borrow_mut()) = match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"rb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }; assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); let buf: Value> = Rc::new(RefCell::new( @@ -219,36 +176,27 @@ pub fn test_fgets_getc_2() { (((!(({ let __buf = (buf.as_pointer() as Ptr).clone(); let __n = 8; - let __stream = (*fp.borrow()).clone(); if __n <= 0 { Ptr::null() } else { let __max = (__n - 1) as usize; let mut __dst = __buf.clone(); let mut __count: usize = 0; - let mut __failed = false; - while __count < __max { - let mut __b: [u8; 1] = [0]; - match __stream.with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { - Ok(0) => break, - Ok(_) => { - __dst.write(__b[0]); - __dst += 1; - __count += 1; - if __b[0] == b'\n' { - break; - } + let __failed = (*fp.borrow()).with_mut(|__f| { + while __count < __max { + let __c = __f.getc(); + if __c < 0 { + break; } - Err(__e) => { - if __e.kind() != ::std::io::ErrorKind::Interrupted { - libcc2rs::cpp2rust_errno() - .write(__e.raw_os_error().unwrap_or(::libc::EIO)); - __failed = true; - break; - } + __dst.write(__c as u8); + __dst += 1; + __count += 1; + if __c as u8 == b'\n' { + break; } } - } + __f.err + }); if __failed || __count == 0 { Ptr::null() } else { @@ -267,54 +215,32 @@ pub fn test_fgets_getc_2() { == 0) as i32) != 0) ); - assert!( - ((({ - let mut __b: [u8; 1] = [0]; - match (*fp.borrow()).with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { - Ok(0) => -1, - Ok(_) => __b[0] as i32, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } - } - } == ('l' as i32)) as i32) - != 0) - ); + assert!(((((*fp.borrow()).with_mut(|__f| __f.getc()) == ('l' as i32)) as i32) != 0)); assert!( (((!(({ let __buf = (buf.as_pointer() as Ptr).clone(); let __n = 4; - let __stream = (*fp.borrow()).clone(); if __n <= 0 { Ptr::null() } else { let __max = (__n - 1) as usize; let mut __dst = __buf.clone(); let mut __count: usize = 0; - let mut __failed = false; - while __count < __max { - let mut __b: [u8; 1] = [0]; - match __stream.with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { - Ok(0) => break, - Ok(_) => { - __dst.write(__b[0]); - __dst += 1; - __count += 1; - if __b[0] == b'\n' { - break; - } + let __failed = (*fp.borrow()).with_mut(|__f| { + while __count < __max { + let __c = __f.getc(); + if __c < 0 { + break; } - Err(__e) => { - if __e.kind() != ::std::io::ErrorKind::Interrupted { - libcc2rs::cpp2rust_errno() - .write(__e.raw_os_error().unwrap_or(::libc::EIO)); - __failed = true; - break; - } + __dst.write(__c as u8); + __dst += 1; + __count += 1; + if __c as u8 == b'\n' { + break; } } - } + __f.err + }); if __failed || __count == 0 { Ptr::null() } else { @@ -337,36 +263,27 @@ pub fn test_fgets_getc_2() { (((!(({ let __buf = (buf.as_pointer() as Ptr).clone(); let __n = 8; - let __stream = (*fp.borrow()).clone(); if __n <= 0 { Ptr::null() } else { let __max = (__n - 1) as usize; let mut __dst = __buf.clone(); let mut __count: usize = 0; - let mut __failed = false; - while __count < __max { - let mut __b: [u8; 1] = [0]; - match __stream.with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { - Ok(0) => break, - Ok(_) => { - __dst.write(__b[0]); - __dst += 1; - __count += 1; - if __b[0] == b'\n' { - break; - } + let __failed = (*fp.borrow()).with_mut(|__f| { + while __count < __max { + let __c = __f.getc(); + if __c < 0 { + break; } - Err(__e) => { - if __e.kind() != ::std::io::ErrorKind::Interrupted { - libcc2rs::cpp2rust_errno() - .write(__e.raw_os_error().unwrap_or(::libc::EIO)); - __failed = true; - break; - } + __dst.write(__c as u8); + __dst += 1; + __count += 1; + if __c as u8 == b'\n' { + break; } } - } + __f.err + }); if __failed || __count == 0 { Ptr::null() } else { @@ -389,36 +306,27 @@ pub fn test_fgets_getc_2() { (((({ let __buf = (buf.as_pointer() as Ptr).clone(); let __n = 8; - let __stream = (*fp.borrow()).clone(); if __n <= 0 { Ptr::null() } else { let __max = (__n - 1) as usize; let mut __dst = __buf.clone(); let mut __count: usize = 0; - let mut __failed = false; - while __count < __max { - let mut __b: [u8; 1] = [0]; - match __stream.with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { - Ok(0) => break, - Ok(_) => { - __dst.write(__b[0]); - __dst += 1; - __count += 1; - if __b[0] == b'\n' { - break; - } + let __failed = (*fp.borrow()).with_mut(|__f| { + while __count < __max { + let __c = __f.getc(); + if __c < 0 { + break; } - Err(__e) => { - if __e.kind() != ::std::io::ErrorKind::Interrupted { - libcc2rs::cpp2rust_errno() - .write(__e.raw_os_error().unwrap_or(::libc::EIO)); - __failed = true; - break; - } + __dst.write(__c as u8); + __dst += 1; + __count += 1; + if __c as u8 == b'\n' { + break; } } - } + __f.err + }); if __failed || __count == 0 { Ptr::null() } else { @@ -430,24 +338,12 @@ pub fn test_fgets_getc_2() { .is_null()) as i32) != 0) ); + assert!(((((*fp.borrow()).with_mut(|__f| __f.getc()) == (-1_i32)) as i32) != 0)); assert!( ((({ - let mut __b: [u8; 1] = [0]; - match (*fp.borrow()).with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { - Ok(0) => -1, - Ok(_) => __b[0] as i32, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } - } - } == (-1_i32)) as i32) - != 0) - ); - assert!( - ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); (*fp.borrow()).delete(); - 0 + __r } == 0) as i32) != 0) ); @@ -464,23 +360,15 @@ pub fn test_fgets_getc_2() { } pub fn test_freopen_3() { let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"cpp2rust_stdio_nofd_reopen.tmp", + b"/tmp/cpp2rust_stdio_nofd_reopen.tmp", ))); - let fp: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal(b"wb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }, )); assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); @@ -489,38 +377,31 @@ pub fn test_freopen_3() { let __bytes: Vec = Ptr::from_string_literal(b"hello") .to_c_string_iterator() .collect(); - match (*fp.borrow()).with_mut(|__f| ::std::io::Write::write_all(__f, &__bytes)) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } + match (*fp.borrow()).with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, } } >= 0) as i32) != 0) ); - let fp2: Value> = Rc::new(RefCell::new({ + let fp2: Value> = Rc::new(RefCell::new({ let __stream = (*fp.borrow()).clone(); - let __new = match Ptr::from_string_literal(b"rb").to_rust_string().as_str() { - "rb" => ::std::fs::OpenOptions::new() - .read(true) - .open((*path.borrow()).to_rust_string()), - "wb" => ::std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*path.borrow()).to_rust_string()), - _ => panic!("unsupported mode"), - }; - match __new { - Ok(__f) => { + let __old = __stream.with(|__f| __f.fd); + match __old { + 0..=2 => {} + __fd => { + FdRegistry::close(__fd); + } + } + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"rb").to_rust_string(), + ) { + Some(__f) => { __stream.write(__f); __stream } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - Ptr::null() - } + None => Ptr::null(), } })); assert!((((!((*fp2.borrow()).is_null())) as i32) != 0)); @@ -553,8 +434,9 @@ pub fn test_freopen_3() { ); assert!( ((({ + let __r = (*fp2.borrow()).with(|__f| __f.close()); (*fp2.borrow()).delete(); - 0 + __r } == 0) as i32) != 0) ); @@ -571,23 +453,15 @@ pub fn test_freopen_3() { } pub fn test_fseeko_4() { let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"cpp2rust_stdio_nofd_seek.tmp", + b"/tmp/cpp2rust_stdio_nofd_seek.tmp", ))); - let fp: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal(b"wb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }, )); assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); @@ -596,54 +470,33 @@ pub fn test_fseeko_4() { let __bytes: Vec = Ptr::from_string_literal(b"hello world") .to_c_string_iterator() .collect(); - match (*fp.borrow()).with_mut(|__f| ::std::io::Write::write_all(__f, &__bytes)) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } + match (*fp.borrow()).with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, } } >= 0) as i32) != 0) ); assert!( ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); (*fp.borrow()).delete(); - 0 + __r } == 0) as i32) != 0) ); - (*fp.borrow_mut()) = match Ptr::from_string_literal(b"rb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + (*fp.borrow_mut()) = match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"rb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }; assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); assert!( - ((({ - let __r = (*fp.borrow()).with_mut(|__f| match 0 { - 0 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Start(6_i64 as u64)), - 1 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Current(6_i64)), - 2 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::End(6_i64)), - _ => Err(::std::io::Error::other("unsupported whence for fseeko.")), - }); - match __r { - Ok(_) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EINVAL)); - -1 - } - } + (((match (*fp.borrow()).with_mut(|__f| __f.seek(6_i64, 0)) { + -1 => -1, + _ => 0, } == 0) as i32) != 0) ); @@ -675,73 +528,26 @@ pub fn test_fseeko_4() { != 0) ); assert!( - ((({ - let __r = (*fp.borrow()).with_mut(|__f| match 2 { - 0 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Start((-5_i32 as i64) as u64)), - 1 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Current((-5_i32 as i64))), - 2 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::End((-5_i32 as i64))), - _ => Err(::std::io::Error::other("unsupported whence for fseeko.")), - }); - match __r { - Ok(_) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EINVAL)); - -1 - } - } + (((match (*fp.borrow()).with_mut(|__f| __f.seek((-5_i32 as i64), 2)) { + -1 => -1, + _ => 0, } == 0) as i32) != 0) ); + assert!(((((*fp.borrow()).with_mut(|__f| __f.getc()) == ('w' as i32)) as i32) != 0)); assert!( - ((({ - let mut __b: [u8; 1] = [0]; - match (*fp.borrow()).with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { - Ok(0) => -1, - Ok(_) => __b[0] as i32, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } - } - } == ('w' as i32)) as i32) - != 0) - ); - assert!( - ((({ - let __r = (*fp.borrow()).with_mut(|__f| match 1 { - 0 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Start(1_i64 as u64)), - 1 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Current(1_i64)), - 2 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::End(1_i64)), - _ => Err(::std::io::Error::other("unsupported whence for fseeko.")), - }); - match __r { - Ok(_) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EINVAL)); - -1 - } - } + (((match (*fp.borrow()).with_mut(|__f| __f.seek(1_i64, 1)) { + -1 => -1, + _ => 0, } == 0) as i32) != 0) ); + assert!(((((*fp.borrow()).with_mut(|__f| __f.getc()) == ('r' as i32)) as i32) != 0)); assert!( ((({ - let mut __b: [u8; 1] = [0]; - match (*fp.borrow()).with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { - Ok(0) => -1, - Ok(_) => __b[0] as i32, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } - } - } == ('r' as i32)) as i32) - != 0) - ); - assert!( - ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); (*fp.borrow()).delete(); - 0 + __r } == 0) as i32) != 0) ); @@ -758,26 +564,18 @@ pub fn test_fseeko_4() { } pub fn test_rename_5() { let from: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"cpp2rust_stdio_nofd_from.tmp", + b"/tmp/cpp2rust_stdio_nofd_from.tmp", ))); let to: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"cpp2rust_stdio_nofd_to.tmp", + b"/tmp/cpp2rust_stdio_nofd_to.tmp", ))); - let fp: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal(b"wb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*from.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*from.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*from.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }, )); assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); @@ -786,20 +584,18 @@ pub fn test_rename_5() { let __bytes: Vec = Ptr::from_string_literal(b"data") .to_c_string_iterator() .collect(); - match (*fp.borrow()).with_mut(|__f| ::std::io::Write::write_all(__f, &__bytes)) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } + match (*fp.borrow()).with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, } } >= 0) as i32) != 0) ); assert!( ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); (*fp.borrow()).delete(); - 0 + __r } == 0) as i32) != 0) ); @@ -817,44 +613,29 @@ pub fn test_rename_5() { != 0) ); assert!( - ((((match Ptr::from_string_literal(b"rb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*from.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*from.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + ((((match CFile::open( + &(*from.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"rb").to_rust_string() + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }) .is_null()) as i32) != 0) ); - (*fp.borrow_mut()) = match Ptr::from_string_literal(b"rb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*to.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*to.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + (*fp.borrow_mut()) = match CFile::open( + &(*to.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"rb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }; assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); assert!( ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); (*fp.borrow()).delete(); - 0 + __r } == 0) as i32) != 0) ); @@ -884,46 +665,36 @@ pub fn test_rename_5() { } pub fn test_setvbuf_6() { let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"cpp2rust_stdio_nofd_vbuf.tmp", + b"/tmp/cpp2rust_stdio_nofd_vbuf.tmp", ))); - let fp: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal(b"wb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }, )); assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); - assert!((((/* std::fs::File is unbuffered */0 == 0) as i32) != 0)); + assert!((((0 == 0) as i32) != 0)); assert!( ((({ let __bytes: Vec = Ptr::from_string_literal(b"x") .to_c_string_iterator() .collect(); - match (*fp.borrow()).with_mut(|__f| ::std::io::Write::write_all(__f, &__bytes)) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } + match (*fp.borrow()).with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, } } >= 0) as i32) != 0) ); assert!( ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); (*fp.borrow()).delete(); - 0 + __r } == 0) as i32) != 0) ); diff --git a/tests/unit/out/refcount/sys_stat.rs b/tests/unit/out/refcount/sys_stat.rs new file mode 100644 index 00000000..6446ae31 --- /dev/null +++ b/tests/unit/out/refcount/sys_stat.rs @@ -0,0 +1,128 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn test_stat_0() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_stat_test.tmp", + ))); + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }, + )); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + { + let __bytes: Vec = Ptr::from_string_literal(b"hello") + .to_c_string_iterator() + .collect(); + match (*fp.borrow()).with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, + } + }; + assert!( + ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + } == 0) as i32) + != 0) + ); + let st: Value = Rc::new(RefCell::new(Default::default())); + assert!( + (((match nix::sys::stat::stat((*path.borrow()).to_rust_string().as_str()) { + 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_mtime.borrow()) > 0_i64) as i32) != 0)); + match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + }; +} +pub fn test_fstat_1() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_fstat_test.tmp", + ))); + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }, + )); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + { + let __bytes: Vec = Ptr::from_string_literal(b"hello world") + .to_c_string_iterator() + .collect(); + match (*fp.borrow()).with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, + } + }; + 0; + let fd: Value = Rc::new(RefCell::new((*fp.borrow()).with(|__f| __f.fd))); + 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()) == 11_i64) as i32) != 0)); + assert!(((((*(*st.borrow()).st_mtime.borrow()) > 0_i64) as i32) != 0)); + assert!( + ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + } == 0) as i32) + != 0) + ); + match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + }; +} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_stat_0() }); + ({ test_fstat_1() }); + return 0; +} diff --git a/tests/unit/out/refcount/unistd.rs b/tests/unit/out/refcount/unistd.rs new file mode 100644 index 00000000..90593761 --- /dev/null +++ b/tests/unit/out/refcount/unistd.rs @@ -0,0 +1,673 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn test_close_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!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); + let buf: Value> = Rc::new(RefCell::new( + (0..1).map(|_| ::default()).collect::>(), + )); + 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(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!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); +} +pub fn test_lseek_1() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_lseek_test.tmp", + ))); + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }, + )); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + { + let __bytes: Vec = Ptr::from_string_literal(b"hello world") + .to_c_string_iterator() + .collect(); + match (*fp.borrow()).with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, + } + }; + assert!( + ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + } == 0) as i32) + != 0) + ); + (*fp.borrow_mut()) = match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"rb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }; + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + let fd: Value = Rc::new(RefCell::new((*fp.borrow()).with(|__f| __f.fd))); + 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(Box::new([ + 0_u8, + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ]))); + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(5_usize, |__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!( + (((((buf.as_pointer() as Ptr::) as Ptr::) + .to_any() + .memcmp(&Ptr::from_string_literal(b"world").to_any(), 5_usize) + == 0) as i32) + != 0) + ); + assert!( + ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + } == 0) as i32) + != 0) + ); + match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + }; +} +pub fn test_read_2() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_read_test.tmp", + ))); + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }, + )); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + { + let __bytes: Vec = Ptr::from_string_literal(b"hello world") + .to_c_string_iterator() + .collect(); + match (*fp.borrow()).with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, + } + }; + assert!( + ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + } == 0) as i32) + != 0) + ); + (*fp.borrow_mut()) = match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"rb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }; + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + let fd: Value = Rc::new(RefCell::new((*fp.borrow()).with(|__f| __f.fd))); + let buf: Value> = Rc::new(RefCell::new(Box::new([ + 0_u8, + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ]))); + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(16_usize, |__buf| nix::unistd::read(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 11_isize) as i32) + != 0) + ); + assert!( + (((((buf.as_pointer() as Ptr::) as Ptr::) + .to_any() + .memcmp(&Ptr::from_string_literal(b"hello world").to_any(), 11_usize) + == 0) as i32) + != 0) + ); + assert!( + ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + } == 0) as i32) + != 0) + ); + match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + }; +} +pub fn test_unlink_3() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_unlink_test.tmp", + ))); + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }, + )); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + assert!( + ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + } == 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) + ); + assert!( + (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == -1_i32) as i32) + != 0) + ); +} +pub fn test_pipe_4() { + 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 msg: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"world"))); + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(1) as usize], |__fd| { + ((*msg.borrow()).clone() as Ptr) + .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 buf: Value> = Rc::new(RefCell::new(Box::new([ + 0_u8, + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ]))); + 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(8_usize, |__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!( + (((((buf.as_pointer() as Ptr::) as Ptr::) + .to_any() + .memcmp(&((*msg.borrow()).clone() as Ptr::).to_any(), 5_usize) + == 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(8_usize, |__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_ftruncate_5() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_ftruncate_test.tmp", + ))); + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }, + )); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + { + let __bytes: Vec = Ptr::from_string_literal(b"hello world") + .to_c_string_iterator() + .collect(); + match (*fp.borrow()).with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, + } + }; + 0; + let fd: Value = Rc::new(RefCell::new((*fp.borrow()).with(|__f| __f.fd))); + 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 __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + } == 0) as i32) + != 0) + ); + (*fp.borrow_mut()) = match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"rb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }; + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + (*fd.borrow_mut()) = (*fp.borrow()).with(|__f| __f.fd); + 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!( + ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + } == 0) as i32) + != 0) + ); + match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + }; +} +pub fn test_open_6() { + 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( + Ptr::from_string_literal(b"/dev/null") + .to_rust_string() + .as_str(), + nix::fcntl::OFlag::from_bits_retain(0), + __mode, + ) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*fd.borrow()) >= -1_i32) as i32) != 0)); + if ((((*fd.borrow()) >= 0) as i32) != 0) { + FdRegistry::close((*fd.borrow())); + } + (*fd.borrow_mut()) = { + let __mode = match &[].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( + Ptr::from_string_literal(b"/dev/null") + .to_rust_string() + .as_str(), + nix::fcntl::OFlag::from_bits_retain(0), + __mode, + ) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + }; + assert!(((((*fd.borrow()) >= -1_i32) as i32) != 0)); + if ((((*fd.borrow()) >= 0) as i32) != 0) { + FdRegistry::close((*fd.borrow())); + } +} +pub fn test_fcntl_7() { + assert!( + ((({ + let __res = match 1 { + ::libc::F_GETFL => FdRegistry::with_fd(0, |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[][0])); + FdRegistry::with_fd(0, |__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])); + FdRegistry::with_fd(0, |__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 + } + } + } >= -1_i32) as i32) + != 0) + ); + let duped: Value = Rc::new(RefCell::new({ + let __res = match 0 { + ::libc::F_GETFL => FdRegistry::with_fd(0, |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(100).into()][0])); + FdRegistry::with_fd(0, |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) + }) + } + ::libc::F_SETFD => { + let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(100).into()][0])); + FdRegistry::with_fd(0, |__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!(((((*duped.borrow()) >= -1_i32) as i32) != 0)); + if ((((*duped.borrow()) >= 0) as i32) != 0) { + FdRegistry::close((*duped.borrow())); + } +} +pub fn test_ioctl_8() { + let arg: Value = Rc::new(RefCell::new(0)); + assert!( + ((({ + panic!( + "ioctl is not supported in the refcount model (fd={}, request={})", + 0, 0_u64 + ); + 0 + } >= -1_i32) as i32) + != 0) + ); +} +pub fn test_isatty_9() { + println!( + "{}", + match FdRegistry::with_fd(0, |__fd| nix::unistd::isatty(__fd)) { + Ok(__tty) => __tty as i32, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + 0 + } + } + ); +} +pub fn test_geteuid_10() { + println!("{}", nix::unistd::geteuid().as_raw()); +} +pub fn test_gethostname_11() { + let name: Value> = Rc::new(RefCell::new( + (0..256).map(|_| ::default()).collect::>(), + )); + assert!( + (((match nix::unistd::gethostname() { + Ok(__name) => { + let __bytes = __name.as_encoded_bytes(); + let __n = __bytes + .len() + .min(::std::mem::size_of::<[u8; 256]>().saturating_sub(1)); + if ::std::mem::size_of::<[u8; 256]>() > 0 { + (name.as_pointer() as Ptr).with_slice_mut(__n + 1, |__s| { + __s[..__n].copy_from_slice(&__bytes[..__n]); + __s[__n] = 0; + }); + } + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + println!("{}", (name.as_pointer() as Ptr::)); +} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_close_0() }); + ({ test_lseek_1() }); + ({ test_read_2() }); + ({ test_unlink_3() }); + ({ test_pipe_4() }); + ({ test_ftruncate_5() }); + ({ test_open_6() }); + ({ test_fcntl_7() }); + ({ test_ioctl_8() }); + ({ test_isatty_9() }); + ({ test_geteuid_10() }); + ({ test_gethostname_11() }); + return 0; +} diff --git a/tests/unit/out/refcount/user_defined_same_as_libc.rs b/tests/unit/out/refcount/user_defined_same_as_libc.rs index f52ec539..56abef01 100644 --- a/tests/unit/out/refcount/user_defined_same_as_libc.rs +++ b/tests/unit/out/refcount/user_defined_same_as_libc.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -pub fn fopen_0(path: Ptr, mode: Ptr) -> Ptr<::std::fs::File> { +pub fn fopen_0(path: Ptr, mode: Ptr) -> Ptr { let path: Value> = Rc::new(RefCell::new(path)); let mode: Value> = Rc::new(RefCell::new(mode)); (*path.borrow()).clone(); @@ -17,7 +17,7 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let fp: Value> = Rc::new(RefCell::new( + let fp: Value> = Rc::new(RefCell::new( ({ fopen_0( Ptr::from_string_literal(b"/tmp/irrelevant-file"), diff --git a/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs b/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs index 7813aa0d..beb9a941 100644 --- a/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs +++ b/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs @@ -79,8 +79,8 @@ pub fn dispatch_0(option: i32, __args: &[VaArg]) -> i32 { break 'switch; } __v if __v == (opt::OPT_FILE as i32) => { - let f: Value> = Rc::new(RefCell::new( - ((*ap.borrow_mut()).arg::>()).clone(), + let f: Value> = Rc::new(RefCell::new( + ((*ap.borrow_mut()).arg::>()).clone(), )); (*result.borrow_mut()) = ((!((*f.borrow()).is_null())) as i32).clone(); break 'switch; @@ -120,7 +120,7 @@ fn main_0() -> i32 { (((({ dispatch_0( (opt::OPT_FILE as i32), - &[((libcc2rs::cout()).clone()).into()], + &[((libcc2rs::c_stdout()).clone()).into()], ) }) == 1) as i32) != 0) @@ -129,7 +129,7 @@ fn main_0() -> i32 { (((({ dispatch_0( (opt::OPT_FILE as i32), - &[((AnyPtr::default()).reinterpret_cast::<::std::fs::File>()).into()], + &[((AnyPtr::default()).reinterpret_cast::()).into()], ) }) == 0) as i32) != 0) diff --git a/tests/unit/out/unsafe/fd_io.rs b/tests/unit/out/unsafe/fd_io.rs index e629e621..d8e02547 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,493 @@ 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 unsafe fn test_fileno_12() { + let mut path: *const libc::c_char = + (c"/tmp/cpp2rust_fd_io_fileno.tmp".as_ptr().cast_mut()).cast_const(); + let mut fp: *mut ::libc::FILE = libc::fopen(path, (c"wb".as_ptr().cast_mut()).cast_const()); + assert!((((!((fp).is_null())) as i32) != 0)); + assert!( + ((((libcc2rs::fwrite_unsafe( + (c"hello".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), + 1_usize, + 5_usize, + fp + )) == (5_usize)) as i32) + != 0) + ); + assert!(((((libc::fflush(fp)) == (0)) as i32) != 0)); + let mut fd: i32 = libc::fileno(fp); + assert!(((((fd) >= (0)) as i32) != 0)); + assert!(((((libc::fileno(fp)) == (fd)) 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!(((((libc::fclose(fp)) == (0)) as i32) != 0)); + assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); +} +pub unsafe fn test_fdopen_13() { + let mut path: *const libc::c_char = + (c"/tmp/cpp2rust_fd_io_fdopen.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)); + let mut fp: *mut ::libc::FILE = libc::fdopen(fd, (c"wb".as_ptr().cast_mut()).cast_const()); + assert!((((!((fp).is_null())) as i32) != 0)); + assert!( + ((((libcc2rs::fwrite_unsafe( + (c"hi".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), + 1_usize, + 2_usize, + fp + )) == (2_usize)) as i32) + != 0) + ); + assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); + fd = (unsafe { libc::open(path as *const i8, ::libc::O_RDONLY as i32) }); + assert!(((((fd) >= (0)) 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( + fd, + (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"hi".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_feof_ferror_14() { + let mut path: *const libc::c_char = + (c"/tmp/cpp2rust_fd_io_eof.tmp".as_ptr().cast_mut()).cast_const(); + let mut fp: *mut ::libc::FILE = libc::fopen(path, (c"wb".as_ptr().cast_mut()).cast_const()); + assert!((((!((fp).is_null())) as i32) != 0)); + assert!( + ((((libcc2rs::fwrite_unsafe( + (c"ab".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), + 1_usize, + 2_usize, + fp + )) == (2_usize)) as i32) + != 0) + ); + assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); + fp = libc::fopen(path, (c"rb".as_ptr().cast_mut()).cast_const()); + assert!((((!((fp).is_null())) as i32) != 0)); + assert!(((!(libc::feof(fp) != 0) as i32) != 0)); + assert!(((!(libc::ferror(fp) != 0) as i32) != 0)); + let mut buf: [libc::c_char; 2] = [(0 as libc::c_char); 2]; + assert!( + ((((libcc2rs::fread_unsafe( + (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), + 1_usize, + 2_usize, + fp + )) == (2_usize)) as i32) + != 0) + ); + assert!(((!(libc::feof(fp) != 0) as i32) != 0)); + assert!( + ((((libcc2rs::fread_unsafe( + (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), + 1_usize, + 1_usize, + fp + )) == (0_usize)) as i32) + != 0) + ); + assert!((libc::feof(fp) != 0)); + assert!(((!(libc::ferror(fp) != 0) as i32) != 0)); + assert!(((((libc::fseek(fp, 0_i64 as ::libc::c_long, 0)) == (0)) as i32) != 0)); + assert!(((!(libc::feof(fp) != 0) as i32) != 0)); + assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); + assert!(((((libc::unlink(path)) == (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() }); + (unsafe { test_fileno_12() }); + (unsafe { test_fdopen_13() }); + (unsafe { test_feof_ferror_14() }); return 0; } diff --git a/tests/unit/stdio.c b/tests/unit/stdio.c index 89a382df..8936431c 100644 --- a/tests/unit/stdio.c +++ b/tests/unit/stdio.c @@ -1,4 +1,3 @@ -// no-compile: refcount #include #include #include diff --git a/tests/unit/sys_stat.c b/tests/unit/sys_stat.c index d1261c4b..433665ee 100644 --- a/tests/unit/sys_stat.c +++ b/tests/unit/sys_stat.c @@ -1,4 +1,3 @@ -// no-compile: refcount #include #include #include diff --git a/tests/unit/unistd.c b/tests/unit/unistd.c index 39b4c8c0..2bb439d3 100644 --- a/tests/unit/unistd.c +++ b/tests/unit/unistd.c @@ -1,4 +1,4 @@ -// no-compile: refcount +// panic-ub: refcount #include #include #include From 7de0cfe2865704b3735d9d9cac5f701326055a40 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 23 Jul 2026 16:19:46 +0100 Subject: [PATCH 2/7] Update tests --- tests/unit/out/refcount/fd_io.rs | 1819 +-------------------------- tests/unit/out/refcount/sys_stat.rs | 14 +- tests/unit/out/refcount/unistd.rs | 79 +- tests/unit/out/unsafe/fd_io.rs | 497 +------- tests/unit/sys_stat.c | 1 + tests/unit/unistd.c | 2 +- 6 files changed, 33 insertions(+), 2379 deletions(-) diff --git a/tests/unit/out/refcount/fd_io.rs b/tests/unit/out/refcount/fd_io.rs index e63aa312..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,1817 +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 test_fileno_12() { - let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_fd_io_fileno.tmp", - ))); - let fp: Value> = Rc::new(RefCell::new( - match CFile::open( - &(*path.borrow()).to_rust_string(), - &Ptr::from_string_literal(b"wb").to_rust_string(), - ) { - Some(__f) => Ptr::alloc(__f), - None => Ptr::null(), - }, - )); - assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); - assert!( - ((({ - let __a0 = Ptr::from_string_literal(b"hello").to_any(); - let __a1 = 1_usize; - let __a2 = 5_usize; - let __a3 = (*fp.borrow()).clone(); - libcc2rs::fwrite_refcount(__a0, __a1, __a2, __a3) - } == 5_usize) as i32) - != 0) - ); - assert!((((0 == 0) as i32) != 0)); - let fd: Value = Rc::new(RefCell::new((*fp.borrow()).with(|__f| __f.fd))); - assert!(((((*fd.borrow()) >= 0) as i32) != 0)); - assert!( - ((({ - let _lhs = (*fp.borrow()).with(|__f| __f.fd); - _lhs == (*fd.borrow()) - }) 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!( - ((({ - let __r = (*fp.borrow()).with(|__f| __f.close()); - (*fp.borrow()).delete(); - __r - } == 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_fdopen_13() { - let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_fd_io_fdopen.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)); - let fp: Value> = Rc::new(RefCell::new(Ptr::alloc(CFile::new((*fd.borrow()))))); - assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); - assert!( - ((({ - let __a0 = Ptr::from_string_literal(b"hi").to_any(); - let __a1 = 1_usize; - let __a2 = 2_usize; - let __a3 = (*fp.borrow()).clone(); - libcc2rs::fwrite_refcount(__a0, __a1, __a2, __a3) - } == 2_usize) as i32) - != 0) - ); - assert!( - ((({ - let __r = (*fp.borrow()).with(|__f| __f.close()); - (*fp.borrow()).delete(); - __r - } == 0) as i32) - != 0) - ); - (*fd.borrow_mut()) = { - let __mode = match &[].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), - __mode, - ) { - Ok(__ofd) => FdRegistry::register(__ofd), - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - }; - assert!(((((*fd.borrow()) >= 0) 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((*fd.borrow()), |__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"hi").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_feof_ferror_14() { - let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_fd_io_eof.tmp", - ))); - let fp: Value> = Rc::new(RefCell::new( - match CFile::open( - &(*path.borrow()).to_rust_string(), - &Ptr::from_string_literal(b"wb").to_rust_string(), - ) { - Some(__f) => Ptr::alloc(__f), - None => Ptr::null(), - }, - )); - assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); - assert!( - ((({ - let __a0 = Ptr::from_string_literal(b"ab").to_any(); - let __a1 = 1_usize; - let __a2 = 2_usize; - let __a3 = (*fp.borrow()).clone(); - libcc2rs::fwrite_refcount(__a0, __a1, __a2, __a3) - } == 2_usize) as i32) - != 0) - ); - assert!( - ((({ - let __r = (*fp.borrow()).with(|__f| __f.close()); - (*fp.borrow()).delete(); - __r - } == 0) as i32) - != 0) - ); - (*fp.borrow_mut()) = match CFile::open( - &(*path.borrow()).to_rust_string(), - &Ptr::from_string_literal(b"rb").to_rust_string(), - ) { - Some(__f) => Ptr::alloc(__f), - None => Ptr::null(), - }; - assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); - assert!(((!((*fp.borrow()).with(|__f| __f.eof) as i32 != 0) as i32) != 0)); - assert!(((!((*fp.borrow()).with(|__f| __f.err) as i32 != 0) as i32) != 0)); - let buf: Value> = Rc::new(RefCell::new( - (0..2).map(|_| ::default()).collect::>(), - )); - assert!( - ((({ - let __a0 = ((buf.as_pointer() as Ptr) as Ptr).to_any(); - let __a1 = 1_usize; - let __a2 = 2_usize; - let __a3 = (*fp.borrow()).clone(); - libcc2rs::fread_refcount(__a0, __a1, __a2, __a3) - } == 2_usize) as i32) - != 0) - ); - assert!(((!((*fp.borrow()).with(|__f| __f.eof) as i32 != 0) as i32) != 0)); - assert!( - ((({ - let __a0 = ((buf.as_pointer() as Ptr) as Ptr).to_any(); - let __a1 = 1_usize; - let __a2 = 1_usize; - let __a3 = (*fp.borrow()).clone(); - libcc2rs::fread_refcount(__a0, __a1, __a2, __a3) - } == 0_usize) as i32) - != 0) - ); - assert!(((*fp.borrow()).with(|__f| __f.eof) as i32 != 0)); - assert!(((!((*fp.borrow()).with(|__f| __f.err) as i32 != 0) as i32) != 0)); - assert!( - (((match (*fp.borrow()).with_mut(|__v: &mut CFile| __v.seek(0_i64, 0)) { - -1 => -1, - _ => 0, - } == 0) as i32) - != 0) - ); - assert!(((!((*fp.borrow()).with(|__f| __f.eof) as i32 != 0) as i32) != 0)); - assert!( - ((({ - let __r = (*fp.borrow()).with(|__f| __f.close()); - (*fp.borrow()).delete(); - __r - } == 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 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() }); - ({ test_fileno_12() }); - ({ test_fdopen_13() }); - ({ test_feof_ferror_14() }); return 0; } diff --git a/tests/unit/out/refcount/sys_stat.rs b/tests/unit/out/refcount/sys_stat.rs index 6446ae31..1115343c 100644 --- a/tests/unit/out/refcount/sys_stat.rs +++ b/tests/unit/out/refcount/sys_stat.rs @@ -87,19 +87,7 @@ pub fn test_fstat_1() { 0; let fd: Value = Rc::new(RefCell::new((*fp.borrow()).with(|__f| __f.fd))); 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!((((libc::fstat((*fd.borrow()), (st.as_pointer())) == 0) as i32) != 0)); assert!(((((*(*st.borrow()).st_size.borrow()) == 11_i64) as i32) != 0)); assert!(((((*(*st.borrow()).st_mtime.borrow()) > 0_i64) as i32) != 0)); assert!( diff --git a/tests/unit/out/refcount/unistd.rs b/tests/unit/out/refcount/unistd.rs index 90593761..0164ef74 100644 --- a/tests/unit/out/refcount/unistd.rs +++ b/tests/unit/out/refcount/unistd.rs @@ -536,62 +536,20 @@ pub fn test_open_6() { } pub fn test_fcntl_7() { assert!( - ((({ - let __res = match 1 { - ::libc::F_GETFL => FdRegistry::with_fd(0, |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) - }), - ::libc::F_SETFL => { - let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[][0])); - FdRegistry::with_fd(0, |__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])); - FdRegistry::with_fd(0, |__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 - } - } - } >= -1_i32) as i32) + (((panic!( + "fcntl is not supported in the refcount model (fd={}, cmd={}, varargs={})", + 0, + 1, + &[].len() + ) >= -1_i32) as i32) != 0) ); - let duped: Value = Rc::new(RefCell::new({ - let __res = match 0 { - ::libc::F_GETFL => FdRegistry::with_fd(0, |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) - }), - ::libc::F_SETFL => { - let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(100).into()][0])); - FdRegistry::with_fd(0, |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) - }) - } - ::libc::F_SETFD => { - let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(100).into()][0])); - FdRegistry::with_fd(0, |__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 - } - } - })); + let duped: Value = Rc::new(RefCell::new(panic!( + "fcntl is not supported in the refcount model (fd={}, cmd={}, varargs={})", + 0, + 0, + &[(100).into(),].len() + ))); assert!(((((*duped.borrow()) >= -1_i32) as i32) != 0)); if ((((*duped.borrow()) >= 0) as i32) != 0) { FdRegistry::close((*duped.borrow())); @@ -600,13 +558,12 @@ pub fn test_fcntl_7() { pub fn test_ioctl_8() { let arg: Value = Rc::new(RefCell::new(0)); assert!( - ((({ - panic!( - "ioctl is not supported in the refcount model (fd={}, request={})", - 0, 0_u64 - ); - 0 - } >= -1_i32) as i32) + (((panic!( + "ioctl is not supported in the refcount model (fd={}, request={}, varargs={})", + 0, + 0_u64, + &[(arg.as_pointer()).into(),].len() + ) >= -1_i32) as i32) != 0) ); } diff --git a/tests/unit/out/unsafe/fd_io.rs b/tests/unit/out/unsafe/fd_io.rs index d8e02547..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,493 +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 unsafe fn test_fileno_12() { - let mut path: *const libc::c_char = - (c"/tmp/cpp2rust_fd_io_fileno.tmp".as_ptr().cast_mut()).cast_const(); - let mut fp: *mut ::libc::FILE = libc::fopen(path, (c"wb".as_ptr().cast_mut()).cast_const()); - assert!((((!((fp).is_null())) as i32) != 0)); - assert!( - ((((libcc2rs::fwrite_unsafe( - (c"hello".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), - 1_usize, - 5_usize, - fp - )) == (5_usize)) as i32) - != 0) - ); - assert!(((((libc::fflush(fp)) == (0)) as i32) != 0)); - let mut fd: i32 = libc::fileno(fp); - assert!(((((fd) >= (0)) as i32) != 0)); - assert!(((((libc::fileno(fp)) == (fd)) 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!(((((libc::fclose(fp)) == (0)) as i32) != 0)); - assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); -} -pub unsafe fn test_fdopen_13() { - let mut path: *const libc::c_char = - (c"/tmp/cpp2rust_fd_io_fdopen.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)); - let mut fp: *mut ::libc::FILE = libc::fdopen(fd, (c"wb".as_ptr().cast_mut()).cast_const()); - assert!((((!((fp).is_null())) as i32) != 0)); - assert!( - ((((libcc2rs::fwrite_unsafe( - (c"hi".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), - 1_usize, - 2_usize, - fp - )) == (2_usize)) as i32) - != 0) - ); - assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); - fd = (unsafe { libc::open(path as *const i8, ::libc::O_RDONLY as i32) }); - assert!(((((fd) >= (0)) 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( - fd, - (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"hi".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_feof_ferror_14() { - let mut path: *const libc::c_char = - (c"/tmp/cpp2rust_fd_io_eof.tmp".as_ptr().cast_mut()).cast_const(); - let mut fp: *mut ::libc::FILE = libc::fopen(path, (c"wb".as_ptr().cast_mut()).cast_const()); - assert!((((!((fp).is_null())) as i32) != 0)); - assert!( - ((((libcc2rs::fwrite_unsafe( - (c"ab".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), - 1_usize, - 2_usize, - fp - )) == (2_usize)) as i32) - != 0) - ); - assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); - fp = libc::fopen(path, (c"rb".as_ptr().cast_mut()).cast_const()); - assert!((((!((fp).is_null())) as i32) != 0)); - assert!(((!(libc::feof(fp) != 0) as i32) != 0)); - assert!(((!(libc::ferror(fp) != 0) as i32) != 0)); - let mut buf: [libc::c_char; 2] = [(0 as libc::c_char); 2]; - assert!( - ((((libcc2rs::fread_unsafe( - (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), - 1_usize, - 2_usize, - fp - )) == (2_usize)) as i32) - != 0) - ); - assert!(((!(libc::feof(fp) != 0) as i32) != 0)); - assert!( - ((((libcc2rs::fread_unsafe( - (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), - 1_usize, - 1_usize, - fp - )) == (0_usize)) as i32) - != 0) - ); - assert!((libc::feof(fp) != 0)); - assert!(((!(libc::ferror(fp) != 0) as i32) != 0)); - assert!(((((libc::fseek(fp, 0_i64 as ::libc::c_long, 0)) == (0)) as i32) != 0)); - assert!(((!(libc::feof(fp) != 0) as i32) != 0)); - assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); - assert!(((((libc::unlink(path)) == (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() }); - (unsafe { test_fileno_12() }); - (unsafe { test_fdopen_13() }); - (unsafe { test_feof_ferror_14() }); return 0; } diff --git a/tests/unit/sys_stat.c b/tests/unit/sys_stat.c index 433665ee..d1261c4b 100644 --- a/tests/unit/sys_stat.c +++ b/tests/unit/sys_stat.c @@ -1,3 +1,4 @@ +// no-compile: refcount #include #include #include diff --git a/tests/unit/unistd.c b/tests/unit/unistd.c index 2bb439d3..39b4c8c0 100644 --- a/tests/unit/unistd.c +++ b/tests/unit/unistd.c @@ -1,4 +1,4 @@ -// panic-ub: refcount +// no-compile: refcount #include #include #include From 9115b848104423efe65b9b8472f202d94c9862fb Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 23 Jul 2026 22:21:02 +0100 Subject: [PATCH 3/7] Update sys_stat test --- tests/unit/out/refcount/sys_stat.rs | 14 +++++++++++++- tests/unit/sys_stat.c | 1 - 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/unit/out/refcount/sys_stat.rs b/tests/unit/out/refcount/sys_stat.rs index 1115343c..6446ae31 100644 --- a/tests/unit/out/refcount/sys_stat.rs +++ b/tests/unit/out/refcount/sys_stat.rs @@ -87,7 +87,19 @@ pub fn test_fstat_1() { 0; let fd: Value = Rc::new(RefCell::new((*fp.borrow()).with(|__f| __f.fd))); let st: Value = Rc::new(RefCell::new(Default::default())); - assert!((((libc::fstat((*fd.borrow()), (st.as_pointer())) == 0) as i32) != 0)); + 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()) == 11_i64) as i32) != 0)); assert!(((((*(*st.borrow()).st_mtime.borrow()) > 0_i64) as i32) != 0)); assert!( diff --git a/tests/unit/sys_stat.c b/tests/unit/sys_stat.c index d1261c4b..433665ee 100644 --- a/tests/unit/sys_stat.c +++ b/tests/unit/sys_stat.c @@ -1,4 +1,3 @@ -// no-compile: refcount #include #include #include From a4a53d3cd3b51ef79f66e01b2cfc707397096ec5 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 23 Jul 2026 22:28:27 +0100 Subject: [PATCH 4/7] Add 0/1/2 fds in initializer of FD_REGISTRY --- libcc2rs/src/fd.rs | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/libcc2rs/src/fd.rs b/libcc2rs/src/fd.rs index 3e846624..119cc87b 100644 --- a/libcc2rs/src/fd.rs +++ b/libcc2rs/src/fd.rs @@ -6,16 +6,16 @@ use std::os::fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd}; pub struct FdRegistry { fds: Vec>, - seeded: bool, } thread_local! { - static FD_REGISTRY: RefCell = const { - RefCell::new(FdRegistry { - fds: Vec::new(), - seeded: false, - }) - }; + static FD_REGISTRY: RefCell = RefCell::new(FdRegistry { + fds: vec![ + std::io::stdin().as_fd().try_clone_to_owned().ok(), + std::io::stdout().as_fd().try_clone_to_owned().ok(), + std::io::stderr().as_fd().try_clone_to_owned().ok(), + ], + }); } impl FdRegistry { @@ -33,19 +33,6 @@ impl FdRegistry { raw } - fn seed_std(&mut self) { - if self.seeded { - return; - } - self.seeded = true; - if self.fds.len() < 3 { - self.fds.resize_with(3, || None); - } - self.fds[0] = std::io::stdin().as_fd().try_clone_to_owned().ok(); - self.fds[1] = std::io::stdout().as_fd().try_clone_to_owned().ok(); - self.fds[2] = std::io::stderr().as_fd().try_clone_to_owned().ok(); - } - fn lookup(&self, fd: i32) -> BorrowedFd<'_> { self.fds .get(fd as usize) @@ -56,7 +43,6 @@ impl FdRegistry { pub fn with_fd(fd: i32, f: impl FnOnce(BorrowedFd<'_>) -> R) -> R { FD_REGISTRY.with(|r| { - r.borrow_mut().seed_std(); let reg = r.borrow(); f(reg.lookup(fd)) }) @@ -64,7 +50,6 @@ impl FdRegistry { pub fn with_fds(fds: &[i32], f: impl FnOnce(&[BorrowedFd<'_>]) -> R) -> R { FD_REGISTRY.with(|r| { - r.borrow_mut().seed_std(); let reg = r.borrow(); let borrowed: Vec> = fds.iter().map(|&fd| reg.lookup(fd)).collect(); f(&borrowed) From fee5aa71702529e8ed3beda68ead6ba5f8e65dcf Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 23 Jul 2026 22:41:08 +0100 Subject: [PATCH 5/7] Drop intermediate buf in fread_refcount --- libcc2rs/src/io.rs | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/libcc2rs/src/io.rs b/libcc2rs/src/io.rs index fe9a1718..70a98e28 100644 --- a/libcc2rs/src/io.rs +++ b/libcc2rs/src/io.rs @@ -107,25 +107,8 @@ pub fn fread_refcount(a0: AnyPtr, a1: usize, a2: usize, a3: Ptr) -> usize if total == 0 { return 0; } - let mut dst = a0.reinterpret_cast::(); - let mut buffer: [u8; 8192] = [0; 8192]; - let mut read_bytes: usize = 0; - - a3.with_mut(|f| { - while read_bytes < total { - let to_read = std::cmp::min(buffer.len(), total - read_bytes); - let n = f.read(&mut buffer[..to_read]); - if n == 0 { - break; - } - for &byte in &buffer[..n] { - dst.write(byte); - dst += 1; - } - read_bytes += n; - } - }); - + let dst = a0.reinterpret_cast::(); + let read_bytes = dst.with_slice_mut(total, |buf| a3.with_mut(|f| f.read(buf))); read_bytes / a1 } From 9352dc934602f31e9e1d224348e71f667befcae2 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Sun, 26 Jul 2026 11:45:03 +0100 Subject: [PATCH 6/7] Add rules for seek_set, seen_end, seek_cur --- rules/stdio/src.cpp | 6 ++++++ rules/stdio/tgt_unsafe.rs | 12 ++++++++++++ tests/unit/out/refcount/errno.rs | 2 +- tests/unit/out/refcount/lseek_ftruncate.rs | 6 +++--- tests/unit/out/refcount/stdio_nofd.rs | 6 +++--- tests/unit/out/unsafe/errno.rs | 6 +++++- tests/unit/out/unsafe/lseek_ftruncate.rs | 6 +++--- tests/unit/out/unsafe/stdio_nofd.rs | 9 ++++++--- tests/unit/out/unsafe/unistd.rs | 6 +++--- 9 files changed, 42 insertions(+), 17 deletions(-) diff --git a/rules/stdio/src.cpp b/rules/stdio/src.cpp index 21822c9a..271233d2 100644 --- a/rules/stdio/src.cpp +++ b/rules/stdio/src.cpp @@ -71,3 +71,9 @@ int f23(FILE *stream) { return getc(stream); } int f24(FILE *stream, char *buf, int mode, size_t size) { return setvbuf(stream, buf, mode, size); } + +int f25(void) { return SEEK_SET; } + +int f26(void) { return SEEK_CUR; } + +int f27(void) { return SEEK_END; } diff --git a/rules/stdio/tgt_unsafe.rs b/rules/stdio/tgt_unsafe.rs index bbd1d0a0..e2dafd93 100644 --- a/rules/stdio/tgt_unsafe.rs +++ b/rules/stdio/tgt_unsafe.rs @@ -104,3 +104,15 @@ unsafe fn f23(a0: *mut ::libc::FILE) -> i32 { unsafe fn f24(a0: *mut ::libc::FILE, a1: *mut libc::c_char, a2: i32, a3: usize) -> i32 { libc::setvbuf(a0, a1, a2, a3) } + +unsafe fn f25() -> i32 { + ::libc::SEEK_SET +} + +unsafe fn f26() -> i32 { + ::libc::SEEK_CUR +} + +unsafe fn f27() -> i32 { + ::libc::SEEK_END +} diff --git a/tests/unit/out/refcount/errno.rs b/tests/unit/out/refcount/errno.rs index f310f414..2cf9c8bc 100644 --- a/tests/unit/out/refcount/errno.rs +++ b/tests/unit/out/refcount/errno.rs @@ -28,7 +28,7 @@ pub fn test_errno_preserved_across_strdup_1() { pub fn test_errno_from_fseek_2() { libcc2rs::cpp2rust_errno().write(0); let r: Value = Rc::new(RefCell::new( - match libcc2rs::c_stdin().with_mut(|__v: &mut CFile| __v.seek(0_i64, 0)) { + match libcc2rs::c_stdin().with_mut(|__v: &mut CFile| __v.seek(0_i64, ::libc::SEEK_SET)) { -1 => -1, _ => 0, }, diff --git a/tests/unit/out/refcount/lseek_ftruncate.rs b/tests/unit/out/refcount/lseek_ftruncate.rs index 732fafa1..5da75c3c 100644 --- a/tests/unit/out/refcount/lseek_ftruncate.rs +++ b/tests/unit/out/refcount/lseek_ftruncate.rs @@ -50,7 +50,7 @@ fn main_0() -> i32 { ); assert!( ((({ - let __whence = match 2 { + let __whence = match ::libc::SEEK_END { 0 => nix::unistd::Whence::SeekSet, 1 => nix::unistd::Whence::SeekCur, 2 => nix::unistd::Whence::SeekEnd, @@ -70,7 +70,7 @@ fn main_0() -> i32 { ); assert!( ((({ - let __whence = match 0 { + let __whence = match ::libc::SEEK_SET { 0 => nix::unistd::Whence::SeekSet, 1 => nix::unistd::Whence::SeekCur, 2 => nix::unistd::Whence::SeekEnd, @@ -143,7 +143,7 @@ fn main_0() -> i32 { ); assert!( ((({ - let __whence = match 2 { + let __whence = match ::libc::SEEK_END { 0 => nix::unistd::Whence::SeekSet, 1 => nix::unistd::Whence::SeekCur, 2 => nix::unistd::Whence::SeekEnd, diff --git a/tests/unit/out/refcount/stdio_nofd.rs b/tests/unit/out/refcount/stdio_nofd.rs index 480f7d16..2cdef1a2 100644 --- a/tests/unit/out/refcount/stdio_nofd.rs +++ b/tests/unit/out/refcount/stdio_nofd.rs @@ -494,7 +494,7 @@ pub fn test_fseeko_4() { }; assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); assert!( - (((match (*fp.borrow()).with_mut(|__f| __f.seek(6_i64, 0)) { + (((match (*fp.borrow()).with_mut(|__f| __f.seek(6_i64, ::libc::SEEK_SET)) { -1 => -1, _ => 0, } == 0) as i32) @@ -528,7 +528,7 @@ pub fn test_fseeko_4() { != 0) ); assert!( - (((match (*fp.borrow()).with_mut(|__f| __f.seek((-5_i32 as i64), 2)) { + (((match (*fp.borrow()).with_mut(|__f| __f.seek((-5_i32 as i64), ::libc::SEEK_END)) { -1 => -1, _ => 0, } == 0) as i32) @@ -536,7 +536,7 @@ pub fn test_fseeko_4() { ); assert!(((((*fp.borrow()).with_mut(|__f| __f.getc()) == ('w' as i32)) as i32) != 0)); assert!( - (((match (*fp.borrow()).with_mut(|__f| __f.seek(1_i64, 1)) { + (((match (*fp.borrow()).with_mut(|__f| __f.seek(1_i64, ::libc::SEEK_CUR)) { -1 => -1, _ => 0, } == 0) as i32) diff --git a/tests/unit/out/unsafe/errno.rs b/tests/unit/out/unsafe/errno.rs index 41eda988..36749fca 100644 --- a/tests/unit/out/unsafe/errno.rs +++ b/tests/unit/out/unsafe/errno.rs @@ -26,7 +26,11 @@ pub unsafe fn test_errno_preserved_across_strdup_1() { } pub unsafe fn test_errno_from_fseek_2() { (*libcc2rs::cpp2rust_errno_unsafe()) = 0; - let mut r: i32 = libc::fseek(libcc2rs::stdin_unsafe(), 0_i64 as ::libc::c_long, 0); + let mut r: i32 = libc::fseek( + libcc2rs::stdin_unsafe(), + 0_i64 as ::libc::c_long, + ::libc::SEEK_SET, + ); assert!(((((r) == (-1_i32)) as i32) != 0)); assert!(((((*libcc2rs::cpp2rust_errno_unsafe()) == (libc::ESPIPE)) as i32) != 0)); (*libcc2rs::cpp2rust_errno_unsafe()) = 0; diff --git a/tests/unit/out/unsafe/lseek_ftruncate.rs b/tests/unit/out/unsafe/lseek_ftruncate.rs index 49be7fda..9bbf1c69 100644 --- a/tests/unit/out/unsafe/lseek_ftruncate.rs +++ b/tests/unit/out/unsafe/lseek_ftruncate.rs @@ -30,8 +30,8 @@ unsafe fn main_0() -> i32 { )) == (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)); + assert!(((((libc::lseek(fd, 0_i64, ::libc::SEEK_END)) == (11_i64)) as i32) != 0)); + assert!(((((libc::lseek(fd, 6_i64, ::libc::SEEK_SET)) == (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; @@ -56,7 +56,7 @@ unsafe fn main_0() -> 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::lseek(fd, 0_i64, ::libc::SEEK_END)) == (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/stdio_nofd.rs b/tests/unit/out/unsafe/stdio_nofd.rs index 15ecb674..0382ace1 100644 --- a/tests/unit/out/unsafe/stdio_nofd.rs +++ b/tests/unit/out/unsafe/stdio_nofd.rs @@ -231,7 +231,7 @@ pub unsafe fn test_fseeko_4() { assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); fp = libc::fopen(path, (c"rb".as_ptr().cast_mut()).cast_const()); assert!((((!((fp).is_null())) as i32) != 0)); - assert!(((((libc::fseeko(fp, 6_i64 as ::libc::off_t, 0)) == (0)) as i32) != 0)); + assert!(((((libc::fseeko(fp, 6_i64 as ::libc::off_t, ::libc::SEEK_SET)) == (0)) as i32) != 0)); let mut buf: [libc::c_char; 8] = [ (0 as libc::c_char), (0 as libc::c_char), @@ -273,9 +273,12 @@ pub unsafe fn test_fseeko_4() { }) == (0)) as i32) != 0) ); - assert!(((((libc::fseeko(fp, (-5_i32 as i64) as ::libc::off_t, 2)) == (0)) as i32) != 0)); + assert!( + ((((libc::fseeko(fp, (-5_i32 as i64) as ::libc::off_t, ::libc::SEEK_END)) == (0)) as i32) + != 0) + ); assert!(((((libc::fgetc(fp)) == ('w' as i32)) as i32) != 0)); - assert!(((((libc::fseeko(fp, 1_i64 as ::libc::off_t, 1)) == (0)) as i32) != 0)); + assert!(((((libc::fseeko(fp, 1_i64 as ::libc::off_t, ::libc::SEEK_CUR)) == (0)) as i32) != 0)); assert!(((((libc::fgetc(fp)) == ('r' as i32)) as i32) != 0)); assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); diff --git a/tests/unit/out/unsafe/unistd.rs b/tests/unit/out/unsafe/unistd.rs index edfeb8d0..4d15e652 100644 --- a/tests/unit/out/unsafe/unistd.rs +++ b/tests/unit/out/unsafe/unistd.rs @@ -31,8 +31,8 @@ pub unsafe fn test_lseek_1() { fp = libc::fopen(path, (c"rb".as_ptr().cast_mut()).cast_const()); assert!((((!((fp).is_null())) as i32) != 0)); let mut fd: i32 = libc::fileno(fp); - assert!(((((libc::lseek(fd, 0_i64, 2)) == (11_i64)) as i32) != 0)); - assert!(((((libc::lseek(fd, 6_i64, 0)) == (6_i64)) as i32) != 0)); + assert!(((((libc::lseek(fd, 0_i64, ::libc::SEEK_END)) == (11_i64)) as i32) != 0)); + assert!(((((libc::lseek(fd, 6_i64, ::libc::SEEK_SET)) == (6_i64)) as i32) != 0)); let mut buf: [libc::c_char; 8] = [ (0 as libc::c_char), (0 as libc::c_char), @@ -221,7 +221,7 @@ pub unsafe fn test_ftruncate_5() { fp = libc::fopen(path, (c"rb".as_ptr().cast_mut()).cast_const()); assert!((((!((fp).is_null())) as i32) != 0)); fd = (libc::fileno(fp)).clone(); - assert!(((((libc::lseek(fd, 0_i64, 2)) == (5_i64)) as i32) != 0)); + assert!(((((libc::lseek(fd, 0_i64, ::libc::SEEK_END)) == (5_i64)) as i32) != 0)); assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); libc::unlink(path); } From 3e403c7f39c736952857c3f5867181a5317aeaf5 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Sun, 26 Jul 2026 11:45:19 +0100 Subject: [PATCH 7/7] Don't use /tmp files in unit tests --- tests/unit/out/refcount/fd_io.rs | 2 +- tests/unit/out/refcount/stdio.rs | 2 +- tests/unit/out/refcount/stdio_nofd.rs | 14 +++++++------- tests/unit/out/refcount/sys_stat.rs | 4 ++-- .../unit/out/refcount/user_defined_same_as_libc.rs | 2 +- tests/unit/out/unsafe/fd_io.rs | 2 +- tests/unit/out/unsafe/user_defined_same_as_libc.rs | 2 +- tests/unit/user_defined_same_as_libc.c | 2 +- 8 files changed, 15 insertions(+), 15 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/refcount/stdio.rs b/tests/unit/out/refcount/stdio.rs index 896d239e..33c19da5 100644 --- a/tests/unit/out/refcount/stdio.rs +++ b/tests/unit/out/refcount/stdio.rs @@ -110,7 +110,7 @@ pub fn test_fileno_3() { assert!((((libcc2rs::c_stdout().with(|__f| __f.fd) == 1) as i32) != 0)); assert!((((libcc2rs::c_stderr().with(|__f| __f.fd) == 2) as i32) != 0)); let file: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_fileno_test.tmp", + b"cpp2rust_fileno_test.tmp", ))); let fp: Value> = Rc::new(RefCell::new( match CFile::open( diff --git a/tests/unit/out/refcount/stdio_nofd.rs b/tests/unit/out/refcount/stdio_nofd.rs index 2cdef1a2..e459d55b 100644 --- a/tests/unit/out/refcount/stdio_nofd.rs +++ b/tests/unit/out/refcount/stdio_nofd.rs @@ -8,7 +8,7 @@ use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn test_fputc_fputs_0() { let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_stdio_nofd_puts.tmp", + b"cpp2rust_stdio_nofd_puts.tmp", ))); let fp: Value> = Rc::new(RefCell::new( match CFile::open( @@ -129,7 +129,7 @@ pub fn test_puts_1() { } pub fn test_fgets_getc_2() { let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_stdio_nofd_gets.tmp", + b"cpp2rust_stdio_nofd_gets.tmp", ))); let fp: Value> = Rc::new(RefCell::new( match CFile::open( @@ -360,7 +360,7 @@ pub fn test_fgets_getc_2() { } pub fn test_freopen_3() { let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_stdio_nofd_reopen.tmp", + b"cpp2rust_stdio_nofd_reopen.tmp", ))); let fp: Value> = Rc::new(RefCell::new( match CFile::open( @@ -453,7 +453,7 @@ pub fn test_freopen_3() { } pub fn test_fseeko_4() { let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_stdio_nofd_seek.tmp", + b"cpp2rust_stdio_nofd_seek.tmp", ))); let fp: Value> = Rc::new(RefCell::new( match CFile::open( @@ -564,10 +564,10 @@ pub fn test_fseeko_4() { } pub fn test_rename_5() { let from: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_stdio_nofd_from.tmp", + b"cpp2rust_stdio_nofd_from.tmp", ))); let to: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_stdio_nofd_to.tmp", + b"cpp2rust_stdio_nofd_to.tmp", ))); let fp: Value> = Rc::new(RefCell::new( match CFile::open( @@ -665,7 +665,7 @@ pub fn test_rename_5() { } pub fn test_setvbuf_6() { let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_stdio_nofd_vbuf.tmp", + b"cpp2rust_stdio_nofd_vbuf.tmp", ))); let fp: Value> = Rc::new(RefCell::new( match CFile::open( diff --git a/tests/unit/out/refcount/sys_stat.rs b/tests/unit/out/refcount/sys_stat.rs index 6446ae31..6ca8fe0c 100644 --- a/tests/unit/out/refcount/sys_stat.rs +++ b/tests/unit/out/refcount/sys_stat.rs @@ -8,7 +8,7 @@ use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn test_stat_0() { let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_stat_test.tmp", + b"cpp2rust_stat_test.tmp", ))); let fp: Value> = Rc::new(RefCell::new( match CFile::open( @@ -63,7 +63,7 @@ pub fn test_stat_0() { } pub fn test_fstat_1() { let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_fstat_test.tmp", + b"cpp2rust_fstat_test.tmp", ))); let fp: Value> = Rc::new(RefCell::new( match CFile::open( diff --git a/tests/unit/out/refcount/user_defined_same_as_libc.rs b/tests/unit/out/refcount/user_defined_same_as_libc.rs index 56abef01..04bd1c0c 100644 --- a/tests/unit/out/refcount/user_defined_same_as_libc.rs +++ b/tests/unit/out/refcount/user_defined_same_as_libc.rs @@ -20,7 +20,7 @@ fn main_0() -> i32 { let fp: Value> = Rc::new(RefCell::new( ({ fopen_0( - Ptr::from_string_literal(b"/tmp/irrelevant-file"), + Ptr::from_string_literal(b"irrelevant-file"), Ptr::from_string_literal(b"r"), ) }), 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, diff --git a/tests/unit/out/unsafe/user_defined_same_as_libc.rs b/tests/unit/out/unsafe/user_defined_same_as_libc.rs index 3fd2350f..6d552900 100644 --- a/tests/unit/out/unsafe/user_defined_same_as_libc.rs +++ b/tests/unit/out/unsafe/user_defined_same_as_libc.rs @@ -22,7 +22,7 @@ pub fn main() { unsafe fn main_0() -> i32 { let mut fp: *mut ::libc::FILE = (unsafe { fopen_0( - (c"/tmp/irrelevant-file".as_ptr().cast_mut()).cast_const(), + (c"irrelevant-file".as_ptr().cast_mut()).cast_const(), (c"r".as_ptr().cast_mut()).cast_const(), ) }); diff --git a/tests/unit/user_defined_same_as_libc.c b/tests/unit/user_defined_same_as_libc.c index 57863b8a..eeb5c0ef 100644 --- a/tests/unit/user_defined_same_as_libc.c +++ b/tests/unit/user_defined_same_as_libc.c @@ -8,7 +8,7 @@ FILE *fopen(const char *path, const char *mode) { } int main() { - FILE *fp = fopen("/tmp/irrelevant-file", "r"); + FILE *fp = fopen("irrelevant-file", "r"); assert(fp == NULL); return 0; }