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
34 changes: 28 additions & 6 deletions rules/fcntl/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,34 @@
use libcc2rs::*;

fn f1(a0: i32, a1: i32, va: &[VaArg]) -> i32 {
panic!(
"fcntl is not supported in the refcount model (fd={}, cmd={}, varargs={})",
a0,
a1,
va.len()
)
let __res = match a1 {
::libc::F_GETFL => FdRegistry::with_fd(a0, |__fd| {
nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL)
}),
::libc::F_SETFL => {
let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&va[0]));
FdRegistry::with_fd(a0, |__fd| {
nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags))
})
}
::libc::F_GETFD => FdRegistry::with_fd(a0, |__fd| {
nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFD)
}),
::libc::F_SETFD => {
let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&va[0]));
FdRegistry::with_fd(a0, |__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
}
}
}

fn f2(a0: Ptr<u8>, a1: i32, va: &[VaArg]) -> i32 {
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/fcntl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <assert.h>
#include <fcntl.h>
#include <unistd.h>

int main(void) {
int fds[2];
assert(pipe(fds) == 0);
int flags = fcntl(fds[0], F_GETFL, 0);
assert(flags >= 0);
assert((flags & O_NONBLOCK) == 0);
assert(fcntl(fds[0], F_SETFL, flags | O_NONBLOCK) == 0);
flags = fcntl(fds[0], F_GETFL, 0);
assert((flags & O_NONBLOCK) != 0);
char b;
assert(read(fds[0], &b, 1) == -1);
assert((fcntl(fds[0], F_GETFD, 0) & FD_CLOEXEC) == 0);
assert(fcntl(fds[0], F_SETFD, FD_CLOEXEC) == 0);
assert((fcntl(fds[0], F_GETFD, 0) & FD_CLOEXEC) != 0);
assert(close(fds[0]) == 0);
assert(close(fds[1]) == 0);
return 0;
}
251 changes: 251 additions & 0 deletions tests/unit/out/refcount/fcntl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
extern crate libcc2rs;
use libcc2rs::*;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
let fds: Value<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)
);
let flags: Value<i32> = 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_GETFD => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| {
nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFD)
}),
::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_GETFD => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| {
nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFD)
}),
::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_GETFD => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| {
nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFD)
}),
::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<u8> = <Value<u8>>::default();
assert!(
(((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| {
((b.as_pointer()) as Ptr<u8>)
.to_any()
.reinterpret_cast::<u8>()
.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 1 {
::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_GETFD => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| {
nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFD)
}),
::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
}
}
} & 1)
== 0) 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_GETFD => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| {
nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFD)
}),
::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!(
(((({
let __res = match 1 {
::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_GETFD => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| {
nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFD)
}),
::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
}
}
} & 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));
return 0;
}
57 changes: 57 additions & 0 deletions tests/unit/out/unsafe/fcntl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
extern crate libc;
use libc::*;
extern crate libcc2rs;
use libcc2rs::*;
use std::collections::BTreeMap;
use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
pub fn main() {
unsafe {
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
let mut fds: [i32; 2] = [0_i32; 2];
assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0));
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, 1 as i32, (0),) }) & (1)) == (0))
as i32)
!= 0)
);
assert!(
((((unsafe { libc::fcntl(fds[(0) as usize] as i32, 2 as i32, (1),) }) == (0)) as i32) != 0)
);
assert!(
(((((unsafe { libc::fcntl(fds[(0) as usize] as i32, 1 as i32, (0),) }) & (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));
return 0;
}
Loading