Skip to content

Commit adab7fe

Browse files
authored
Add unistd safe rules (#259)
1 parent a50ac5a commit adab7fe

9 files changed

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

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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ 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((file.as_pointer() as Ptr<u8>).to_rust_string().as_str()) {
33+
Ok(()) => 0,
34+
Err(__e) => {
35+
libcc2rs::cpp2rust_errno().write(__e as i32);
36+
-1
37+
}
38+
};
3339
return 0;
3440
}

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)