Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions rules/eventfd/tgt_refcount.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

use libcc2rs::*;

#[cfg(target_os = "linux")]
fn f1() -> i32 {
::libc::EFD_CLOEXEC
}

#[cfg(target_os = "linux")]
fn f2() -> i32 {
::libc::EFD_NONBLOCK
}

#[cfg(target_os = "linux")]
fn f3() -> i32 {
::libc::EFD_SEMAPHORE
}

#[cfg(target_os = "linux")]
fn f4(a0: u32, a1: i32) -> i32 {
match nix::sys::eventfd::EventFd::from_value_and_flags(
a0,
nix::sys::eventfd::EfdFlags::from_bits_truncate(a1),
) {
Ok(__efd) => FdRegistry::register(std::os::fd::OwnedFd::from(__efd)),
Err(__e) => {
libcc2rs::cpp2rust_errno().write(__e as i32);
-1
}
}
}
16 changes: 16 additions & 0 deletions rules/socket/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,19 @@ fn f11(a0: i32, a1: i32, a2: i32, a3: Ptr<i32>) -> i32 {
}
}
}

fn f17(a0: i32, a1: i32) -> i32 {
match nix::sys::socket::Backlog::new(a1) {
Ok(__b) => match FdRegistry::with_fd(a0, |__fd| nix::sys::socket::listen(&__fd, __b)) {
Ok(()) => 0,
Err(__e) => {
libcc2rs::cpp2rust_errno().write(__e as i32);
-1
}
},
Err(__e) => {
libcc2rs::cpp2rust_errno().write(__e as i32);
-1
}
}
}
2 changes: 2 additions & 0 deletions rules/src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub mod dirent_tgt_unsafe;
pub mod errno_tgt_refcount;
#[path = r#"../errno/tgt_unsafe.rs"#]
pub mod errno_tgt_unsafe;
#[path = r#"../eventfd/tgt_refcount.rs"#]
pub mod eventfd_tgt_refcount;
#[path = r#"../eventfd/tgt_unsafe.rs"#]
pub mod eventfd_tgt_unsafe;
#[path = r#"../fcntl/tgt_refcount.rs"#]
Expand Down
36 changes: 36 additions & 0 deletions rules/unistd/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ fn f1(a0: i32) -> i32 {
FdRegistry::close(a0)
}

fn f2(a0: i32, a1: i64, a2: i32) -> i64 {
let __whence = match a2 {
0 => nix::unistd::Whence::SeekSet,
1 => nix::unistd::Whence::SeekCur,
2 => nix::unistd::Whence::SeekEnd,
__w => panic!("lseek: unsupported whence {__w}"),
};
match FdRegistry::with_fd(a0, |__fd| nix::unistd::lseek(__fd, a1, __whence)) {
Ok(__off) => __off,
Err(__e) => {
libcc2rs::cpp2rust_errno().write(__e as i32);
-1
}
}
}

