Skip to content

Commit 47af007

Browse files
authored
Add safe rules for lseek,ftruncate,isatty,listen,eventfd (#279)
1 parent 30e67a1 commit 47af007

22 files changed

Lines changed: 799 additions & 184 deletions

‎rules/eventfd/tgt_refcount.rs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
use libcc2rs::*;
5+
6+
#[cfg(target_os = "linux")]
7+
fn f1() -> i32 {
8+
::libc::EFD_CLOEXEC
9+
}
10+
11+
#[cfg(target_os = "linux")]
12+
fn f2() -> i32 {
13+
::libc::EFD_NONBLOCK
14+
}
15+
16+
#[cfg(target_os = "linux")]
17+
fn f3() -> i32 {
18+
::libc::EFD_SEMAPHORE
19+
}
20+
21+
#[cfg(target_os = "linux")]
22+
fn f4(a0: u32, a1: i32) -> i32 {
23+
match nix::sys::eventfd::EventFd::from_value_and_flags(
24+
a0,
25+
nix::sys::eventfd::EfdFlags::from_bits_truncate(a1),
26+
) {
27+
Ok(__efd) => FdRegistry::register(std::os::fd::OwnedFd::from(__efd)),
28+
Err(__e) => {
29+
libcc2rs::cpp2rust_errno().write(__e as i32);
30+
-1
31+
}
32+
}
33+
}

‎rules/socket/tgt_refcount.rs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,19 @@ fn f11(a0: i32, a1: i32, a2: i32, a3: Ptr<i32>) -> i32 {
202202
}
203203
}
204204
}
205+
206+
fn f17(a0: i32, a1: i32) -> i32 {
207+
match nix::sys::socket::Backlog::new(a1) {
208+
Ok(__b) => match FdRegistry::with_fd(a0, |__fd| nix::sys::socket::listen(&__fd, __b)) {
209+
Ok(()) => 0,
210+
Err(__e) => {
211+
libcc2rs::cpp2rust_errno().write(__e as i32);
212+
-1
213+
}
214+
},
215+
Err(__e) => {
216+
libcc2rs::cpp2rust_errno().write(__e as i32);
217+
-1
218+
}
219+
}
220+
}

‎rules/src/modules.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ pub mod dirent_tgt_unsafe;
4848
pub mod errno_tgt_refcount;
4949
#[path = r#"../errno/tgt_unsafe.rs"#]
5050
pub mod errno_tgt_unsafe;
51+
#[path = r#"../eventfd/tgt_refcount.rs"#]
52+
pub mod eventfd_tgt_refcount;
5153
#[path = r#"../eventfd/tgt_unsafe.rs"#]
5254
pub mod eventfd_tgt_unsafe;
5355
#[path = r#"../fcntl/tgt_refcount.rs"#]

‎rules/unistd/tgt_refcount.rs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ fn f1(a0: i32) -> i32 {
77
FdRegistry::close(a0)
88
}
99

