Skip to content

Commit 7ddcee8

Browse files
committed
Add safe rules for lseek,ftruncate,isatty,listen
1 parent a30d60b commit 7ddcee8

5 files changed

Lines changed: 485 additions & 0 deletions

File tree

‎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
@@ -141,3 +141,19 @@ fn f10(a0: i32, a1: AnyPtr, a2: usize, a3: i32) -> isize {
141141
}
142142
}
143143
}
144+
145+
fn f17(a0: i32, a1: i32) -> i32 {
146+
match nix::sys::socket::Backlog::new(a1) {
147+
Ok(__b) => match FdRegistry::with_fd(a0, |__fd| nix::sys::socket::listen(&__fd, __b)) {
148+
Ok(()) => 0,
149+
Err(__e) => {
150+
libcc2rs::cpp2rust_errno().write(__e as i32);
151+
-1
152+
}
153+
},
154+
Err(__e) => {
155+
libcc2rs::cpp2rust_errno().write(__e as i32);
156+
-1
157+
}
158+
}
159+
}

‎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>()
@@ -30,6 +46,26 @@ fn f4(a0: Ptr<u8>) -> i32 {
3046
}
3147
}
3248

49+
fn f6(a0: i32, a1: i64) -> i32 {
50+
match FdRegistry::with_fd(a0, |__fd| nix::unistd::ftruncate(__fd, a1)) {
51+
Ok(()) => 0,
52+
Err(__e) => {
53+
libcc2rs::cpp2rust_errno().write(__e as i32);
54+
-1
55+
}
56+
}
57+
}
58+
59+
fn f7(a0: i32) -> i32 {
60+
match FdRegistry::with_fd(a0, |__fd| nix::unistd::isatty(__fd)) {
61+
Ok(__tty) => __tty as i32,
62+
Err(__e) => {
63+
libcc2rs::cpp2rust_errno().write(__e as i32);
64+
0
65+
}
66+
}
67+
}
68+
3369
fn f8() -> u32 {
3470
nix::unistd::geteuid().as_raw()
3571
}

0 commit comments

Comments
 (0)