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
61 changes: 61 additions & 0 deletions rules/socket/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,64 @@ fn f10(a0: i32, a1: AnyPtr, a2: usize, a3: i32) -> isize {
}
}
}

fn f6(a0: i32, a1: i32, a2: i32) -> i32 {
let __family = match a0 {
::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(a1);
let __ty = match a1 & !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 a2 {
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
}
}
}

fn f11(a0: i32, a1: i32, a2: i32, a3: Ptr<i32>) -> i32 {
let __family = match a0 {
::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!("socketpair: unsupported domain {__d}"),
};
let __flags = nix::sys::socket::SockFlag::from_bits_truncate(a1);
let __ty = match a1 & !nix::sys::socket::SockFlag::all().bits() {
::libc::SOCK_STREAM => nix::sys::socket::SockType::Stream,
::libc::SOCK_DGRAM => nix::sys::socket::SockType::Datagram,
__t => panic!("socketpair: unsupported type {__t}"),
};
let __proto = match a2 {
0 => None,
::libc::IPPROTO_TCP => Some(nix::sys::socket::SockProtocol::Tcp),
::libc::IPPROTO_UDP => Some(nix::sys::socket::SockProtocol::Udp),
__p => panic!("socketpair: unsupported protocol {__p}"),
};
match nix::sys::socket::socketpair(__family, __ty, __proto, __flags) {
Ok((__a, __b)) => {
let __sv = a3.clone();
__sv.write(FdRegistry::register(__a));
__sv.offset(1).write(FdRegistry::register(__b));
0
}
Err(__e) => {
libcc2rs::cpp2rust_errno().write(__e as i32);
-1
}
}
}
15 changes: 15 additions & 0 deletions rules/unistd/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ fn f4(a0: Ptr<u8>) -> i32 {
}
}

fn f5(a0: Ptr<i32>) -> i32 {
match nix::unistd::pipe() {
Ok((__r, __w)) => {
let __fds = a0.clone();
__fds.write(FdRegistry::register(__r));
__fds.offset(1).write(FdRegistry::register(__w));
0
}
Err(__e) => {
libcc2rs::cpp2rust_errno().write(__e as i32);
-1
}
}
}

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

int main(void) {
Expand All @@ -18,5 +19,18 @@ 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;
}
124 changes: 124 additions & 0 deletions tests/unit/out/refcount/fd_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,129 @@ 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;
}
46 changes: 46 additions & 0 deletions tests/unit/out/unsafe/fd_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,51 @@ unsafe fn main_0() -> i32 {
);
assert!(((((libc::close(fd)) == (0)) as i32) != 0));
assert!(((((libc::unlink(path)) == (0)) as i32) != 0));
let mut fds: [i32; 2] = [0_i32; 2];
assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0));
assert!(
((((libc::write(
fds[(1) as usize],
(c"ab".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void),
2_usize
)) == (2_isize)) as i32)
!= 0)
);
let mut buf2: [libc::c_char; 4] = [(0 as libc::c_char); 4];
{
let byte_0 = (buf2.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) as *mut u8;
for offset in 0..::std::mem::size_of::<[libc::c_char; 4]>() {
*byte_0.offset(offset as isize) = 0 as u8;
}
(buf2.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void)
};
assert!(
((((libc::read(
fds[(0) as usize],
(buf2.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void),
::std::mem::size_of::<[libc::c_char; 4]>()
)) == (2_isize)) as i32)
!= 0)
);
assert!(
((((libc::strcmp(
(buf2.as_mut_ptr()).cast_const(),
(c"ab".as_ptr().cast_mut()).cast_const()
)) == (0)) as i32)
!= 0)
);
assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0));
assert!(
((((libc::read(
fds[(0) as usize],
(buf2.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void),
::std::mem::size_of::<[libc::c_char; 4]>()
)) == (0_isize)) as i32)
!= 0)
);
assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0));
let mut s: i32 = libc::socket(libc::AF_INET, libc::SOCK_STREAM, 0);
assert!(((((s) >= (0)) as i32) != 0));
assert!(((((libc::close(s)) == (0)) as i32) != 0));
return 0;
}
Loading