From efdb5ab3af6c675fdcb14ad0d406d2bb9837b0fb Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Sun, 19 Jul 2026 17:51:36 +0100 Subject: [PATCH 1/2] Add rules for CLOCK_MONOTONIC constants --- rules/time/src.c | 6 +++ rules/time/tgt_refcount.rs | 9 ++++ rules/time/tgt_unsafe.rs | 9 ++++ tests/unit/out/refcount/sys_time.rs | 75 ++++++++++++++++++++++++----- tests/unit/out/unsafe/sys_time.rs | 51 +++++++++++++++----- tests/unit/sys_time.c | 12 +++++ 6 files changed, 138 insertions(+), 24 deletions(-) diff --git a/rules/time/src.c b/rules/time/src.c index 7d4f8d2c..90fbbdc3 100644 --- a/rules/time/src.c +++ b/rules/time/src.c @@ -45,3 +45,9 @@ int f8(struct timeval *tv, void *tz) { clockid_t f9() { return CLOCK_REALTIME; } + +clockid_t f10() { return CLOCK_MONOTONIC; } + +#ifdef __linux__ +clockid_t f11() { return CLOCK_MONOTONIC_RAW; } +#endif diff --git a/rules/time/tgt_refcount.rs b/rules/time/tgt_refcount.rs index d8aa4204..8100f0d3 100644 --- a/rules/time/tgt_refcount.rs +++ b/rules/time/tgt_refcount.rs @@ -170,3 +170,12 @@ fn f8(a0: Ptr, a1: AnyPtr) -> i32 { unsafe fn f9() -> nix::time::ClockId { nix::time::ClockId::CLOCK_REALTIME } + +unsafe fn f10() -> nix::time::ClockId { + nix::time::ClockId::CLOCK_MONOTONIC +} + +#[cfg(target_os = "linux")] +unsafe fn f11() -> nix::time::ClockId { + nix::time::ClockId::CLOCK_MONOTONIC_RAW +} diff --git a/rules/time/tgt_unsafe.rs b/rules/time/tgt_unsafe.rs index 58bd70de..b1b41c9e 100644 --- a/rules/time/tgt_unsafe.rs +++ b/rules/time/tgt_unsafe.rs @@ -55,3 +55,12 @@ unsafe fn f8(a0: *mut libc::timeval, a1: *mut libc::c_void) -> i32 { unsafe fn f9() -> libc::clockid_t { libc::CLOCK_REALTIME } + +unsafe fn f10() -> libc::clockid_t { + libc::CLOCK_MONOTONIC +} + +#[cfg(target_os = "linux")] +unsafe fn f11() -> libc::clockid_t { + libc::CLOCK_MONOTONIC_RAW +} diff --git a/tests/unit/out/refcount/sys_time.rs b/tests/unit/out/refcount/sys_time.rs index a26480fb..c3d01552 100644 --- a/tests/unit/out/refcount/sys_time.rs +++ b/tests/unit/out/refcount/sys_time.rs @@ -80,7 +80,57 @@ pub fn print_tm_1(t: i64) { (*(*tm.borrow()).tm_isdst.borrow()) ); } -pub fn test_gmtime_r_2() { +pub fn test_clock_gettime_2() { + let ts: Value = Rc::new(RefCell::new(Default::default())); + assert!( + (((match nix::time::clock_gettime(nix::time::ClockId::CLOCK_REALTIME) { + Ok(__ts) => { + (ts.as_pointer()).with_mut(|__t| { + *__t.tv_sec.borrow_mut() = __ts.tv_sec() as i64; + *__t.tv_nsec.borrow_mut() = __ts.tv_nsec() as i64; + }); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + assert!(((((*(*ts.borrow()).tv_sec.borrow()) > 1500000000_i64) as i32) != 0)); + assert!( + (((((((*(*ts.borrow()).tv_nsec.borrow()) >= 0_i64) as i32) != 0) + && ((((*(*ts.borrow()).tv_nsec.borrow()) < 1000000000_i64) as i32) != 0)) + as i32) + != 0) + ); + let mono: Value = Rc::new(RefCell::new(Default::default())); + assert!( + (((match nix::time::clock_gettime(nix::time::ClockId::CLOCK_MONOTONIC) { + Ok(__ts) => { + (mono.as_pointer()).with_mut(|__t| { + *__t.tv_sec.borrow_mut() = __ts.tv_sec() as i64; + *__t.tv_nsec.borrow_mut() = __ts.tv_nsec() as i64; + }); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + assert!(((((*(*mono.borrow()).tv_sec.borrow()) >= 0_i64) as i32) != 0)); + assert!( + (((((((*(*mono.borrow()).tv_nsec.borrow()) >= 0_i64) as i32) != 0) + && ((((*(*mono.borrow()).tv_nsec.borrow()) < 1000000000_i64) as i32) != 0)) + as i32) + != 0) + ); +} +pub fn test_gmtime_r_3() { ({ print_tm_1(0_i64) }); ({ print_tm_1(1_i64) }); ({ print_tm_1(86399_i64) }); @@ -92,7 +142,7 @@ pub fn test_gmtime_r_2() { ({ print_tm_1(1721126096_i64) }); ({ print_tm_1(4102444800_i64) }); } -pub fn print_local_tm_3(t: i64) { +pub fn print_local_tm_4(t: i64) { let t: Value = Rc::new(RefCell::new(t)); let tm: Value = Rc::new(RefCell::new(Default::default())); assert!( @@ -135,14 +185,14 @@ pub fn print_local_tm_3(t: i64) { (*(*tm.borrow()).tm_isdst.borrow()) ); } -pub fn test_localtime_r_4() { - ({ print_local_tm_3(0_i64) }); - ({ print_local_tm_3(951782400_i64) }); - ({ print_local_tm_3(1704067199_i64) }); - ({ print_local_tm_3(1721126096_i64) }); - ({ print_local_tm_3(1735689600_i64) }); +pub fn test_localtime_r_5() { + ({ print_local_tm_4(0_i64) }); + ({ print_local_tm_4(951782400_i64) }); + ({ print_local_tm_4(1704067199_i64) }); + ({ print_local_tm_4(1721126096_i64) }); + ({ print_local_tm_4(1735689600_i64) }); } -pub fn test_strftime_5() { +pub fn test_strftime_6() { let t: Value = Rc::new(RefCell::new(1721126096_i64)); let tm: Value = Rc::new(RefCell::new(Default::default())); assert!( @@ -302,8 +352,9 @@ pub fn main() { } fn main_0() -> i32 { ({ test_time_0() }); - ({ test_gmtime_r_2() }); - ({ test_localtime_r_4() }); - ({ test_strftime_5() }); + ({ test_clock_gettime_2() }); + ({ test_gmtime_r_3() }); + ({ test_localtime_r_5() }); + ({ test_strftime_6() }); return 0; } diff --git a/tests/unit/out/unsafe/sys_time.rs b/tests/unit/out/unsafe/sys_time.rs index 2bcfd93c..a7f2d836 100644 --- a/tests/unit/out/unsafe/sys_time.rs +++ b/tests/unit/out/unsafe/sys_time.rs @@ -42,7 +42,33 @@ pub unsafe fn print_tm_1(mut t: i64) { tm.tm_isdst, ); } -pub unsafe fn test_gmtime_r_2() { +pub unsafe fn test_clock_gettime_2() { + let mut ts: ::libc::timespec = unsafe { std::mem::zeroed() }; + assert!( + ((((libc::clock_gettime(libc::CLOCK_REALTIME, (&mut ts as *mut ::libc::timespec))) == (0)) + as i32) + != 0) + ); + assert!(((((ts.tv_sec) > (1500000000_i64)) as i32) != 0)); + assert!( + (((((((ts.tv_nsec) >= (0_i64)) as i32) != 0) + && ((((ts.tv_nsec) < (1000000000_i64)) as i32) != 0)) as i32) + != 0) + ); + let mut mono: ::libc::timespec = unsafe { std::mem::zeroed() }; + assert!( + ((((libc::clock_gettime(libc::CLOCK_MONOTONIC, (&mut mono as *mut ::libc::timespec))) + == (0)) as i32) + != 0) + ); + assert!(((((mono.tv_sec) >= (0_i64)) as i32) != 0)); + assert!( + (((((((mono.tv_nsec) >= (0_i64)) as i32) != 0) + && ((((mono.tv_nsec) < (1000000000_i64)) as i32) != 0)) as i32) + != 0) + ); +} +pub unsafe fn test_gmtime_r_3() { (unsafe { print_tm_1(0_i64) }); (unsafe { print_tm_1(1_i64) }); (unsafe { print_tm_1(86399_i64) }); @@ -54,7 +80,7 @@ pub unsafe fn test_gmtime_r_2() { (unsafe { print_tm_1(1721126096_i64) }); (unsafe { print_tm_1(4102444800_i64) }); } -pub unsafe fn print_local_tm_3(mut t: i64) { +pub unsafe fn print_local_tm_4(mut t: i64) { let mut tm: ::libc::tm = unsafe { std::mem::zeroed() }; assert!( (((!((libc::localtime_r( @@ -82,14 +108,14 @@ pub unsafe fn print_local_tm_3(mut t: i64) { tm.tm_isdst, ); } -pub unsafe fn test_localtime_r_4() { - (unsafe { print_local_tm_3(0_i64) }); - (unsafe { print_local_tm_3(951782400_i64) }); - (unsafe { print_local_tm_3(1704067199_i64) }); - (unsafe { print_local_tm_3(1721126096_i64) }); - (unsafe { print_local_tm_3(1735689600_i64) }); +pub unsafe fn test_localtime_r_5() { + (unsafe { print_local_tm_4(0_i64) }); + (unsafe { print_local_tm_4(951782400_i64) }); + (unsafe { print_local_tm_4(1704067199_i64) }); + (unsafe { print_local_tm_4(1721126096_i64) }); + (unsafe { print_local_tm_4(1735689600_i64) }); } -pub unsafe fn test_strftime_5() { +pub unsafe fn test_strftime_6() { let mut t: i64 = 1721126096_i64; let mut tm: ::libc::tm = unsafe { std::mem::zeroed() }; assert!( @@ -171,8 +197,9 @@ pub fn main() { } unsafe fn main_0() -> i32 { (unsafe { test_time_0() }); - (unsafe { test_gmtime_r_2() }); - (unsafe { test_localtime_r_4() }); - (unsafe { test_strftime_5() }); + (unsafe { test_clock_gettime_2() }); + (unsafe { test_gmtime_r_3() }); + (unsafe { test_localtime_r_5() }); + (unsafe { test_strftime_6() }); return 0; } diff --git a/tests/unit/sys_time.c b/tests/unit/sys_time.c index 51983f43..d5c5f054 100644 --- a/tests/unit/sys_time.c +++ b/tests/unit/sys_time.c @@ -21,6 +21,17 @@ static void print_tm(time_t t) { tm.tm_wday, tm.tm_yday, tm.tm_zone, tm.tm_gmtoff, tm.tm_isdst); } +static void test_clock_gettime(void) { + struct timespec ts; + assert(clock_gettime(CLOCK_REALTIME, &ts) == 0); + assert(ts.tv_sec > 1500000000); + assert(ts.tv_nsec >= 0 && ts.tv_nsec < 1000000000); + struct timespec mono; + assert(clock_gettime(CLOCK_MONOTONIC, &mono) == 0); + assert(mono.tv_sec >= 0); + assert(mono.tv_nsec >= 0 && mono.tv_nsec < 1000000000); +} + static void test_gmtime_r(void) { print_tm(0); print_tm(1); @@ -69,6 +80,7 @@ static void test_strftime(void) { int main(void) { test_time(); + test_clock_gettime(); test_gmtime_r(); test_localtime_r(); test_strftime(); From 71e97550a1c78d4dd01b47d46cf032e8fc24793f Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Sun, 19 Jul 2026 18:02:37 +0100 Subject: [PATCH 2/2] Drop non-portable test --- tests/unit/out/refcount/sys_time.rs | 75 +++++------------------------ tests/unit/out/unsafe/sys_time.rs | 51 +++++--------------- tests/unit/sys_time.c | 12 ----- 3 files changed, 24 insertions(+), 114 deletions(-) diff --git a/tests/unit/out/refcount/sys_time.rs b/tests/unit/out/refcount/sys_time.rs index c3d01552..a26480fb 100644 --- a/tests/unit/out/refcount/sys_time.rs +++ b/tests/unit/out/refcount/sys_time.rs @@ -80,57 +80,7 @@ pub fn print_tm_1(t: i64) { (*(*tm.borrow()).tm_isdst.borrow()) ); } -pub fn test_clock_gettime_2() { - let ts: Value = Rc::new(RefCell::new(Default::default())); - assert!( - (((match nix::time::clock_gettime(nix::time::ClockId::CLOCK_REALTIME) { - Ok(__ts) => { - (ts.as_pointer()).with_mut(|__t| { - *__t.tv_sec.borrow_mut() = __ts.tv_sec() as i64; - *__t.tv_nsec.borrow_mut() = __ts.tv_nsec() as i64; - }); - 0 - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); - assert!(((((*(*ts.borrow()).tv_sec.borrow()) > 1500000000_i64) as i32) != 0)); - assert!( - (((((((*(*ts.borrow()).tv_nsec.borrow()) >= 0_i64) as i32) != 0) - && ((((*(*ts.borrow()).tv_nsec.borrow()) < 1000000000_i64) as i32) != 0)) - as i32) - != 0) - ); - let mono: Value = Rc::new(RefCell::new(Default::default())); - assert!( - (((match nix::time::clock_gettime(nix::time::ClockId::CLOCK_MONOTONIC) { - Ok(__ts) => { - (mono.as_pointer()).with_mut(|__t| { - *__t.tv_sec.borrow_mut() = __ts.tv_sec() as i64; - *__t.tv_nsec.borrow_mut() = __ts.tv_nsec() as i64; - }); - 0 - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); - assert!(((((*(*mono.borrow()).tv_sec.borrow()) >= 0_i64) as i32) != 0)); - assert!( - (((((((*(*mono.borrow()).tv_nsec.borrow()) >= 0_i64) as i32) != 0) - && ((((*(*mono.borrow()).tv_nsec.borrow()) < 1000000000_i64) as i32) != 0)) - as i32) - != 0) - ); -} -pub fn test_gmtime_r_3() { +pub fn test_gmtime_r_2() { ({ print_tm_1(0_i64) }); ({ print_tm_1(1_i64) }); ({ print_tm_1(86399_i64) }); @@ -142,7 +92,7 @@ pub fn test_gmtime_r_3() { ({ print_tm_1(1721126096_i64) }); ({ print_tm_1(4102444800_i64) }); } -pub fn print_local_tm_4(t: i64) { +pub fn print_local_tm_3(t: i64) { let t: Value = Rc::new(RefCell::new(t)); let tm: Value = Rc::new(RefCell::new(Default::default())); assert!( @@ -185,14 +135,14 @@ pub fn print_local_tm_4(t: i64) { (*(*tm.borrow()).tm_isdst.borrow()) ); } -pub fn test_localtime_r_5() { - ({ print_local_tm_4(0_i64) }); - ({ print_local_tm_4(951782400_i64) }); - ({ print_local_tm_4(1704067199_i64) }); - ({ print_local_tm_4(1721126096_i64) }); - ({ print_local_tm_4(1735689600_i64) }); +pub fn test_localtime_r_4() { + ({ print_local_tm_3(0_i64) }); + ({ print_local_tm_3(951782400_i64) }); + ({ print_local_tm_3(1704067199_i64) }); + ({ print_local_tm_3(1721126096_i64) }); + ({ print_local_tm_3(1735689600_i64) }); } -pub fn test_strftime_6() { +pub fn test_strftime_5() { let t: Value = Rc::new(RefCell::new(1721126096_i64)); let tm: Value = Rc::new(RefCell::new(Default::default())); assert!( @@ -352,9 +302,8 @@ pub fn main() { } fn main_0() -> i32 { ({ test_time_0() }); - ({ test_clock_gettime_2() }); - ({ test_gmtime_r_3() }); - ({ test_localtime_r_5() }); - ({ test_strftime_6() }); + ({ test_gmtime_r_2() }); + ({ test_localtime_r_4() }); + ({ test_strftime_5() }); return 0; } diff --git a/tests/unit/out/unsafe/sys_time.rs b/tests/unit/out/unsafe/sys_time.rs index a7f2d836..2bcfd93c 100644 --- a/tests/unit/out/unsafe/sys_time.rs +++ b/tests/unit/out/unsafe/sys_time.rs @@ -42,33 +42,7 @@ pub unsafe fn print_tm_1(mut t: i64) { tm.tm_isdst, ); } -pub unsafe fn test_clock_gettime_2() { - let mut ts: ::libc::timespec = unsafe { std::mem::zeroed() }; - assert!( - ((((libc::clock_gettime(libc::CLOCK_REALTIME, (&mut ts as *mut ::libc::timespec))) == (0)) - as i32) - != 0) - ); - assert!(((((ts.tv_sec) > (1500000000_i64)) as i32) != 0)); - assert!( - (((((((ts.tv_nsec) >= (0_i64)) as i32) != 0) - && ((((ts.tv_nsec) < (1000000000_i64)) as i32) != 0)) as i32) - != 0) - ); - let mut mono: ::libc::timespec = unsafe { std::mem::zeroed() }; - assert!( - ((((libc::clock_gettime(libc::CLOCK_MONOTONIC, (&mut mono as *mut ::libc::timespec))) - == (0)) as i32) - != 0) - ); - assert!(((((mono.tv_sec) >= (0_i64)) as i32) != 0)); - assert!( - (((((((mono.tv_nsec) >= (0_i64)) as i32) != 0) - && ((((mono.tv_nsec) < (1000000000_i64)) as i32) != 0)) as i32) - != 0) - ); -} -pub unsafe fn test_gmtime_r_3() { +pub unsafe fn test_gmtime_r_2() { (unsafe { print_tm_1(0_i64) }); (unsafe { print_tm_1(1_i64) }); (unsafe { print_tm_1(86399_i64) }); @@ -80,7 +54,7 @@ pub unsafe fn test_gmtime_r_3() { (unsafe { print_tm_1(1721126096_i64) }); (unsafe { print_tm_1(4102444800_i64) }); } -pub unsafe fn print_local_tm_4(mut t: i64) { +pub unsafe fn print_local_tm_3(mut t: i64) { let mut tm: ::libc::tm = unsafe { std::mem::zeroed() }; assert!( (((!((libc::localtime_r( @@ -108,14 +82,14 @@ pub unsafe fn print_local_tm_4(mut t: i64) { tm.tm_isdst, ); } -pub unsafe fn test_localtime_r_5() { - (unsafe { print_local_tm_4(0_i64) }); - (unsafe { print_local_tm_4(951782400_i64) }); - (unsafe { print_local_tm_4(1704067199_i64) }); - (unsafe { print_local_tm_4(1721126096_i64) }); - (unsafe { print_local_tm_4(1735689600_i64) }); +pub unsafe fn test_localtime_r_4() { + (unsafe { print_local_tm_3(0_i64) }); + (unsafe { print_local_tm_3(951782400_i64) }); + (unsafe { print_local_tm_3(1704067199_i64) }); + (unsafe { print_local_tm_3(1721126096_i64) }); + (unsafe { print_local_tm_3(1735689600_i64) }); } -pub unsafe fn test_strftime_6() { +pub unsafe fn test_strftime_5() { let mut t: i64 = 1721126096_i64; let mut tm: ::libc::tm = unsafe { std::mem::zeroed() }; assert!( @@ -197,9 +171,8 @@ pub fn main() { } unsafe fn main_0() -> i32 { (unsafe { test_time_0() }); - (unsafe { test_clock_gettime_2() }); - (unsafe { test_gmtime_r_3() }); - (unsafe { test_localtime_r_5() }); - (unsafe { test_strftime_6() }); + (unsafe { test_gmtime_r_2() }); + (unsafe { test_localtime_r_4() }); + (unsafe { test_strftime_5() }); return 0; } diff --git a/tests/unit/sys_time.c b/tests/unit/sys_time.c index d5c5f054..51983f43 100644 --- a/tests/unit/sys_time.c +++ b/tests/unit/sys_time.c @@ -21,17 +21,6 @@ static void print_tm(time_t t) { tm.tm_wday, tm.tm_yday, tm.tm_zone, tm.tm_gmtoff, tm.tm_isdst); } -static void test_clock_gettime(void) { - struct timespec ts; - assert(clock_gettime(CLOCK_REALTIME, &ts) == 0); - assert(ts.tv_sec > 1500000000); - assert(ts.tv_nsec >= 0 && ts.tv_nsec < 1000000000); - struct timespec mono; - assert(clock_gettime(CLOCK_MONOTONIC, &mono) == 0); - assert(mono.tv_sec >= 0); - assert(mono.tv_nsec >= 0 && mono.tv_nsec < 1000000000); -} - static void test_gmtime_r(void) { print_tm(0); print_tm(1); @@ -80,7 +69,6 @@ static void test_strftime(void) { int main(void) { test_time(); - test_clock_gettime(); test_gmtime_r(); test_localtime_r(); test_strftime();