Skip to contentSkip to conversation

clock_settime: Detect external clock adjustments via CLOCK_MONOTONIC

PATCH Applied Andrea Cervesato 3 participants 3 messages Apr 9 – Apr 13

lore
Share & tools
lore
#0 lore raw

PATCH v4 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 sporadic failures.

Add pts_mono_time_start()/pts_mono_time_check() helpers to detect external clock interference using CLOCK_MONOTONIC. When interference is detected, the test retries up to PTS_MONO_MAX_RETRIES times before reporting UNTESTED. Guard with _POSIX_MONOTONIC_CLOCK since CLOCK_MONOTONIC is optional in POSIX.1-2001.

---
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.
---
Changes in v4:
- handle return value for helpers functions
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- cycle multiple times before failing the tests
- add helpers functions for CLOCK_MONOTONIC checks
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- print a message if CLOCK_MONOTONIC is not available
- always verify that CLOCK_MONOTONIC is available
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 .../conformance/interfaces/clock_settime/4-1.c     |  86 +++++++++------
 .../conformance/interfaces/clock_settime/5-1.c     |  68 +++++++-----
 .../conformance/interfaces/clock_settime/5-2.c     |  58 ++++++----
 .../conformance/interfaces/clock_settime/7-1.c     |  98 ++++++++++-------
 .../conformance/interfaces/clock_settime/7-2.c     | 119 +++++++++++++--------
 .../conformance/interfaces/clock_settime/helpers.h |  55 ++++++++++
 6 files changed, 323 insertions(+), 161 deletions(-)
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/4-1.c +51 -35
index f8a3e9c542ca8c9767cdd0057005e519f58b2b55..763a68fc2b5b085c67597fafa859b0aa300fb54b 100644
@@ -47,7 +47,7 @@ int main(void)
 	int delta;
 	int sig;
 	sigset_t set;
-	int flags = 0;
+	int attempt, ret;
 
 	/* Check that we're root...can't call clock_settime with CLOCK_REALTIME otherwise */
 	if (geteuid() != 0) {
@@ -77,49 +77,65 @@ int main(void)
 		return PTS_UNRESOLVED;
 	}
 
-	if (clock_gettime(CLOCK_REALTIME, &tpT0) != 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;
 	}
 
-	flags |= TIMER_ABSTIME;
-	its.it_interval.tv_sec = 0;
-	its.it_interval.tv_nsec = 0;
-	its.it_value.tv_sec = tpT0.tv_sec + TIMEROFFSET;
-	its.it_value.tv_nsec = tpT0.tv_nsec;
-	if (timer_settime(tid, flags, &its, NULL) != 0) {
-		perror("timer_settime() did not return success\n");
-		return PTS_UNRESOLVED;
-	}
-
-	sleep(SLEEPTIME);
-	getBeforeTime(&tpreset);
-	if (clock_settime(CLOCK_REALTIME, &tpT0) != 0) {
-		perror("clock_settime() was not successful");
-		return PTS_UNRESOLVED;
-	}
-
-	if (sigwait(&set, &sig) == -1) {
-		perror("sigwait() was not successful\n");
-		return PTS_UNRESOLVED;
+	for (attempt = 0; attempt < PTS_MONO_MAX_RETRIES; attempt++) {
+		if (clock_gettime(CLOCK_REALTIME, &tpT0) != 0) {
+			perror("clock_gettime() was not successful\n");
+			return PTS_UNRESOLVED;
+		}
+
+		if (pts_mono_time_start() != 0)
+			return PTS_UNRESOLVED;
+
+		its.it_interval.tv_sec = 0;
+		its.it_interval.tv_nsec = 0;
+		its.it_value.tv_sec = tpT0.tv_sec + TIMEROFFSET;
+		its.it_value.tv_nsec = tpT0.tv_nsec;
+		if (timer_settime(tid, TIMER_ABSTIME, &its, NULL) != 0) {
+			perror("timer_settime() did not return success\n");
+			return PTS_UNRESOLVED;
+		}
+
+		sleep(SLEEPTIME);
+		getBeforeTime(&tpreset);
+		if (clock_settime(CLOCK_REALTIME, &tpT0) != 0) {
+			perror("clock_settime() was not successful");
+			return PTS_UNRESOLVED;
+		}
+
+		if (sigwait(&set, &sig) == -1) {
+			perror("sigwait() was not successful\n");
+			return PTS_UNRESOLVED;
+		}
+
+		if (clock_gettime(CLOCK_REALTIME, &tpT2) != 0) {
+			printf("clock_gettime() was not successful\n");
+			return PTS_UNRESOLVED;
+		}
+
+		delta = tpT2.tv_sec - its.it_value.tv_sec;
+
+		// add back time waited to reset value and reset time
+		tpreset.tv_sec += tpT2.tv_sec - tpT0.tv_sec;
+		setBackTime(tpreset);
+
+		ret = pts_mono_time_check(SLEEPTIME + TIMEROFFSET);
+		if (ret < 0)
+			return PTS_UNRESOLVED;
+		if (ret == 0)
+			break;
 	}
 
-	if (clock_gettime(CLOCK_REALTIME, &tpT2) != 0) {
-		printf("clock_gettime() was not successful\n");
-		return PTS_UNRESOLVED;
+	if (attempt == PTS_MONO_MAX_RETRIES) {
+		printf("UNTESTED: persistent clock interference after %d attempts\n",
+		       PTS_MONO_MAX_RETRIES);
+		return PTS_UNTESTED;
 	}
 
-	delta = tpT2.tv_sec - its.it_value.tv_sec;
-
-	// add back time waited to reset value and reset time
-	tpreset.tv_sec += tpT2.tv_sec - tpT0.tv_sec;
-	setBackTime(tpreset);
-
 	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 +44 -24

92 lines not on this page · raw message

testcases/open_posix_testsuite/conformance/interfaces/clock_settime/5-2.c +40 -18

81 lines not on this page · raw message

testcases/open_posix_testsuite/conformance/interfaces/clock_settime/7-1.c +57 -41

137 lines not on this page · raw message

testcases/open_posix_testsuite/conformance/interfaces/clock_settime/7-2.c +76 -43

161 lines not on this page · raw message

testcases/open_posix_testsuite/conformance/interfaces/clock_settime/helpers.h +55 -0

64 lines not on this page · raw message

---
base-commit: 1c46589251d6efa461d0e837540c990cd6dea188
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