clock_settime: Detect external clock adjustments via CLOCK_MONOTONIC
PATCH 1/1 clock_settime: Detect external clock adjustments via CLOCK_MONOTONIC
From: Andrea Cervesato <[email protected]>
These tests manipulate CLOCK_REALTIME to verify timer and clock_nanosleep behavior, but NTP or VM time sync can also adjust CLOCK_REALTIME during the test, causing timers to fire at wrong times and producing spurious failures.
Use CLOCK_MONOTONIC as a sideband check to detect external interference. If the monotonic elapsed time is anomalous, report UNTESTED instead of a false FAIL.
---
I used Claude to generate the right patches to fix sporadic issues in the clock_settime testing suite. We tried many attempt for fixing it, but the CLOCK_REALTIME usage seems to be the real issue in here. The idea is that we use internal Openposix mechanisms to skip tests if CLOCK_MONOTONIC usage spots a possible interference from external tools.
--- .../conformance/interfaces/clock_settime/4-1.c | 26 +++++++++++++++++ .../conformance/interfaces/clock_settime/5-1.c | 33 +++++++++++++++++++++- .../conformance/interfaces/clock_settime/5-2.c | 33 +++++++++++++++++++++- .../conformance/interfaces/clock_settime/7-1.c | 28 ++++++++++++++++++ .../conformance/interfaces/clock_settime/7-2.c | 31 +++++++++++++++++++- 5 files changed, 148 insertions(+), 3 deletions(-)
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/4-1.c +26 -0
@@ -42,6 +42,7 @@ int main(void) {struct sigevent ev;struct timespec tpT0, tpT2, tpreset; +struct timespec mono_before, mono_after;struct itimerspec its;timer_t tid;int delta;
@@ -82,6 +83,11 @@ int main(void)return PTS_UNRESOLVED ; } +if (clock_gettime (CLOCK_MONOTONIC , &mono_before) !=0 ) { +perror ("clock_gettime() was not successful\n" ); +return PTS_UNRESOLVED ; + } +if (timer_create (CLOCK_REALTIME , &ev, &tid) !=0 ) {perror ("timer_create() did not return success\n" );return PTS_UNRESOLVED ;
@@ -116,10 +122,30 @@ int main(void) delta = tpT2.tv_sec - its.it_value.tv_sec; +if (clock_gettime (CLOCK_MONOTONIC , &mono_after) !=0 ) { +perror ("clock_gettime() was not successful\n" ); +return PTS_UNRESOLVED ; + } +// add back time waited to reset value and reset time tpreset.tv_sec += tpT2.tv_sec - tpT0.tv_sec;setBackTime (tpreset); +/* +* The expected monotonic elapsed time is SLEEPTIME + TIMEROFFSET +* since after resetting the clock, the timer must wait another +* full TIMEROFFSET. If monotonic time is much shorter, an external +* clock adjustment (NTP, VM sync) interfered with the test. +*/ +if (mono_after.tv_sec - mono_before.tv_sec < +SLEEPTIME +TIMEROFFSET -ACCEPTABLEDELTA ) { +printf ("UNTESTED: external clock adjustment detected " +"(monotonic elapsed %ds, expected ~%ds)\n" , + (int )(mono_after.tv_sec - mono_before.tv_sec), +SLEEPTIME +TIMEROFFSET ); +return PTS_UNTESTED ; + } +printf ("delta: %d\n" , delta);if ((delta <=ACCEPTABLEDELTA ) && (delta >=0 )) {printf ("Test PASSED\n" );
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/5-1.c +32 -1
@@ -46,9 +46,11 @@ int main(void)struct sigevent ev;struct sigaction act;struct timespec tsclock, ts, tsleft, tsreset; +struct timespec mono_before, mono_after;struct itimerspec its;timer_t tid;sigset_t set; +int ns_ret;/* Check that we're root...can't call clock_settime with CLOCK_REALTIME otherwise */ if (getuid () !=0 ) {
@@ -97,6 +99,11 @@ int main(void)return PTS_UNRESOLVED ; } +if (clock_gettime (CLOCK_MONOTONIC , &mono_before) !=0 ) { +perror ("clock_gettime() was not successful\n" ); +return PTS_UNRESOLVED ; + } +if (clock_gettime (CLOCK_REALTIME , &tsclock) !=0 ) {printf ("clock_gettime() did not return success\n" );return PTS_UNRESOLVED ;
@@ -112,7 +119,31 @@ int main(void) ts.tv_sec =TIMERSEC +SLEEPDELTA ; ts.tv_nsec =0 ; -if (nanosleep (&ts, &tsleft) !=-1 ) { + ns_ret =nanosleep (&ts, &tsleft); + +if (clock_gettime (CLOCK_MONOTONIC , &mono_after) !=0 ) { +perror ("clock_gettime() was not successful\n" ); +return PTS_UNRESOLVED ; + } + +/* +* The relative timer should fire after TIMERSEC regardless of +* clock_settime changes. If monotonic elapsed time is too far +* from TIMERSEC, an external clock adjustment (NTP, VM sync) +* interfered with the test. +*/ +if (labs (mono_after.tv_sec - mono_before.tv_sec -TIMERSEC ) > +ACCEPTABLEDELTA +1 ) { +printf ("UNTESTED: external clock adjustment detected " +"(monotonic elapsed %ds, expected ~%ds)\n" , + (int )(mono_after.tv_sec - mono_before.tv_sec), +TIMERSEC ); + tsreset.tv_sec += mono_after.tv_sec - mono_before.tv_sec; +setBackTime (tsreset); +return PTS_UNTESTED ; + } + +if (ns_ret !=-1 ) {printf ("nanosleep() not interrupted\n" );return PTS_FAIL ; }
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/5-2.c +32 -1
@@ -46,9 +46,11 @@ int main(void)struct sigevent ev;struct sigaction act;struct timespec tsclock, ts, tsleft, tsreset; +struct timespec mono_before, mono_after;struct itimerspec its;timer_t tid;sigset_t set; +int ns_ret;/* Check that we're root...can't call clock_settime with CLOCK_REALTIME otherwise */ if (getuid () !=0 ) {
@@ -97,6 +99,11 @@ int main(void)return PTS_UNRESOLVED ; } +if (clock_gettime (CLOCK_MONOTONIC , &mono_before) !=0 ) { +perror ("clock_gettime() was not successful\n" ); +return PTS_UNRESOLVED ; + } +if (clock_gettime (CLOCK_REALTIME , &tsclock) !=0 ) {printf ("clock_gettime() did not return success\n" );return PTS_UNRESOLVED ;
@@ -112,7 +119,31 @@ int main(void) ts.tv_sec =TIMERSEC +SLEEPDELTA ; ts.tv_nsec =0 ; -if (nanosleep (&ts, &tsleft) !=-1 ) { + ns_ret =nanosleep (&ts, &tsleft); + +if (clock_gettime (CLOCK_MONOTONIC , &mono_after) !=0 ) { +perror ("clock_gettime() was not successful\n" ); +return PTS_UNRESOLVED ; + } + +/* +* The relative timer should fire after TIMERSEC regardless of +* clock_settime changes. If monotonic elapsed time is too far +* from TIMERSEC, an external clock adjustment (NTP, VM sync) +* interfered with the test. +*/ +if (labs (mono_after.tv_sec - mono_before.tv_sec -TIMERSEC ) > +ACCEPTABLEDELTA +1 ) { +printf ("UNTESTED: external clock adjustment detected " +"(monotonic elapsed %ds, expected ~%ds)\n" , + (int )(mono_after.tv_sec - mono_before.tv_sec), +TIMERSEC ); + tsreset.tv_sec += mono_after.tv_sec - mono_before.tv_sec; +setBackTime (tsreset); +return PTS_UNTESTED ; + } + +if (ns_ret !=-1 ) {printf ("nanosleep() not interrupted\n" );return PTS_FAIL ; }
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/7-1.c +28 -0
@@ -36,6 +36,7 @@int main (void ) {struct timespec tsT0, tsT1; +struct timespec mono_before, mono_after;int pid;/* Check that we're root...can't call clock_settime with CLOCK_REALTIME otherwise */
@@ -49,6 +50,11 @@ int main(void)return PTS_UNRESOLVED ; } +if (clock_gettime (CLOCK_MONOTONIC , &mono_before) !=0 ) { +perror ("clock_gettime() was not successful\n" ); +return PTS_UNRESOLVED ; + } +if ((pid =fork ()) ==0 ) {/* child here */ int flags =0 ;
@@ -100,9 +106,31 @@ int main(void)return PTS_UNRESOLVED ; } +if (clock_gettime (CLOCK_MONOTONIC , &mono_after) !=0 ) { +perror ("clock_gettime() was not successful\n" ); +return PTS_UNRESOLVED ; + } +getBeforeTime (&tsreset);// get current time tsreset.tv_sec +=SMALLTIME ;setBackTime (tsreset); + +/* +* Expected monotonic elapsed time is SMALLTIME + SLEEPOFFSET +* since after resetting the clock, the child must wait +* another full SLEEPOFFSET. If monotonic time is much +* shorter, an external clock adjustment (NTP, VM sync) +* interfered with the test. +*/ +if (mono_after.tv_sec - mono_before.tv_sec < +SMALLTIME +SLEEPOFFSET -ACCEPTABLEDELTA -1 ) { +printf ("UNTESTED: external clock adjustment detected " +"(monotonic elapsed %ds, expected ~%ds)\n" , + (int )(mono_after.tv_sec - mono_before.tv_sec), +SMALLTIME +SLEEPOFFSET ); +return PTS_UNTESTED ; + } +if (WIFEXITED (i) &&WEXITSTATUS (i)) {printf ("Test PASSED\n" );return PTS_PASS ;
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/7-2.c +30 -1
@@ -31,6 +31,7 @@#define CHILDPASS 1#define CHILDFAIL 0 +#define CHILDUNTESTED 2int main (void ) {
@@ -58,13 +59,38 @@ int main(void)/* child here */ int flags =0 ;struct timespec tsT3; +struct timespec mono_start, mono_end; flags |=TIMER_ABSTIME ; + +if (clock_gettime (CLOCK_MONOTONIC , &mono_start) !=0 ) { +perror ("clock_gettime() was not successful\n" ); +return CHILDFAIL ; + } +if (clock_nanosleep (CLOCK_REALTIME , flags, &tsT1,NULL ) !=0 ) {printf ("clock_nanosleep() did not return success\n" );return CHILDFAIL ; } +if (clock_gettime (CLOCK_MONOTONIC , &mono_end) !=0 ) { +perror ("clock_gettime() was not successful\n" ); +return CHILDFAIL ; + } + +/* +* The parent sleeps 1s before jumping the clock forward. +* If clock_nanosleep returned in less than 1s monotonic, +* an external clock adjustment (NTP, VM sync) woke us +* instead of the parent's clock_settime. +*/ +if (mono_end.tv_sec - mono_start.tv_sec <1 ) { +printf ("UNTESTED: external clock adjustment detected " +"(monotonic elapsed %ds)\n" , + (int )(mono_end.tv_sec - mono_start.tv_sec)); +return CHILDUNTESTED ; + } +if (clock_gettime (CLOCK_REALTIME , &tsT3) !=0 ) {perror ("clock_gettime() did not return success\n" );return CHILDFAIL ;
@@ -105,7 +131,10 @@ int main(void)setBackTime (tsreset);//should be ~= before time -if (WIFEXITED (i) &&WEXITSTATUS (i)) { +if (WIFEXITED (i) &&WEXITSTATUS (i) ==CHILDUNTESTED ) { +printf ("Test UNTESTED\n" ); +return PTS_UNTESTED ; + }else if (WIFEXITED (i) &&WEXITSTATUS (i)) {printf ("Test PASSED\n" );return PTS_PASS ; }else {
---
base-commit: 4688c20c01eece869b59e05ca3dd68c43e0d6af7
change-id: 20260331-clock_settime_fix-8e4117dcb609
Best regards,
Signature · 5 lines
Andrea Cervesato <[email protected]> -- Mailing list info: https://lists.linux.it/listinfo/ltp