10+
fn f2(a0: i32, a1: i64, a2: i32) -> i64 {
11+
let __whence = match a2 {
12+
0 => nix::unistd::Whence::SeekSet,
13+
1 => nix::unistd::Whence::SeekCur,
14+
2 => nix::unistd::Whence::SeekEnd,
15+
__w => panic!("lseek: unsupported whence {__w}"),
16+
};
17+
match FdRegistry::with_fd(a0, |__fd| nix::unistd::lseek(__fd, a1, __whence)) {
18+
Ok(__off) => __off,
19+
Err(__e) => {
20+
libcc2rs::cpp2rust_errno().write(__e as i32);
21+
-1
22+
}
23+
}
24+
}
25+
1026
fn f3(a0: i32, a1: AnyPtr, a2: usize) -> isize {
1127
match FdRegistry::with_fd(a0, |__fd| {
1228
a1.reinterpret_cast::<u8>()
@@ -45,6 +61,26 @@ fn f5(a0: Ptr<i32>) -> i32 {
4561
}
4662
}
4763

64+
fn f6(a0: i32, a1: i64) -> i32 {
65+
match FdRegistry::with_fd(a0, |__fd| nix::unistd::ftruncate(__fd, a1)) {
66+
Ok(()) => 0,
67+
Err(__e) => {
68+
libcc2rs::cpp2rust_errno().write(__e as i32);
69+
-1
70+
}
71+
}
72+
}
73+
74+
fn f7(a0: i32) -> i32 {
75+
match FdRegistry::with_fd(a0, |__fd| nix::unistd::isatty(__fd)) {
76+
Ok(__tty) => __tty as i32,
77+
Err(__e) => {
78+
libcc2rs::cpp2rust_errno().write(__e as i32);
79+
0
80+
}
81+
}
82+
}
83+
4884
fn f8() -> u32 {
4985
nix::unistd::geteuid().as_raw()
5086
}

‎tests/unit/fd_io.c‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include <assert.h>
22
#include <fcntl.h>
33
#include <string.h>
4-
#include <sys/socket.h>
54
#include <unistd.h>
65

76
int main(void) {
@@ -19,18 +18,5 @@ int main(void) {
1918
assert(read(fd, buf, sizeof(buf)) == 0);
2019
assert(close(fd) == 0);
2120
assert(unlink(path) == 0);
22-
int fds[2];
23-
assert(pipe(fds) == 0);
24-
assert(write(fds[1], "ab", 2) == 2);
25-
char buf2[4];
26-
memset(buf2, 0, sizeof(buf2));
27-
assert(read(fds[0], buf2, sizeof(buf2)) == 2);
28-
assert(strcmp(buf2, "ab") == 0);
29-
assert(close(fds[1]) == 0);
30-
assert(read(fds[0], buf2, sizeof(buf2)) == 0);
31-
assert(close(fds[0]) == 0);
32-
int s = socket(AF_INET, SOCK_STREAM, 0);
33-
assert(s >= 0);
34-
assert(close(s) == 0);
3521
return 0;
3622
}

‎tests/unit/isatty.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <assert.h>
2+
#include <fcntl.h>
3+
#include <unistd.h>
4+
5+
int main(void) {
6+
const char *path = "/tmp/cpp2rust_isatty_test.tmp";
7+
int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644);
8+
assert(fd >= 0);
9+
assert(isatty(fd) == 0);
10+
assert(close(fd) == 0);
11+
assert(unlink(path) == 0);
12+
return 0;
13+
}

‎tests/unit/lseek_ftruncate.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <assert.h>
2+
#include <fcntl.h>
3+
#include <string.h>
4+
#include <unistd.h>
5+
6+
int main(void) {
7+
const char *path = "/tmp/cpp2rust_lseek_ftruncate_test.tmp";
8+
int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644);
9+
assert(fd >= 0);
10+
assert(write(fd, "hello world", 11) == 11);
11+
assert(lseek(fd, 0, SEEK_END) == 11);
12+
assert(lseek(fd, 6, SEEK_SET) == 6);
13+
char buf[16];
14+
memset(buf, 0, sizeof(buf));
15+
assert(read(fd, buf, sizeof(buf)) == 5);
16+
assert(strcmp(buf, "world") == 0);
17+
assert(ftruncate(fd, 5) == 0);
18+
assert(lseek(fd, 0, SEEK_END) == 5);
19+
assert(close(fd) == 0);
20+
assert(unlink(path) == 0);
21+
return 0;
22+
}

‎tests/unit/out/refcount/fd_io.rs‎

Lines changed: 0 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -138,129 +138,5 @@ fn main_0() -> i32 {
138138
} == 0) as i32)
139139
!= 0)
140140
);
141-
let fds: Value<Box<[i32]>> = Rc::new(RefCell::new(
142-
(0..2).map(|_| <i32>::default()).collect::<Box<[i32]>>(),
143-
));
144-
assert!(
145-
(((match nix::unistd::pipe() {
146-
Ok((__r, __w)) => {
147-
let __fds = (fds.as_pointer() as Ptr<i32>).clone();
148-
__fds.write(FdRegistry::register(__r));
149-
__fds.offset(1).write(FdRegistry::register(__w));
150-
0
151-
}
152-
Err(__e) => {
153-
libcc2rs::cpp2rust_errno().write(__e as i32);
154-
-1
155-
}
156-
} == 0) as i32)
157-
!= 0)
158-
);
159-
assert!(
160-
(((match FdRegistry::with_fd((*fds.borrow())[(1) as usize], |__fd| {
161-
Ptr::from_string_literal(b"ab")
162-
.to_any()
163-
.reinterpret_cast::<u8>()
164-
.with_slice(2_usize, |__buf| nix::unistd::write(__fd, __buf))
165-
}) {
166-
Ok(__n) => __n as isize,
167-
Err(__e) => {
168-
libcc2rs::cpp2rust_errno().write(__e as i32);
169-
-1
170-
}
171-
} == 2_isize) as i32)
172-
!= 0)
173-
);
174-
let buf2: Value<Box<[u8]>> = Rc::new(RefCell::new(
175-
(0..4).map(|_| <u8>::default()).collect::<Box<[u8]>>(),
176-
));
177-
{
178-
((buf2.as_pointer() as Ptr<u8>) as Ptr<u8>)
179-
.to_any()
180-
.memset((0) as u8, ::std::mem::size_of::<[u8; 4]>() as usize);
181-
((buf2.as_pointer() as Ptr<u8>) as Ptr<u8>).to_any().clone()
182-
};
183-
assert!(
184-
(((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| {
185-
((buf2.as_pointer() as Ptr<u8>) as Ptr<u8>)
186-
.to_any()
187-
.reinterpret_cast::<u8>()
188-
.with_slice_mut(::std::mem::size_of::<[u8; 4]>(), |__buf| {
189-
nix::unistd::read(__fd, __buf)
190-
})
191-
}) {
192-
Ok(__n) => __n as isize,
193-
Err(__e) => {
194-
libcc2rs::cpp2rust_errno().write(__e as i32);
195-
-1
196-
}
197-
} == 2_isize) as i32)
198-
!= 0)
199-
);
200-
assert!(
201-
((({
202-
let mut __it1 = (buf2.as_pointer() as Ptr<u8>).to_c_string_iterator();
203-
let mut __it2 = Ptr::from_string_literal(b"ab").to_c_string_iterator();
204-
loop {
205-
let __c1 = __it1.next();
206-
let __c2 = __it2.next();
207-
if __c1 != __c2 {
208-
break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32);
209-
}
210-
if __c1.is_none() {
211-
break 0;
212-
}
213-
}
214-
} == 0) as i32)
215-
!= 0)
216-
);
217-
assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0));
218-
assert!(
219-
(((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| {
220-
((buf2.as_pointer() as Ptr<u8>) as Ptr<u8>)
221-
.to_any()
222-
.reinterpret_cast::<u8>()
223-
.with_slice_mut(::std::mem::size_of::<[u8; 4]>(), |__buf| {
224-
nix::unistd::read(__fd, __buf)
225-
})
226-
}) {
227-
Ok(__n) => __n as isize,
228-
Err(__e) => {
229-
libcc2rs::cpp2rust_errno().write(__e as i32);
230-
-1
231-
}
232-
} == 0_isize) as i32)
233-
!= 0)
234-
);
235-
assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0));
236-
let s: Value<i32> = Rc::new(RefCell::new({
237-
let __family = match libc::AF_INET {
238-
::libc::AF_INET => nix::sys::socket::AddressFamily::Inet,
239-
::libc::AF_INET6 => nix::sys::socket::AddressFamily::Inet6,
240-
::libc::AF_UNIX => nix::sys::socket::AddressFamily::Unix,
241-
__d => panic!("socket: unsupported domain {__d}"),
242-
};
243-
let __flags = nix::sys::socket::SockFlag::from_bits_truncate(libc::SOCK_STREAM);
244-
let __ty = match libc::SOCK_STREAM & !nix::sys::socket::SockFlag::all().bits() {
245-
::libc::SOCK_STREAM => nix::sys::socket::SockType::Stream,
246-
::libc::SOCK_DGRAM => nix::sys::socket::SockType::Datagram,
247-
__t => panic!("socket: unsupported type {__t}"),
248-
};
249-
let __proto = match 0 {
250-
0 => None,
251-
::libc::IPPROTO_TCP => Some(nix::sys::socket::SockProtocol::Tcp),
252-
::libc::IPPROTO_UDP => Some(nix::sys::socket::SockProtocol::Udp),
253-
__p => panic!("socket: unsupported protocol {__p}"),
254-
};
255-
match nix::sys::socket::socket(__family, __ty, __flags, __proto) {
256-
Ok(__ofd) => FdRegistry::register(__ofd),
257-
Err(__e) => {
258-
libcc2rs::cpp2rust_errno().write(__e as i32);
259-
-1
260-
}
261-
}
262-
}));
263-
assert!(((((*s.borrow()) >= 0) as i32) != 0));
264-
assert!((((FdRegistry::close((*s.borrow())) == 0) as i32) != 0));
265141
return 0;
266142
}

