Skip to content

Commit 527dea1

Browse files
committed
Add safe rules for lseek,ftruncate,isatty,listen
1 parent 0cadc91 commit 527dea1

6 files changed

Lines changed: 487 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
@@ -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
}

0 commit comments

Comments
 (0)