fn f3(a0: i32, a1: AnyPtr, a2: usize) -> isize {
match FdRegistry::with_fd(a0, |__fd| {
a1.reinterpret_cast::<u8>()
Expand Down Expand Up @@ -45,6 +61,26 @@ fn f5(a0: Ptr<i32>) -> i32 {
}
}

fn f6(a0: i32, a1: i64) -> i32 {
match FdRegistry::with_fd(a0, |__fd| nix::unistd::ftruncate(__fd, a1)) {
Ok(()) => 0,
Err(__e) => {
libcc2rs::cpp2rust_errno().write(__e as i32);
-1
}
}
}

fn f7(a0: i32) -> i32 {
match FdRegistry::with_fd(a0, |__fd| nix::unistd::isatty(__fd)) {
Ok(__tty) => __tty as i32,
Err(__e) => {
libcc2rs::cpp2rust_errno().write(__e as i32);
0
}
}
}

fn f8() -> u32 {
nix::unistd::geteuid().as_raw()
}
Expand Down
14 changes: 0 additions & 14 deletions tests/unit/fd_io.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <assert.h>
#include <fcntl.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

int main(void) {
Expand All @@ -19,18 +18,5 @@ int main(void) {
assert(read(fd, buf, sizeof(buf)) == 0);
assert(close(fd) == 0);
assert(unlink(path) == 0);
int fds[2];
assert(pipe(fds) == 0);
assert(write(fds[1], "ab", 2) == 2);
char buf2[4];
memset(buf2, 0, sizeof(buf2));
assert(read(fds[0], buf2, sizeof(buf2)) == 2);
assert(strcmp(buf2, "ab") == 0);
assert(close(fds[1]) == 0);
assert(read(fds[0], buf2, sizeof(buf2)) == 0);
assert(close(fds[0]) == 0);
int s = socket(AF_INET, SOCK_STREAM, 0);
assert(s >= 0);
assert(close(s) == 0);
return 0;
}
13 changes: 13 additions & 0 deletions tests/unit/isatty.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <assert.h>
#include <fcntl.h>
#include <unistd.h>

int main(void) {
const char *path = "/tmp/cpp2rust_isatty_test.tmp";
int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644);
assert(fd >= 0);
assert(isatty(fd) == 0);
assert(close(fd) == 0);
assert(unlink(path) == 0);
return 0;
}
22 changes: 22 additions & 0 deletions tests/unit/lseek_ftruncate.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <assert.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>

int main(void) {
const char *path = "/tmp/cpp2rust_lseek_ftruncate_test.tmp";
int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644);
assert(fd >= 0);
assert(write(fd, "hello world", 11) == 11);
assert(lseek(fd, 0, SEEK_END) == 11);
assert(lseek(fd, 6, SEEK_SET) == 6);
char buf[16];
memset(buf, 0, sizeof(buf));
assert(read(fd, buf, sizeof(buf)) == 5);
assert(strcmp(buf, "world") == 0);
assert(ftruncate(fd, 5) == 0);
assert(lseek(fd, 0, SEEK_END) == 5);
assert(close(fd) == 0);
assert(unlink(path) == 0);
return 0;
}
124 changes: 0 additions & 124 deletions tests/unit/out/refcount/fd_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,129 +138,5 @@ fn main_0() -> i32 {
} == 0) as i32)
!= 0)
);
let fds: Value<Box<[i32]>> = Rc::new(RefCell::new(
(0..2).map(|_| <i32>::default()).collect::<Box<[i32]>>(),
));
assert!(
(((match nix::unistd::pipe() {
Ok((__r, __w)) => {
let __fds = (fds.as_pointer() as Ptr<i32>).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::<u8>()
.with_slice(2_usize, |__buf| nix::unistd::write(__fd, __buf))
}) {
Ok(__n) => __n as isize,
Err(__e) => {
libcc2rs::cpp2rust_errno().write(__e as i32);
-1
}
} == 2_isize) as i32)
!= 0)
);
let buf2: Value<Box<[u8]>> = Rc::new(RefCell::new(
(0..4).map(|_| <u8>::default()).collect::<Box<[u8]>>(),
));
{
((buf2.as_pointer() as Ptr<u8>) as Ptr<u8>)
.to_any()
.memset((0) as u8, ::std::mem::size_of::<[u8; 4]>() as usize);
((buf2.as_pointer() as Ptr<u8>) as Ptr<u8>).to_any().clone()
};
assert!(
(((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| {
((buf2.as_pointer() as Ptr<u8>) as Ptr<u8>)
.to_any()
.reinterpret_cast::<u8>()
.with_slice_mut(::std::mem::size_of::<[u8; 4]>(), |__buf| {
nix::unistd::read(__fd, __buf)
})
}) {
Ok(__n) => __n as isize,
Err(__e) => {
libcc2rs::cpp2rust_errno().write(__e as i32);
-1
}
} == 2_isize) as i32)
!= 0)
);
assert!(
((({
let mut __it1 = (buf2.as_pointer() as Ptr<u8>).to_c_string_iterator();
let mut __it2 = Ptr::from_string_literal(b"ab").to_c_string_iterator();
loop {
let __c1 = __it1.next();
let __c2 = __it2.next();
if __c1 != __c2 {
break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32);
}
if __c1.is_none() {
break 0;
}
}
} == 0) as i32)
!= 0)
);
assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0));
assert!(
(((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| {
((buf2.as_pointer() as Ptr<u8>) as Ptr<u8>)
.to_any()
.reinterpret_cast::<u8>()
.with_slice_mut(::std::mem::size_of::<[u8; 4]>(), |__buf| {
nix::unistd::read(__fd, __buf)
})
}) {
Ok(__n) => __n as isize,
Err(__e) => {
libcc2rs::cpp2rust_errno().write(__e as i32);
-1
}
} == 0_isize) as i32)
!= 0)
);
assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0));
let s: Value<i32> = Rc::new(RefCell::new({
let __family = match libc::AF_INET {
::libc::AF_INET => nix::sys::socket::AddressFamily::Inet,
::libc::AF_INET6 => nix::sys::socket::AddressFamily::Inet6,
::libc::AF_UNIX => nix::sys::socket::AddressFamily::Unix,
__d => panic!("socket: unsupported domain {__d}"),
};
let __flags = nix::sys::socket::SockFlag::from_bits_truncate(libc::SOCK_STREAM);
let __ty = match libc::SOCK_STREAM & !nix::sys::socket::SockFlag::all().bits() {
::libc::SOCK_STREAM => nix::sys::socket::SockType::Stream,
::libc::SOCK_DGRAM => nix::sys::socket::SockType::Datagram,
__t => panic!("socket: unsupported type {__t}"),
};
let __proto = match 0 {
0 => None,
::libc::IPPROTO_TCP => Some(nix::sys::socket::SockProtocol::Tcp),
::libc::IPPROTO_UDP => Some(nix::sys::socket::SockProtocol::Udp),
__p => panic!("socket: unsupported protocol {__p}"),
};
match nix::sys::socket::socket(__family, __ty, __flags, __proto) {
Ok(__ofd) => FdRegistry::register(__ofd),
Err(__e) => {
libcc2rs::cpp2rust_errno().write(__e as i32);
-1
}
}
}));
assert!(((((*s.borrow()) >= 0) as i32) != 0));
assert!((((FdRegistry::close((*s.borrow())) == 0) as i32) != 0));
return 0;
}
58 changes: 58 additions & 0 deletions tests/unit/out/refcount/isatty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
extern crate libcc2rs;
use libcc2rs::*;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
let path: Value<Ptr<u8>> = Rc::new(RefCell::new(Ptr::from_string_literal(
b"/tmp/cpp2rust_isatty_test.tmp",
)));
let fd: Value<i32> = Rc::new(RefCell::new({
let __mode = match &[(420).into()].first() {
Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t),
None => nix::sys::stat::Mode::empty(),
};
match nix::fcntl::open(
(*path.borrow()).to_rust_string().as_str(),
nix::fcntl::OFlag::from_bits_retain(
((::libc::O_RDWR | ::libc::O_CREAT) | ::libc::O_TRUNC),
),
__mode,
) {
Ok(__ofd) => FdRegistry::register(__ofd),
Err(__e) => {
libcc2rs::cpp2rust_errno().write(__e as i32);
-1
}
}
}));
assert!(((((*fd.borrow()) >= 0) as i32) != 0));
assert!(
(((match FdRegistry::with_fd((*fd.borrow()), |__fd| nix::unistd::isatty(__fd)) {
Ok(__tty) => __tty as i32,
Err(__e) => {
libcc2rs::cpp2rust_errno().write(__e as i32);
0
}
} == 0) as i32)
!= 0)
);
assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0));
assert!(
(((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) {
Ok(()) => 0,
Err(__e) => {
libcc2rs::cpp2rust_errno().write(__e as i32);
-1
}
} == 0) as i32)
!= 0)
);
return 0;
}
Loading
Loading