‎tests/unit/out/refcount/isatty.rs‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn main() {
10+
std::process::exit(main_0());
11+
}
12+
fn main_0() -> i32 {
13+
let path: Value<Ptr<u8>> = Rc::new(RefCell::new(Ptr::from_string_literal(
14+
b"/tmp/cpp2rust_isatty_test.tmp",
15+
)));
16+
let fd: Value<i32> = Rc::new(RefCell::new({
17+
let __mode = match &[(420).into()].first() {
18+
Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t),
19+
None => nix::sys::stat::Mode::empty(),
20+
};
21+
match nix::fcntl::open(
22+
(*path.borrow()).to_rust_string().as_str(),
23+
nix::fcntl::OFlag::from_bits_retain(
24+
((::libc::O_RDWR | ::libc::O_CREAT) | ::libc::O_TRUNC),
25+
),
26+
__mode,
27+
) {
28+
Ok(__ofd) => FdRegistry::register(__ofd),
29+
Err(__e) => {
30+
libcc2rs::cpp2rust_errno().write(__e as i32);
31+
-1
32+
}
33+
}
34+
}));
35+
assert!(((((*fd.borrow()) >= 0) as i32) != 0));
36+
assert!(
37+
(((match FdRegistry::with_fd((*fd.borrow()), |__fd| nix::unistd::isatty(__fd)) {
38+
Ok(__tty) => __tty as i32,
39+
Err(__e) => {
40+
libcc2rs::cpp2rust_errno().write(__e as i32);
41+
0
42+
}
43+
} == 0) as i32)
44+
!= 0)
45+
);
46+
assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0));
47+
assert!(
48+
(((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) {
49+
Ok(()) => 0,
50+
Err(__e) => {
51+
libcc2rs::cpp2rust_errno().write(__e as i32);
52+
-1
53+
}
54+
} == 0) as i32)
55+
!= 0)
56+
);
57+
return 0;
58+
}

0 commit comments

Comments
 (0)