From 4dbf89f2e70a054e3c2ebc7319553434fc71f8e0 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 19 May 2026 14:03:24 +0100 Subject: [PATCH 1/8] Add termios rules --- rules/termios/ir_unsafe.json | 88 ++++++++++++++++++++++++++++++++ rules/termios/src.cpp | 10 ++++ rules/termios/tgt_unsafe.rs | 10 ++++ tests/unit/out/unsafe/termios.rs | 43 ++++++++++++++++ tests/unit/termios.c | 28 ++++++++++ 5 files changed, 179 insertions(+) create mode 100644 rules/termios/ir_unsafe.json create mode 100644 rules/termios/src.cpp create mode 100644 rules/termios/tgt_unsafe.rs create mode 100644 tests/unit/out/unsafe/termios.rs create mode 100644 tests/unit/termios.c diff --git a/rules/termios/ir_unsafe.json b/rules/termios/ir_unsafe.json new file mode 100644 index 0000000..8a24240 --- /dev/null +++ b/rules/termios/ir_unsafe.json @@ -0,0 +1,88 @@ +{ + "f1": { + "body": [ + { + "text": "libc::tcsetattr(" + }, + { + "placeholder": { + "arg": 0, + "access": "read" + } + }, + { + "text": ", " + }, + { + "placeholder": { + "arg": 1, + "access": "read" + } + }, + { + "text": ", " + }, + { + "placeholder": { + "arg": 2, + "access": "read" + } + }, + { + "text": ")" + } + ], + "params": { + "a0": { + "type": "i32" + }, + "a1": { + "type": "i32" + }, + "a2": { + "type": "*const ::libc::termios", + "is_unsafe_pointer": true + } + }, + "return_type": { + "type": "i32" + } + }, + "f2": { + "body": [ + { + "text": "libc::tcgetattr(" + }, + { + "placeholder": { + "arg": 0, + "access": "read" + } + }, + { + "text": ", " + }, + { + "placeholder": { + "arg": 1, + "access": "read" + } + }, + { + "text": ")" + } + ], + "params": { + "a0": { + "type": "i32" + }, + "a1": { + "type": "*mut ::libc::termios", + "is_unsafe_pointer": true + } + }, + "return_type": { + "type": "i32" + } + } +} diff --git a/rules/termios/src.cpp b/rules/termios/src.cpp new file mode 100644 index 0000000..32f014b --- /dev/null +++ b/rules/termios/src.cpp @@ -0,0 +1,10 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +#include + +int f1(int fd, int optional_actions, const struct termios *termios_p) { + return tcsetattr(fd, optional_actions, termios_p); +} + +int f2(int fd, struct termios *termios_p) { return tcgetattr(fd, termios_p); } diff --git a/rules/termios/tgt_unsafe.rs b/rules/termios/tgt_unsafe.rs new file mode 100644 index 0000000..1d40338 --- /dev/null +++ b/rules/termios/tgt_unsafe.rs @@ -0,0 +1,10 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +unsafe fn f1(a0: i32, a1: i32, a2: *const ::libc::termios) -> i32 { + libc::tcsetattr(a0, a1, a2) +} + +unsafe fn f2(a0: i32, a1: *mut ::libc::termios) -> i32 { + libc::tcgetattr(a0, a1) +} diff --git a/tests/unit/out/unsafe/termios.rs b/tests/unit/out/unsafe/termios.rs new file mode 100644 index 0000000..4e502d7 --- /dev/null +++ b/tests/unit/out/unsafe/termios.rs @@ -0,0 +1,43 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub unsafe fn test_tcgetattr_0() { + let mut t: termios = std::mem::zeroed::(); + (*libc::__errno_location()) = 0; + assert!(((((libc::tcgetattr(0, (&mut t as *mut termios))) == (-1_i32)) as i32) != 0)); + assert!(((((*libc::__errno_location()) == (25)) as i32) != 0)); + (*libc::__errno_location()) = 0; +} +pub unsafe fn test_tcsetattr_1() { + let mut t: termios = std::mem::zeroed::(); + { + let byte_0 = ((&mut t as *mut termios) as *mut termios as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::() as u64 { + *byte_0.offset(offset as isize) = 0 as u8; + } + ((&mut t as *mut termios) as *mut termios as *mut ::libc::c_void) + }; + (*libc::__errno_location()) = 0; + assert!( + ((((libc::tcsetattr(-1_i32, 0, (&mut t as *mut termios).cast_const())) == (-1_i32)) + as i32) + != 0) + ); + assert!(((((*libc::__errno_location()) == (9)) as i32) != 0)); + (*libc::__errno_location()) = 0; +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_tcgetattr_0() }); + (unsafe { test_tcsetattr_1() }); + return 0; +} diff --git a/tests/unit/termios.c b/tests/unit/termios.c new file mode 100644 index 0000000..e2aaa3a --- /dev/null +++ b/tests/unit/termios.c @@ -0,0 +1,28 @@ +// no-compile: refcount +#include +#include +#include +#include + +static void test_tcgetattr(void) { + struct termios t; + errno = 0; + assert(tcgetattr(0, &t) == -1); + assert(errno == ENOTTY); + errno = 0; +} + +static void test_tcsetattr(void) { + struct termios t; + memset(&t, 0, sizeof(t)); + errno = 0; + assert(tcsetattr(-1, TCSANOW, &t) == -1); + assert(errno == EBADF); + errno = 0; +} + +int main(void) { + test_tcgetattr(); + test_tcsetattr(); + return 0; +} From 29e5f2cd20c46b07e373d39e8a46e948789e39cb Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 19 May 2026 14:05:10 +0100 Subject: [PATCH 2/8] Add signal rules --- rules/signal/ir_unsafe.json | 52 +++++++++++++++++++++++++++++++++ rules/signal/src.cpp | 8 +++++ rules/signal/tgt_unsafe.rs | 10 +++++++ tests/unit/out/unsafe/signal.rs | 36 +++++++++++++++++++++++ tests/unit/signal.c | 20 +++++++++++++ 5 files changed, 126 insertions(+) create mode 100644 rules/signal/ir_unsafe.json create mode 100644 rules/signal/src.cpp create mode 100644 rules/signal/tgt_unsafe.rs create mode 100644 tests/unit/out/unsafe/signal.rs create mode 100644 tests/unit/signal.c diff --git a/rules/signal/ir_unsafe.json b/rules/signal/ir_unsafe.json new file mode 100644 index 0000000..5b2436f --- /dev/null +++ b/rules/signal/ir_unsafe.json @@ -0,0 +1,52 @@ +{ + "f1": { + "body": [ + { + "text": "libc::sigaction(" + }, + { + "placeholder": { + "arg": 0, + "access": "read" + } + }, + { + "text": ", " + }, + { + "placeholder": { + "arg": 1, + "access": "read" + } + }, + { + "text": ", " + }, + { + "placeholder": { + "arg": 2, + "access": "read" + } + }, + { + "text": ")" + } + ], + "params": { + "a0": { + "type": "i32" + }, + "a1": { + "type": "*const ::libc::sigaction", + "is_unsafe_pointer": true + }, + "a2": { + "type": "*mut ::libc::sigaction", + "is_unsafe_pointer": true + } + }, + "return_type": { + "type": "i32" + } + } +} diff --git a/rules/signal/src.cpp b/rules/signal/src.cpp new file mode 100644 index 0000000..9ff7721 --- /dev/null +++ b/rules/signal/src.cpp @@ -0,0 +1,8 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +#include + +int f1(int signum, const struct sigaction *act, struct sigaction *oldact) { + return sigaction(signum, act, oldact); +} diff --git a/rules/signal/tgt_unsafe.rs b/rules/signal/tgt_unsafe.rs new file mode 100644 index 0000000..1147fb2 --- /dev/null +++ b/rules/signal/tgt_unsafe.rs @@ -0,0 +1,10 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +unsafe fn f1( + a0: i32, + a1: *const ::libc::sigaction, + a2: *mut ::libc::sigaction, +) -> i32 { + libc::sigaction(a0, a1, a2) +} diff --git a/tests/unit/out/unsafe/signal.rs b/tests/unit/out/unsafe/signal.rs new file mode 100644 index 0000000..fa672c3 --- /dev/null +++ b/tests/unit/out/unsafe/signal.rs @@ -0,0 +1,36 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub unsafe fn test_sigaction_0() { + let mut sa: sigaction = std::mem::zeroed::(); + assert!( + ((((libc::sigaction(2, std::ptr::null(), (&mut sa as *mut sigaction))) == (0)) as i32) + != 0) + ); + assert!( + ((((libc::sigaction(15, std::ptr::null(), (&mut sa as *mut sigaction))) == (0)) as i32) + != 0) + ); + (*libc::__errno_location()) = 0; + assert!( + ((((libc::sigaction(99999, std::ptr::null(), (&mut sa as *mut sigaction))) == (-1_i32)) + as i32) + != 0) + ); + assert!(((((*libc::__errno_location()) == (22)) as i32) != 0)); + (*libc::__errno_location()) = 0; +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_sigaction_0() }); + return 0; +} diff --git a/tests/unit/signal.c b/tests/unit/signal.c new file mode 100644 index 0000000..6673f94 --- /dev/null +++ b/tests/unit/signal.c @@ -0,0 +1,20 @@ +// no-compile: refcount +#include +#include +#include +#include + +static void test_sigaction(void) { + struct sigaction sa; + assert(sigaction(SIGINT, NULL, &sa) == 0); + assert(sigaction(SIGTERM, NULL, &sa) == 0); + errno = 0; + assert(sigaction(99999, NULL, &sa) == -1); + assert(errno == EINVAL); + errno = 0; +} + +int main(void) { + test_sigaction(); + return 0; +} From aecac442fbcb4d72e4ff9a5fef4ca3c06cf5bb38 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 19 May 2026 14:07:33 +0100 Subject: [PATCH 3/8] Add pwd rules --- rules/pwd/ir_unsafe.json | 27 +++++++++++++++++++++++++++ rules/pwd/src.cpp | 6 ++++++ rules/pwd/tgt_unsafe.rs | 6 ++++++ tests/unit/out/unsafe/pwd.rs | 27 +++++++++++++++++++++++++++ tests/unit/pwd.c | 19 +++++++++++++++++++ 5 files changed, 85 insertions(+) create mode 100644 rules/pwd/ir_unsafe.json create mode 100644 rules/pwd/src.cpp create mode 100644 rules/pwd/tgt_unsafe.rs create mode 100644 tests/unit/out/unsafe/pwd.rs create mode 100644 tests/unit/pwd.c diff --git a/rules/pwd/ir_unsafe.json b/rules/pwd/ir_unsafe.json new file mode 100644 index 0000000..9350198 --- /dev/null +++ b/rules/pwd/ir_unsafe.json @@ -0,0 +1,27 @@ +{ + "f1": { + "body": [ + { + "text": "libc::getpwuid(" + }, + { + "placeholder": { + "arg": 0, + "access": "read" + } + }, + { + "text": ")" + } + ], + "params": { + "a0": { + "type": "u32" + } + }, + "return_type": { + "type": "*mut ::libc::passwd", + "is_unsafe_pointer": true + } + } +} diff --git a/rules/pwd/src.cpp b/rules/pwd/src.cpp new file mode 100644 index 0000000..5836bac --- /dev/null +++ b/rules/pwd/src.cpp @@ -0,0 +1,6 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +#include + +struct passwd *f1(uid_t uid) { return getpwuid(uid); } diff --git a/rules/pwd/tgt_unsafe.rs b/rules/pwd/tgt_unsafe.rs new file mode 100644 index 0000000..275499a --- /dev/null +++ b/rules/pwd/tgt_unsafe.rs @@ -0,0 +1,6 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +unsafe fn f1(a0: u32) -> *mut ::libc::passwd { + libc::getpwuid(a0) +} diff --git a/tests/unit/out/unsafe/pwd.rs b/tests/unit/out/unsafe/pwd.rs new file mode 100644 index 0000000..edbbf85 --- /dev/null +++ b/tests/unit/out/unsafe/pwd.rs @@ -0,0 +1,27 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub unsafe fn test_getpwuid_0() { + let mut uid: u32 = libc::geteuid(); + let mut pw: *mut passwd = libc::getpwuid(uid); + assert!((((!((pw).is_null())) as i32) != 0)); + assert!((((((*pw).pw_uid) == (uid)) as i32) != 0)); + printf( + (b"%s\n\0".as_ptr().cast_mut()).cast_const() as *const i8, + (*pw).pw_name, + ); +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_getpwuid_0() }); + return 0; +} diff --git a/tests/unit/pwd.c b/tests/unit/pwd.c new file mode 100644 index 0000000..6b575c0 --- /dev/null +++ b/tests/unit/pwd.c @@ -0,0 +1,19 @@ +// no-compile: refcount +#include +#include +#include +#include +#include + +static void test_getpwuid(void) { + uid_t uid = geteuid(); + struct passwd *pw = getpwuid(uid); + assert(pw != NULL); + assert(pw->pw_uid == uid); + printf("%s\n", pw->pw_name); +} + +int main(void) { + test_getpwuid(); + return 0; +} From 164471fea3707a96d80469a24cf493adad9e6c6d Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 19 May 2026 14:12:00 +0100 Subject: [PATCH 4/8] Add rules for netdb --- rules/netdb/ir_unsafe.json | 88 ++++++++++++++++++++++++++++++++++++++ rules/netdb/src.cpp | 11 +++++ rules/netdb/tgt_unsafe.rs | 15 +++++++ tests/unit/netdb.c | 25 +++++++++++ 4 files changed, 139 insertions(+) create mode 100644 rules/netdb/ir_unsafe.json create mode 100644 rules/netdb/src.cpp create mode 100644 rules/netdb/tgt_unsafe.rs create mode 100644 tests/unit/netdb.c diff --git a/rules/netdb/ir_unsafe.json b/rules/netdb/ir_unsafe.json new file mode 100644 index 0000000..8b4897a --- /dev/null +++ b/rules/netdb/ir_unsafe.json @@ -0,0 +1,88 @@ +{ + "f1": { + "body": [ + { + "text": "libc::getaddrinfo(" + }, + { + "placeholder": { + "arg": 0, + "access": "read" + } + }, + { + "text": " as *const i8, " + }, + { + "placeholder": { + "arg": 1, + "access": "read" + } + }, + { + "text": " as *const i8, " + }, + { + "placeholder": { + "arg": 2, + "access": "read" + } + }, + { + "text": ", " + }, + { + "placeholder": { + "arg": 3, + "access": "read" + } + }, + { + "text": ")" + } + ], + "params": { + "a0": { + "type": "*const u8", + "is_unsafe_pointer": true + }, + "a1": { + "type": "*const u8", + "is_unsafe_pointer": true + }, + "a2": { + "type": "*const ::libc::addrinfo", + "is_unsafe_pointer": true + }, + "a3": { + "type": "*mut *mut ::libc::addrinfo", + "is_unsafe_pointer": true + } + }, + "return_type": { + "type": "i32" + } + }, + "f2": { + "body": [ + { + "text": "libc::freeaddrinfo(" + }, + { + "placeholder": { + "arg": 0, + "access": "read" + } + }, + { + "text": ")" + } + ], + "params": { + "a0": { + "type": "*mut ::libc::addrinfo", + "is_unsafe_pointer": true + } + } + } +} diff --git a/rules/netdb/src.cpp b/rules/netdb/src.cpp new file mode 100644 index 0000000..be8e42c --- /dev/null +++ b/rules/netdb/src.cpp @@ -0,0 +1,11 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +#include + +int f1(const char *node, const char *service, const struct addrinfo *hints, + struct addrinfo **res) { + return getaddrinfo(node, service, hints, res); +} + +void f2(struct addrinfo *res) { return freeaddrinfo(res); } diff --git a/rules/netdb/tgt_unsafe.rs b/rules/netdb/tgt_unsafe.rs new file mode 100644 index 0000000..6e40b3d --- /dev/null +++ b/rules/netdb/tgt_unsafe.rs @@ -0,0 +1,15 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +unsafe fn f1( + a0: *const u8, + a1: *const u8, + a2: *const ::libc::addrinfo, + a3: *mut *mut ::libc::addrinfo, +) -> i32 { + libc::getaddrinfo(a0 as *const i8, a1 as *const i8, a2, a3) +} + +unsafe fn f2(a0: *mut ::libc::addrinfo) { + libc::freeaddrinfo(a0) +} diff --git a/tests/unit/netdb.c b/tests/unit/netdb.c new file mode 100644 index 0000000..7c417f5 --- /dev/null +++ b/tests/unit/netdb.c @@ -0,0 +1,25 @@ +// no-compile: refcount +#include +#include +#include +#include +#include + +static void test_getaddrinfo(void) { + struct addrinfo hints; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_NUMERICHOST; + struct addrinfo *res = NULL; + assert(getaddrinfo("127.0.0.1", "80", &hints, &res) == 0); + assert(res != NULL); + assert(res->ai_family == AF_INET); + assert(res->ai_socktype == SOCK_STREAM); + freeaddrinfo(res); +} + +int main(void) { + test_getaddrinfo(); + return 0; +} From 2f533418ed2f8447ee926e170860a943c2a2597a Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 19 May 2026 14:12:28 +0100 Subject: [PATCH 5/8] Add rules for locale and netdb --- rules/locale/ir_unsafe.json | 40 +++++++++++++++++++++++++++++ rules/locale/src.cpp | 8 ++++++ rules/locale/tgt_unsafe.rs | 6 +++++ tests/unit/locale.c | 16 ++++++++++++ tests/unit/out/unsafe/locale.rs | 28 ++++++++++++++++++++ tests/unit/out/unsafe/netdb.rs | 45 +++++++++++++++++++++++++++++++++ 6 files changed, 143 insertions(+) create mode 100644 rules/locale/ir_unsafe.json create mode 100644 rules/locale/src.cpp create mode 100644 rules/locale/tgt_unsafe.rs create mode 100644 tests/unit/locale.c create mode 100644 tests/unit/out/unsafe/locale.rs create mode 100644 tests/unit/out/unsafe/netdb.rs diff --git a/rules/locale/ir_unsafe.json b/rules/locale/ir_unsafe.json new file mode 100644 index 0000000..09a799e --- /dev/null +++ b/rules/locale/ir_unsafe.json @@ -0,0 +1,40 @@ +{ + "f1": { + "body": [ + { + "text": "libc::setlocale(" + }, + { + "placeholder": { + "arg": 0, + "access": "read" + } + }, + { + "text": ", " + }, + { + "placeholder": { + "arg": 1, + "access": "read" + } + }, + { + "text": " as *const i8) as *mut u8" + } + ], + "params": { + "a0": { + "type": "i32" + }, + "a1": { + "type": "*const u8", + "is_unsafe_pointer": true + } + }, + "return_type": { + "type": "*mut u8", + "is_unsafe_pointer": true + } + } +} diff --git a/rules/locale/src.cpp b/rules/locale/src.cpp new file mode 100644 index 0000000..188a39e --- /dev/null +++ b/rules/locale/src.cpp @@ -0,0 +1,8 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +#include + +char *f1(int category, const char *locale) { + return setlocale(category, locale); +} diff --git a/rules/locale/tgt_unsafe.rs b/rules/locale/tgt_unsafe.rs new file mode 100644 index 0000000..53d2d1a --- /dev/null +++ b/rules/locale/tgt_unsafe.rs @@ -0,0 +1,6 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +unsafe fn f1(a0: i32, a1: *const u8) -> *mut u8 { + libc::setlocale(a0, a1 as *const i8) as *mut u8 +} diff --git a/tests/unit/locale.c b/tests/unit/locale.c new file mode 100644 index 0000000..a6851d9 --- /dev/null +++ b/tests/unit/locale.c @@ -0,0 +1,16 @@ +// no-compile: refcount +#include +#include +#include +#include + +static void test_setlocale(void) { + char *cur = setlocale(LC_ALL, NULL); + assert(cur != NULL); + assert(strcmp(cur, "C") == 0); +} + +int main(void) { + test_setlocale(); + return 0; +} diff --git a/tests/unit/out/unsafe/locale.rs b/tests/unit/out/unsafe/locale.rs new file mode 100644 index 0000000..cd67570 --- /dev/null +++ b/tests/unit/out/unsafe/locale.rs @@ -0,0 +1,28 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub unsafe fn test_setlocale_0() { + let mut cur: *mut u8 = libc::setlocale(6, std::ptr::null() as *const i8) as *mut u8; + assert!((((!((cur).is_null())) as i32) != 0)); + assert!( + ((((libc::strcmp( + (cur).cast_const() as *const i8, + (b"C\0".as_ptr().cast_mut()).cast_const() as *const i8 + )) == (0)) as i32) + != 0) + ); +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_setlocale_0() }); + return 0; +} diff --git a/tests/unit/out/unsafe/netdb.rs b/tests/unit/out/unsafe/netdb.rs new file mode 100644 index 0000000..110c695 --- /dev/null +++ b/tests/unit/out/unsafe/netdb.rs @@ -0,0 +1,45 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub unsafe fn test_getaddrinfo_0() { + let mut hints: addrinfo = std::mem::zeroed::(); + { + let byte_0 = + ((&mut hints as *mut addrinfo) as *mut addrinfo as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::() as u64 { + *byte_0.offset(offset as isize) = 0 as u8; + } + ((&mut hints as *mut addrinfo) as *mut addrinfo as *mut ::libc::c_void) + }; + hints.ai_family = 2; + hints.ai_socktype = libc::SOCK_STREAM; + hints.ai_flags = 4; + let mut res: *mut addrinfo = std::ptr::null_mut(); + assert!( + ((((libc::getaddrinfo( + (b"127.0.0.1\0".as_ptr().cast_mut()).cast_const() as *const i8, + (b"80\0".as_ptr().cast_mut()).cast_const() as *const i8, + (&mut hints as *mut addrinfo).cast_const(), + (&mut res as *mut *mut addrinfo) + )) == (0)) as i32) + != 0) + ); + assert!((((!((res).is_null())) as i32) != 0)); + assert!((((((*res).ai_family) == (2)) as i32) != 0)); + assert!((((((*res).ai_socktype) == (libc::SOCK_STREAM)) as i32) != 0)); + libc::freeaddrinfo(res); +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_getaddrinfo_0() }); + return 0; +} From bc60e51ac86f85862a48498401f4a666e6a73549 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 21 May 2026 09:10:52 +0100 Subject: [PATCH 6/8] Delete unportable tests --- tests/unit/locale.c | 16 ------------ tests/unit/netdb.c | 25 ------------------ tests/unit/out/unsafe/locale.rs | 28 -------------------- tests/unit/out/unsafe/netdb.rs | 45 -------------------------------- tests/unit/out/unsafe/pwd.rs | 27 ------------------- tests/unit/out/unsafe/signal.rs | 36 ------------------------- tests/unit/out/unsafe/termios.rs | 43 ------------------------------ tests/unit/pwd.c | 19 -------------- tests/unit/signal.c | 20 -------------- tests/unit/termios.c | 28 -------------------- 10 files changed, 287 deletions(-) delete mode 100644 tests/unit/locale.c delete mode 100644 tests/unit/netdb.c delete mode 100644 tests/unit/out/unsafe/locale.rs delete mode 100644 tests/unit/out/unsafe/netdb.rs delete mode 100644 tests/unit/out/unsafe/pwd.rs delete mode 100644 tests/unit/out/unsafe/signal.rs delete mode 100644 tests/unit/out/unsafe/termios.rs delete mode 100644 tests/unit/pwd.c delete mode 100644 tests/unit/signal.c delete mode 100644 tests/unit/termios.c diff --git a/tests/unit/locale.c b/tests/unit/locale.c deleted file mode 100644 index a6851d9..0000000 --- a/tests/unit/locale.c +++ /dev/null @@ -1,16 +0,0 @@ -// no-compile: refcount -#include -#include -#include -#include - -static void test_setlocale(void) { - char *cur = setlocale(LC_ALL, NULL); - assert(cur != NULL); - assert(strcmp(cur, "C") == 0); -} - -int main(void) { - test_setlocale(); - return 0; -} diff --git a/tests/unit/netdb.c b/tests/unit/netdb.c deleted file mode 100644 index 7c417f5..0000000 --- a/tests/unit/netdb.c +++ /dev/null @@ -1,25 +0,0 @@ -// no-compile: refcount -#include -#include -#include -#include -#include - -static void test_getaddrinfo(void) { - struct addrinfo hints; - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_INET; - hints.ai_socktype = SOCK_STREAM; - hints.ai_flags = AI_NUMERICHOST; - struct addrinfo *res = NULL; - assert(getaddrinfo("127.0.0.1", "80", &hints, &res) == 0); - assert(res != NULL); - assert(res->ai_family == AF_INET); - assert(res->ai_socktype == SOCK_STREAM); - freeaddrinfo(res); -} - -int main(void) { - test_getaddrinfo(); - return 0; -} diff --git a/tests/unit/out/unsafe/locale.rs b/tests/unit/out/unsafe/locale.rs deleted file mode 100644 index cd67570..0000000 --- a/tests/unit/out/unsafe/locale.rs +++ /dev/null @@ -1,28 +0,0 @@ -extern crate libc; -use libc::*; -extern crate libcc2rs; -use libcc2rs::*; -use std::collections::BTreeMap; -use std::io::{Read, Seek, Write}; -use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; -use std::rc::Rc; -pub unsafe fn test_setlocale_0() { - let mut cur: *mut u8 = libc::setlocale(6, std::ptr::null() as *const i8) as *mut u8; - assert!((((!((cur).is_null())) as i32) != 0)); - assert!( - ((((libc::strcmp( - (cur).cast_const() as *const i8, - (b"C\0".as_ptr().cast_mut()).cast_const() as *const i8 - )) == (0)) as i32) - != 0) - ); -} -pub fn main() { - unsafe { - std::process::exit(main_0() as i32); - } -} -unsafe fn main_0() -> i32 { - (unsafe { test_setlocale_0() }); - return 0; -} diff --git a/tests/unit/out/unsafe/netdb.rs b/tests/unit/out/unsafe/netdb.rs deleted file mode 100644 index 110c695..0000000 --- a/tests/unit/out/unsafe/netdb.rs +++ /dev/null @@ -1,45 +0,0 @@ -extern crate libc; -use libc::*; -extern crate libcc2rs; -use libcc2rs::*; -use std::collections::BTreeMap; -use std::io::{Read, Seek, Write}; -use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; -use std::rc::Rc; -pub unsafe fn test_getaddrinfo_0() { - let mut hints: addrinfo = std::mem::zeroed::(); - { - let byte_0 = - ((&mut hints as *mut addrinfo) as *mut addrinfo as *mut ::libc::c_void) as *mut u8; - for offset in 0..::std::mem::size_of::() as u64 { - *byte_0.offset(offset as isize) = 0 as u8; - } - ((&mut hints as *mut addrinfo) as *mut addrinfo as *mut ::libc::c_void) - }; - hints.ai_family = 2; - hints.ai_socktype = libc::SOCK_STREAM; - hints.ai_flags = 4; - let mut res: *mut addrinfo = std::ptr::null_mut(); - assert!( - ((((libc::getaddrinfo( - (b"127.0.0.1\0".as_ptr().cast_mut()).cast_const() as *const i8, - (b"80\0".as_ptr().cast_mut()).cast_const() as *const i8, - (&mut hints as *mut addrinfo).cast_const(), - (&mut res as *mut *mut addrinfo) - )) == (0)) as i32) - != 0) - ); - assert!((((!((res).is_null())) as i32) != 0)); - assert!((((((*res).ai_family) == (2)) as i32) != 0)); - assert!((((((*res).ai_socktype) == (libc::SOCK_STREAM)) as i32) != 0)); - libc::freeaddrinfo(res); -} -pub fn main() { - unsafe { - std::process::exit(main_0() as i32); - } -} -unsafe fn main_0() -> i32 { - (unsafe { test_getaddrinfo_0() }); - return 0; -} diff --git a/tests/unit/out/unsafe/pwd.rs b/tests/unit/out/unsafe/pwd.rs deleted file mode 100644 index edbbf85..0000000 --- a/tests/unit/out/unsafe/pwd.rs +++ /dev/null @@ -1,27 +0,0 @@ -extern crate libc; -use libc::*; -extern crate libcc2rs; -use libcc2rs::*; -use std::collections::BTreeMap; -use std::io::{Read, Seek, Write}; -use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; -use std::rc::Rc; -pub unsafe fn test_getpwuid_0() { - let mut uid: u32 = libc::geteuid(); - let mut pw: *mut passwd = libc::getpwuid(uid); - assert!((((!((pw).is_null())) as i32) != 0)); - assert!((((((*pw).pw_uid) == (uid)) as i32) != 0)); - printf( - (b"%s\n\0".as_ptr().cast_mut()).cast_const() as *const i8, - (*pw).pw_name, - ); -} -pub fn main() { - unsafe { - std::process::exit(main_0() as i32); - } -} -unsafe fn main_0() -> i32 { - (unsafe { test_getpwuid_0() }); - return 0; -} diff --git a/tests/unit/out/unsafe/signal.rs b/tests/unit/out/unsafe/signal.rs deleted file mode 100644 index fa672c3..0000000 --- a/tests/unit/out/unsafe/signal.rs +++ /dev/null @@ -1,36 +0,0 @@ -extern crate libc; -use libc::*; -extern crate libcc2rs; -use libcc2rs::*; -use std::collections::BTreeMap; -use std::io::{Read, Seek, Write}; -use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; -use std::rc::Rc; -pub unsafe fn test_sigaction_0() { - let mut sa: sigaction = std::mem::zeroed::(); - assert!( - ((((libc::sigaction(2, std::ptr::null(), (&mut sa as *mut sigaction))) == (0)) as i32) - != 0) - ); - assert!( - ((((libc::sigaction(15, std::ptr::null(), (&mut sa as *mut sigaction))) == (0)) as i32) - != 0) - ); - (*libc::__errno_location()) = 0; - assert!( - ((((libc::sigaction(99999, std::ptr::null(), (&mut sa as *mut sigaction))) == (-1_i32)) - as i32) - != 0) - ); - assert!(((((*libc::__errno_location()) == (22)) as i32) != 0)); - (*libc::__errno_location()) = 0; -} -pub fn main() { - unsafe { - std::process::exit(main_0() as i32); - } -} -unsafe fn main_0() -> i32 { - (unsafe { test_sigaction_0() }); - return 0; -} diff --git a/tests/unit/out/unsafe/termios.rs b/tests/unit/out/unsafe/termios.rs deleted file mode 100644 index 4e502d7..0000000 --- a/tests/unit/out/unsafe/termios.rs +++ /dev/null @@ -1,43 +0,0 @@ -extern crate libc; -use libc::*; -extern crate libcc2rs; -use libcc2rs::*; -use std::collections::BTreeMap; -use std::io::{Read, Seek, Write}; -use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; -use std::rc::Rc; -pub unsafe fn test_tcgetattr_0() { - let mut t: termios = std::mem::zeroed::(); - (*libc::__errno_location()) = 0; - assert!(((((libc::tcgetattr(0, (&mut t as *mut termios))) == (-1_i32)) as i32) != 0)); - assert!(((((*libc::__errno_location()) == (25)) as i32) != 0)); - (*libc::__errno_location()) = 0; -} -pub unsafe fn test_tcsetattr_1() { - let mut t: termios = std::mem::zeroed::(); - { - let byte_0 = ((&mut t as *mut termios) as *mut termios as *mut ::libc::c_void) as *mut u8; - for offset in 0..::std::mem::size_of::() as u64 { - *byte_0.offset(offset as isize) = 0 as u8; - } - ((&mut t as *mut termios) as *mut termios as *mut ::libc::c_void) - }; - (*libc::__errno_location()) = 0; - assert!( - ((((libc::tcsetattr(-1_i32, 0, (&mut t as *mut termios).cast_const())) == (-1_i32)) - as i32) - != 0) - ); - assert!(((((*libc::__errno_location()) == (9)) as i32) != 0)); - (*libc::__errno_location()) = 0; -} -pub fn main() { - unsafe { - std::process::exit(main_0() as i32); - } -} -unsafe fn main_0() -> i32 { - (unsafe { test_tcgetattr_0() }); - (unsafe { test_tcsetattr_1() }); - return 0; -} diff --git a/tests/unit/pwd.c b/tests/unit/pwd.c deleted file mode 100644 index 6b575c0..0000000 --- a/tests/unit/pwd.c +++ /dev/null @@ -1,19 +0,0 @@ -// no-compile: refcount -#include -#include -#include -#include -#include - -static void test_getpwuid(void) { - uid_t uid = geteuid(); - struct passwd *pw = getpwuid(uid); - assert(pw != NULL); - assert(pw->pw_uid == uid); - printf("%s\n", pw->pw_name); -} - -int main(void) { - test_getpwuid(); - return 0; -} diff --git a/tests/unit/signal.c b/tests/unit/signal.c deleted file mode 100644 index 6673f94..0000000 --- a/tests/unit/signal.c +++ /dev/null @@ -1,20 +0,0 @@ -// no-compile: refcount -#include -#include -#include -#include - -static void test_sigaction(void) { - struct sigaction sa; - assert(sigaction(SIGINT, NULL, &sa) == 0); - assert(sigaction(SIGTERM, NULL, &sa) == 0); - errno = 0; - assert(sigaction(99999, NULL, &sa) == -1); - assert(errno == EINVAL); - errno = 0; -} - -int main(void) { - test_sigaction(); - return 0; -} diff --git a/tests/unit/termios.c b/tests/unit/termios.c deleted file mode 100644 index e2aaa3a..0000000 --- a/tests/unit/termios.c +++ /dev/null @@ -1,28 +0,0 @@ -// no-compile: refcount -#include -#include -#include -#include - -static void test_tcgetattr(void) { - struct termios t; - errno = 0; - assert(tcgetattr(0, &t) == -1); - assert(errno == ENOTTY); - errno = 0; -} - -static void test_tcsetattr(void) { - struct termios t; - memset(&t, 0, sizeof(t)); - errno = 0; - assert(tcsetattr(-1, TCSANOW, &t) == -1); - assert(errno == EBADF); - errno = 0; -} - -int main(void) { - test_tcgetattr(); - test_tcsetattr(); - return 0; -} From 1f306255ed4094eb36cc06fe834d38efda6c0698 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 21 May 2026 09:14:14 +0100 Subject: [PATCH 7/8] Add modules --- rules/signal/tgt_unsafe.rs | 6 +----- rules/src/modules.rs | 10 ++++++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/rules/signal/tgt_unsafe.rs b/rules/signal/tgt_unsafe.rs index 1147fb2..dfa56f8 100644 --- a/rules/signal/tgt_unsafe.rs +++ b/rules/signal/tgt_unsafe.rs @@ -1,10 +1,6 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -unsafe fn f1( - a0: i32, - a1: *const ::libc::sigaction, - a2: *mut ::libc::sigaction, -) -> i32 { +unsafe fn f1(a0: i32, a1: *const ::libc::sigaction, a2: *mut ::libc::sigaction) -> i32 { libc::sigaction(a0, a1, a2) } diff --git a/rules/src/modules.rs b/rules/src/modules.rs index 8faa0a0..06eb614 100644 --- a/rules/src/modules.rs +++ b/rules/src/modules.rs @@ -52,12 +52,16 @@ pub mod iostream_tgt_unsafe; pub mod ip_tgt_unsafe; #[path = r#"../limits/tgt_unsafe.rs"#] pub mod limits_tgt_unsafe; +#[path = r#"../locale/tgt_unsafe.rs"#] +pub mod locale_tgt_unsafe; #[path = r#"../map/tgt_refcount.rs"#] pub mod map_tgt_refcount; #[path = r#"../map/tgt_unsafe.rs"#] pub mod map_tgt_unsafe; #[path = r#"../math/tgt_unsafe.rs"#] pub mod math_tgt_unsafe; +#[path = r#"../netdb/tgt_unsafe.rs"#] +pub mod netdb_tgt_unsafe; #[path = r#"../pair/tgt_refcount.rs"#] pub mod pair_tgt_refcount; #[path = r#"../pair/tgt_unsafe.rs"#] @@ -66,6 +70,10 @@ pub mod pair_tgt_unsafe; pub mod poll_tgt_unsafe; #[path = r#"../select/tgt_unsafe.rs"#] pub mod select_tgt_unsafe; +#[path = r#"../pwd/tgt_unsafe.rs"#] +pub mod pwd_tgt_unsafe; +#[path = r#"../signal/tgt_unsafe.rs"#] +pub mod signal_tgt_unsafe; #[path = r#"../socket/tgt_unsafe.rs"#] pub mod socket_tgt_unsafe; #[path = r#"../stat/tgt_unsafe.rs"#] @@ -78,6 +86,8 @@ pub mod stdio_tgt_unsafe; pub mod string_tgt_refcount; #[path = r#"../string/tgt_unsafe.rs"#] pub mod string_tgt_unsafe; +#[path = r#"../termios/tgt_unsafe.rs"#] +pub mod termios_tgt_unsafe; #[path = r#"../unique_ptr/tgt_refcount.rs"#] pub mod unique_ptr_tgt_refcount; #[path = r#"../unique_ptr/tgt_unsafe.rs"#] From 218a41b28d988d3aba358b2eb23d82dc9bab707b Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 21 May 2026 14:47:31 +0100 Subject: [PATCH 8/8] Update modules --- rules/src/modules.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rules/src/modules.rs b/rules/src/modules.rs index 06eb614..b04c66f 100644 --- a/rules/src/modules.rs +++ b/rules/src/modules.rs @@ -68,10 +68,10 @@ pub mod pair_tgt_refcount; pub mod pair_tgt_unsafe; #[path = r#"../poll/tgt_unsafe.rs"#] pub mod poll_tgt_unsafe; -#[path = r#"../select/tgt_unsafe.rs"#] -pub mod select_tgt_unsafe; #[path = r#"../pwd/tgt_unsafe.rs"#] pub mod pwd_tgt_unsafe; +#[path = r#"../select/tgt_unsafe.rs"#] +pub mod select_tgt_unsafe; #[path = r#"../signal/tgt_unsafe.rs"#] pub mod signal_tgt_unsafe; #[path = r#"../socket/tgt_unsafe.rs"#]