Skip to content

Commit ea698aa

Browse files
committed
Add unistd safe rules
1 parent a50ac5a commit ea698aa

9 files changed

Lines changed: 85 additions & 10 deletions

File tree

libc-dep/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ edition = "2024"
55

66
[dependencies]
77
libc = "0.2"
8+
nix = { version = "0.30.1", features = ["socket", "net", "fs", "poll", "time", "user", "dir", "term", "event", "hostname"] }

libcc2rs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ edition = "2024"
66
[dependencies]
77
libcc2rs-macros = { path = "../libcc2rs-macros", version = "0.1.0" }
88
libc = "0.2"
9+
nix = { version = "0.30.1", features = ["socket", "net", "fs", "poll", "time", "user", "dir", "term", "event", "hostname"] }

rule-preprocessor/src/semantic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn build_rustc_args(crate_root: &Path) -> Vec<String> {
4646
args.push("-L".to_string());
4747
args.push(format!("dependency={}", deps.display()));
4848

49-
for dep in &["libcc2rs", "libc", "brotli_sys", "rustls_ffi"] {
49+
for dep in &["libcc2rs", "libc", "brotli_sys", "rustls_ffi", "nix"] {
5050
if let Some(rlib) = find_rlib(deps.as_path(), dep) {
5151
args.push("--extern".to_string());
5252
args.push(format!("{}={}", dep, rlib.display()));

rules/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ libc = "0.2.148"
1313
libcc2rs = { version = "0.1.0", path = "../libcc2rs" }
1414
brotli-sys = "0.3"
1515
rustls-ffi = { version = "0.15.3", default-features = false }
16+
nix = { version = "0.30.1", features = ["socket", "net", "fs", "poll", "time", "user", "dir", "term", "event", "hostname"] }

rules/src/modules.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ pub mod time_tgt_unsafe;
144144
pub mod unique_ptr_tgt_refcount;
145145
#[path = r#"../unique_ptr/tgt_unsafe.rs"#]
146146
pub mod unique_ptr_tgt_unsafe;
147+
#[path = r#"../unistd/tgt_refcount.rs"#]
148+
pub mod unistd_tgt_refcount;
147149
#[path = r#"../unistd/tgt_unsafe.rs"#]
148150
pub mod unistd_tgt_unsafe;
149151
#[path = r#"../vector/tgt_refcount.rs"#]

rules/unistd/tgt_refcount.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
fn f4(a0: Ptr<u8>) -> i32 {
7+
match nix::unistd::unlink(
8+
::std::ffi::CString::new(a0.to_c_string_iterator().collect::<Vec<u8>>())
9+
.unwrap()
10+
.as_c_str(),
11+
) {
12+
Ok(()) => 0,
13+
Err(__e) => {
14+
libcc2rs::cpp2rust_errno().write(__e as i32);
15+
-1
16+
}
17+
}
18+
}
19+
20+
fn f8() -> u32 {
21+
nix::unistd::geteuid().as_raw()
22+
}
23+
24+
fn f9(a0: Ptr<u8>, a1: usize) -> i32 {
25+
match nix::unistd::gethostname() {
26+
Ok(__name) => {
27+
let __bytes = __name.as_encoded_bytes();
28+
let __n = __bytes.len().min(a1.saturating_sub(1));
29+
let mut __dst = a0.clone();
30+
for __b in &__bytes[..__n] {
31+
__dst.write(*__b);
32+
__dst += 1;
33+
}
34+
if a1 > 0 {
35+
__dst.write(0);
36+
}
37+
0
38+
}
39+
Err(__e) => {
40+
libcc2rs::cpp2rust_errno().write(__e as i32);
41+
-1
42+
}
43+
}
44+
}
45+
46+
fn f11(a0: Ptr<u8>) -> i32 {
47+
match ::std::fs::remove_dir(a0.to_rust_string()) {
48+
Ok(()) => 0,
49+
Err(__e) => {
50+
libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO));
51+
-1
52+
}
53+
}
54+
}

tests/lit/lit/formats/Cpp2RustTest.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,14 @@ def build_rust(self):
202202

203203
parent = Path(__file__).resolve().parent.parent.parent.parent.parent
204204
cc2rs_dir = parent / "libcc2rs" / "target" / "release"
205+
libc_dep_deps = parent / "libc-dep" / "target" / "release" / "deps"
205206
# pick the most recently compiled libc
206207
libc_rlib = max(
207-
(parent / "libc-dep" / "target" / "release" / "deps").glob(
208-
"liblibc-*.rlib"
209-
),
208+
libc_dep_deps.glob("liblibc-*.rlib"),
209+
key=lambda p: p.stat().st_mtime,
210+
)
211+
nix_rlib = max(
212+
libc_dep_deps.glob("libnix-*.rlib"),
210213
key=lambda p: p.stat().st_mtime,
211214
)
212215
cmd = [
@@ -229,10 +232,14 @@ def build_rust(self):
229232
str(self.tmp_dir),
230233
"-L",
231234
f"dependency={cc2rs_dir / 'deps'}",
235+
"-L",
236+
f"dependency={libc_dep_deps}",
232237
"--extern",
233238
f"libcc2rs={cc2rs_dir / 'liblibcc2rs.rlib'}",
234239
"--extern",
235240
f"libc={libc_rlib}",
241+
"--extern",
242+
f"nix={nix_rlib}",
236243
]
237244
_, err, returncode = lit.util.executeCommand(cmd, str(self.tmp_dir))
238245
if exp.should_not_compile:

tests/unit/out/refcount/stdcopy_ostream.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ fn main_0() -> i32 {
2929
);
3030
(*ofs.borrow_mut()).try_clone().unwrap()
3131
};
32-
libc::unlink((file.as_pointer() as Ptr<u8>));
32+
match nix::unistd::unlink(
33+
::std::ffi::CString::new(
34+
(file.as_pointer() as Ptr<u8>)
35+
.to_c_string_iterator()
36+
.collect::<Vec<u8>>(),
37+
)
38+
.unwrap()
39+
.as_c_str(),
40+
) {
41+
Ok(()) => 0,
42+
Err(__e) => {
43+
libcc2rs::cpp2rust_errno().write(__e as i32);
44+
-1
45+
}
46+
};
3347
return 0;
3448
}

tests/unit/stdcopy_ostream.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
// Copyright (c) 2022-present INESC-ID.
2-
// Distributed under the MIT license that can be found in the LICENSE file.
3-
4-
// XFAIL: refcount
5-
61
#include <algorithm>
72
#include <fstream>
83
#include <iterator>

0 commit comments

Comments
 (0)