diff --git a/libc-dep/Cargo.toml b/libc-dep/Cargo.toml index 28cb84c3..fefb8b74 100644 --- a/libc-dep/Cargo.toml +++ b/libc-dep/Cargo.toml @@ -5,3 +5,4 @@ edition = "2024" [dependencies] libc = "0.2" +nix = { version = "0.30", features = ["socket", "net", "fs", "poll", "time", "user", "dir", "term", "event", "hostname"] } diff --git a/libcc2rs/Cargo.toml b/libcc2rs/Cargo.toml index 6221433a..5e10915b 100644 --- a/libcc2rs/Cargo.toml +++ b/libcc2rs/Cargo.toml @@ -6,3 +6,4 @@ edition = "2024" [dependencies] libcc2rs-macros = { path = "../libcc2rs-macros", version = "0.1.0" } libc = "0.2" +nix = { version = "0.30", features = ["socket", "net", "fs", "poll", "time", "user", "dir", "term", "event", "hostname"] } diff --git a/rule-preprocessor/src/semantic.rs b/rule-preprocessor/src/semantic.rs index 77a12f5c..f9030457 100644 --- a/rule-preprocessor/src/semantic.rs +++ b/rule-preprocessor/src/semantic.rs @@ -46,7 +46,7 @@ fn build_rustc_args(crate_root: &Path) -> Vec { args.push("-L".to_string()); args.push(format!("dependency={}", deps.display())); - for dep in &["libcc2rs", "libc", "brotli_sys", "rustls_ffi"] { + for dep in &["libcc2rs", "libc", "brotli_sys", "rustls_ffi", "nix"] { if let Some(rlib) = find_rlib(deps.as_path(), dep) { args.push("--extern".to_string()); args.push(format!("{}={}", dep, rlib.display())); diff --git a/rules/Cargo.toml b/rules/Cargo.toml index c36f8bf0..d9a5e1be 100644 --- a/rules/Cargo.toml +++ b/rules/Cargo.toml @@ -13,3 +13,4 @@ libc = "0.2.148" libcc2rs = { version = "0.1.0", path = "../libcc2rs" } brotli-sys = "0.3" rustls-ffi = { version = "0.15.3", default-features = false } +nix = { version = "0.30", features = ["socket", "net", "fs", "poll", "time", "user", "dir", "term", "event", "hostname"] } diff --git a/rules/src/modules.rs b/rules/src/modules.rs index e1a53cdd..65dbeae7 100644 --- a/rules/src/modules.rs +++ b/rules/src/modules.rs @@ -144,6 +144,8 @@ pub mod time_tgt_unsafe; pub mod unique_ptr_tgt_refcount; #[path = r#"../unique_ptr/tgt_unsafe.rs"#] pub mod unique_ptr_tgt_unsafe; +#[path = r#"../unistd/tgt_refcount.rs"#] +pub mod unistd_tgt_refcount; #[path = r#"../unistd/tgt_unsafe.rs"#] pub mod unistd_tgt_unsafe; #[path = r#"../vector/tgt_refcount.rs"#] diff --git a/rules/unistd/tgt_refcount.rs b/rules/unistd/tgt_refcount.rs new file mode 100644 index 00000000..c455ed6f --- /dev/null +++ b/rules/unistd/tgt_refcount.rs @@ -0,0 +1,50 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +use libcc2rs::*; + +fn f4(a0: Ptr) -> i32 { + match nix::unistd::unlink(a0.to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } +} + +fn f8() -> u32 { + nix::unistd::geteuid().as_raw() +} + +fn f9(a0: Ptr, a1: usize) -> i32 { + match nix::unistd::gethostname() { + Ok(__name) => { + let __bytes = __name.as_encoded_bytes(); + let __n = __bytes.len().min(a1.saturating_sub(1)); + let mut __dst = a0.clone(); + for __b in &__bytes[..__n] { + __dst.write(*__b); + __dst += 1; + } + if a1 > 0 { + __dst.write(0); + } + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } +} + +fn f11(a0: Ptr) -> i32 { + match ::std::fs::remove_dir(a0.to_rust_string()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + -1 + } + } +} diff --git a/tests/lit/lit/formats/Cpp2RustTest.py b/tests/lit/lit/formats/Cpp2RustTest.py index 5b8ae0bb..89465616 100644 --- a/tests/lit/lit/formats/Cpp2RustTest.py +++ b/tests/lit/lit/formats/Cpp2RustTest.py @@ -202,11 +202,14 @@ def build_rust(self): parent = Path(__file__).resolve().parent.parent.parent.parent.parent cc2rs_dir = parent / "libcc2rs" / "target" / "release" + libc_dep_deps = parent / "libc-dep" / "target" / "release" / "deps" # pick the most recently compiled libc libc_rlib = max( - (parent / "libc-dep" / "target" / "release" / "deps").glob( - "liblibc-*.rlib" - ), + libc_dep_deps.glob("liblibc-*.rlib"), + key=lambda p: p.stat().st_mtime, + ) + nix_rlib = max( + libc_dep_deps.glob("libnix-*.rlib"), key=lambda p: p.stat().st_mtime, ) cmd = [ @@ -229,10 +232,14 @@ def build_rust(self): str(self.tmp_dir), "-L", f"dependency={cc2rs_dir / 'deps'}", + "-L", + f"dependency={libc_dep_deps}", "--extern", f"libcc2rs={cc2rs_dir / 'liblibcc2rs.rlib'}", "--extern", f"libc={libc_rlib}", + "--extern", + f"nix={nix_rlib}", ] _, err, returncode = lit.util.executeCommand(cmd, str(self.tmp_dir)) if exp.should_not_compile: diff --git a/tests/unit/out/refcount/stdcopy_ostream.rs b/tests/unit/out/refcount/stdcopy_ostream.rs index abb70823..9ae7dd16 100644 --- a/tests/unit/out/refcount/stdcopy_ostream.rs +++ b/tests/unit/out/refcount/stdcopy_ostream.rs @@ -29,6 +29,12 @@ fn main_0() -> i32 { ); (*ofs.borrow_mut()).try_clone().unwrap() }; - libc::unlink((file.as_pointer() as Ptr)); + match nix::unistd::unlink((file.as_pointer() as Ptr).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + }; return 0; } diff --git a/tests/unit/stdcopy_ostream.cpp b/tests/unit/stdcopy_ostream.cpp index 81a469f6..523188cf 100644 --- a/tests/unit/stdcopy_ostream.cpp +++ b/tests/unit/stdcopy_ostream.cpp @@ -1,8 +1,3 @@ -// Copyright (c) 2022-present INESC-ID. -// Distributed under the MIT license that can be found in the LICENSE file. - -// XFAIL: refcount - #include #include #include