openposix: timer_*/speculative: Skip untestable optional behavior on Linux
PATCH v2 1/1 openposix: timer_*/speculative: Skip untestable optional behavior on Linux
From: Avinesh Kumar <[email protected]>
timer_delete/speculative/5-{1,2}, timer_getoverrun/speculative/6-{1,2,3}, timer_gettime/speculative/6-{1,2,3}, and timer_settime/speculative/12-{1,2,3} all test EINVAL for an invalid timerid, which POSIX Issue 6/TC2 made optional rather than required.[0][1]
Linux does not implement this optional behavior, so skip it there with PTS_UNSUPPORTED and leave the test intact for implementations that do.
[0] https://pubs.opengroup.org/onlinepubs/9799919799/functions/timer_getoverrun.html
[1] https://pubs.opengroup.org/onlinepubs/9799919799/functions/timer_delete.html
--- .../conformance/interfaces/timer_delete/speculative/5-1.c | 5 +++++ .../conformance/interfaces/timer_delete/speculative/5-2.c | 5 +++++ .../interfaces/timer_getoverrun/speculative/6-1.c | 5 +++++ .../interfaces/timer_getoverrun/speculative/6-2.c | 5 +++++ .../interfaces/timer_getoverrun/speculative/6-3.c | 5 +++++ .../conformance/interfaces/timer_gettime/speculative/6-1.c | 5 +++++ .../conformance/interfaces/timer_gettime/speculative/6-2.c | 5 +++++ .../conformance/interfaces/timer_gettime/speculative/6-3.c | 5 +++++ .../conformance/interfaces/timer_settime/speculative/12-1.c | 5 +++++ .../conformance/interfaces/timer_settime/speculative/12-2.c | 5 +++++ .../conformance/interfaces/timer_settime/speculative/12-3.c | 5 +++++ 11 files changed, 55 insertions(+)
testcases/open_posix_testsuite/conformance/interfaces/timer_delete/speculative/5-1.c +5 -0
@@ -19,6 +19,10 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED) { +#ifdef __linux__ +printf ("Linux does not implement this optional behavior\n" ); +return PTS_UNSUPPORTED ; +#else timer_t tid;int tval =BOGUSTIMERID ; tid = (timer_t) & tval;
@@ -37,4 +41,5 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)printf ("timer_delete() did not return -1\n" );return PTS_PASS ; +#endif }
testcases/open_posix_testsuite/conformance/interfaces/timer_delete/speculative/5-2.c +5 -0
@@ -34,6 +34,10 @@ static void handler(int signo PTS_ATTRIBUTE_UNUSED) int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED) { +#ifdef __linux__ +printf ("Linux does not implement this optional behavior\n" ); +return PTS_UNSUPPORTED ; +#else struct sigevent ev;struct sigaction act;timer_t tid;
@@ -91,4 +95,5 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)printf ("fcn did not return -1\n" );return PTS_PASS ; +#endif }
testcases/open_posix_testsuite/conformance/interfaces/timer_getoverrun/speculative/6-1.c +5 -0
@@ -19,6 +19,10 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED) { +#ifdef __linux__ +printf ("Linux does not implement this optional behavior\n" ); +return PTS_UNSUPPORTED ; +#else timer_t tid;int tval =BOGUSTID ; tid = (timer_t) & tval;
@@ -36,4 +40,5 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)printf ("fcn did not return -1\n" );return PTS_PASS ; +#endif }
testcases/open_posix_testsuite/conformance/interfaces/timer_getoverrun/speculative/6-2.c +5 -0
@@ -26,6 +26,10 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED) { +#ifdef __linux__ +printf ("Linux does not implement this optional behavior\n" ); +return PTS_UNSUPPORTED ; +#else struct sigevent ev;timer_t tid;
@@ -55,4 +59,5 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)printf ("fcn did not return -1\n" );return PTS_PASS ; +#endif }
testcases/open_posix_testsuite/conformance/interfaces/timer_getoverrun/speculative/6-3.c +5 -0
@@ -23,6 +23,10 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED) { +#ifdef __linux__ +printf ("Linux does not implement this optional behavior\n" ); +return PTS_UNSUPPORTED ; +#else struct sigevent ev;timer_t tid;
@@ -47,4 +51,5 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)printf ("fcn did not return -1\n" );return PTS_PASS ; +#endif }
testcases/open_posix_testsuite/conformance/interfaces/timer_gettime/speculative/6-1.c +5 -0
@@ -19,6 +19,10 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED) { +#ifdef __linux__ +printf ("Linux does not implement this optional behavior\n" ); +return PTS_UNSUPPORTED ; +#else timer_t tid;struct itimerspec its;int tval =BOGUSTID ;
@@ -35,4 +39,5 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED) }printf ("fcn did not return -1\n" );return PTS_PASS ; +#endif }
testcases/open_posix_testsuite/conformance/interfaces/timer_gettime/speculative/6-2.c +5 -0
@@ -21,6 +21,10 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED) { +#ifdef __linux__ +printf ("Linux does not implement this optional behavior\n" ); +return PTS_UNSUPPORTED ; +#else struct sigevent ev;timer_t tid;struct itimerspec its;
@@ -45,4 +49,5 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED) }printf ("fcn did not return -1\n" );return PTS_PASS ; +#endif }
testcases/open_posix_testsuite/conformance/interfaces/timer_gettime/speculative/6-3.c +5 -0
@@ -21,6 +21,10 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED) { +#ifdef __linux__ +printf ("Linux does not implement this optional behavior\n" ); +return PTS_UNSUPPORTED ; +#else struct sigevent ev;timer_t tid;struct itimerspec its;
@@ -49,4 +53,5 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED) }printf ("fcn did not return -1\n" );return PTS_PASS ; +#endif }
testcases/open_posix_testsuite/conformance/interfaces/timer_settime/speculative/12-1.c +5 -0
@@ -18,6 +18,10 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED) { +#ifdef __linux__ +printf ("Linux does not implement this optional behavior\n" ); +return PTS_UNSUPPORTED ; +#else timer_t tid;struct itimerspec its;int tval =BOGUSTID ;
@@ -39,4 +43,5 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED) }printf ("fcn did not return -1\n" );return PTS_PASS ; +#endif }
testcases/open_posix_testsuite/conformance/interfaces/timer_settime/speculative/12-2.c +5 -0
@@ -25,6 +25,10 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED) { +#ifdef __linux__ +printf ("Linux does not implement this optional behavior\n" ); +return PTS_UNSUPPORTED ; +#else struct sigevent ev;timer_t tid;struct itimerspec its;
@@ -54,4 +58,5 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED) }printf ("fcn did not return -1\n" );return PTS_PASS ; +#endif }
testcases/open_posix_testsuite/conformance/interfaces/timer_settime/speculative/12-3.c +5 -0
@@ -23,6 +23,10 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED) { +#ifdef __linux__ +printf ("Linux does not implement this optional behavior\n" ); +return PTS_UNSUPPORTED ; +#else struct sigevent ev;timer_t tid;struct itimerspec its;
@@ -56,4 +60,5 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED) }printf ("fcn did not return -1\n" );return PTS_PASS ; +#endif }
Signature · 5 lines
2.55.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Avinesh,
testcases/open_posix_testsuite/conformance/interfaces/timer_delete/speculative/5-1.c line 25
Quoted 6 lines · Avinesh Kumar
openposix: timer_*/speculative: Skip untestable optional behavior on Linux
testcases/open_posix_testsuite/conformance/interfaces/timer_delete/speculative/5-1.c +4
-0+#ifdef __linux__ +printf ("Linux does not implement this optional behavior\n" ); +return PTS_UNSUPPORTED ; +#else
Could these branches be removed from all eleven tests? Linux 7.2 uses scoped_timer_get_or_fail() to return -EINVAL for invalid timer IDs in timer_gettime(), timer_getoverrun(), timer_settime(), and timer_delete(). The installed man pages document the same EINVAL result.
More directly, every pre-patch test reports errno == EINVAL and returns PTS_PASS when built and run on Linux. The platform check therefore replaces working coverage with PTS_UNSUPPORTED, and the quoted runtime message is not accurate.
Verdict - Needs revision
---
Note:
The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.
Regards,
LTP AI Reviewer
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Avinesh,
Quoted 9 lines
testcases/open_posix_testsuite/conformance/interfaces/timer_delete/speculative/5-1.c line 25
Quoted 4 lines
testcases/open_posix_testsuite/conformance/interfaces/timer_delete/speculative/5-1.c +4
-0+#ifdef __linux__ +printf ("Linux does not implement this optional behavior\n" ); +return PTS_UNSUPPORTED ; +#else Could these branches be removed from all eleven tests? Linux 7.2 uses
scoped_timer_get_or_fail()to return-EINVALfor invalid timer IDs intimer_gettime(),timer_getoverrun(),timer_settime(), andtimer_delete(). The installed man pages document the same EINVAL result.
can you please take a look at this?
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 12 lines
Review
Quoted 6 lines
openposix: timer_*/speculative: Skip untestable optional behavior on Linux
(quoted diff) +4
-0+#ifdef __linux__ + printf("Linux does not implement this optional behavior\n"); + return PTS_UNSUPPORTED; +#elseCould these branches be removed from all eleven tests? Linux 7.2 uses
scoped_timer_get_or_fail()to return-EINVALfor invalid timer IDs intimer_gettime(),timer_getoverrun(),timer_settime(), andtimer_delete(). The installed man pages document the same EINVAL result.
Quoted 4 lines
More directly, every pre-patch test reports errno == EINVAL and returns
PTS_PASSwhen built and run on Linux. The platform check therefore replaces working coverage withPTS_UNSUPPORTED, and the quoted runtime message is not accurate.
That's the kernel part, apparently it's more complex in libc.
Libc has two types of timer_t values, either it's a directly kernel timer id (small int) or a pointer to a structure that holds the id.
The timer libc functions, before calling the kernel syscall, convert the libc timer id into kernel timer id with:
static inline kernel_timer_ttimerid_to_kernel_timer (timer_t timerid)
{
if (timer_is_sigev_thread (timerid))
return timerid_to_timer (timerid)->ktimerid;
else
return (kernel_timer_t) ((uintptr_t) timerid);
}The library does a bit of magic with the pointers:
https://codebrowser.dev/glibc/glibc/sysdeps/unix/sysv/linux/kernel-posix-timers.h.html
But overall it checks the MSB bit of the pointer to figure out if it's kernel timer id which should be passed verbatim, or a structure that needs to be dereferenced.
Looking at the timer_gettime/speculative/6-1.c we do pass a pointer to the stack (instead of the invalid value) which on 32bit may be an address with the MSB bit set. So this triggers undefined behavior, since glibc thinks it's a pointer to it's internal data structure, but the real pointer the glibc exports as the timer is bit-shifted. Hence we access random and possibly invalid address. With some luck that address is accesible and contains non-zero data and we end up passing invalid timer ID to the kernel, but when I straced the test, the value was pretty much random.
With that in mind, we can fix the test with passing the BOGUSID instead of random stack pointer:
testcases/open_posix_testsuite/conformance/interfaces/timer_gettime/speculative/6-1.c +1 -2
@@ -21,8 +21,7 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED) {timer_t tid;struct itimerspec its; -int tval =BOGUSTID ; - tid = (timer_t) & tval; + tid = (timer_t )BOGUSTID ;if (timer_gettime (tid, &its) ==-1 ) {if (EINVAL == errno) {printf ("fcn returned -1 and errno==EINVAL\n" );
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Cyril,
Quoted 43 lines · Cyril Hrubis
Hi!
Quoted 12 lines
Review
Quoted 6 lines
openposix: timer_*/speculative: Skip untestable optional behavior on Linux
(quoted diff) +4
-0+#ifdef __linux__ + printf("Linux does not implement this optional behavior\n"); + return PTS_UNSUPPORTED; +#elseCould these branches be removed from all eleven tests? Linux 7.2 uses
scoped_timer_get_or_fail()to return-EINVALfor invalid timer IDs intimer_gettime(),timer_getoverrun(),timer_settime(), andtimer_delete(). The installed man pages document the same EINVAL result.Quoted 4 lines
More directly, every pre-patch test reports errno == EINVAL and returns
PTS_PASSwhen built and run on Linux. The platform check therefore replaces working coverage withPTS_UNSUPPORTED, and the quoted runtime message is not accurate.That's the kernel part, apparently it's more complex in libc.
Libc has two types of
timer_tvalues, either it's a directly kernel timer id (small int) or a pointer to a structure that holds the id.The timer libc functions, before calling the kernel syscall, convert the libc timer id into kernel timer id with:
static inline
kernel_timer_ttimerid_to_kernel_timer(timer_ttimerid){ if (timer_is_sigev_thread (timerid)) return timerid_to_timer (timerid)->ktimerid; else return (kernel_timer_t) ((uintptr_t) timerid); }The library does a bit of magic with the pointers:
https://codebrowser.dev/glibc/glibc/sysdeps/unix/sysv/linux/kernel-posix-timers.h.html
But overall it checks the MSB bit of the pointer to figure out if it's kernel timer id which should be passed verbatim, or a structure that needs to be dereferenced.
Quoted 10 lines
Looking at the timer_gettime/speculative/6-1.c we do pass a pointer to the stack (instead of the invalid value) which on 32bit may be an address with the MSB bit set. So this triggers undefined behavior, since glibc thinks it's a pointer to it's internal data structure, but the real pointer the glibc exports as the timer is bit-shifted. Hence we access random and possibly invalid address. With some luck that address is accesible and contains non-zero data and we end up passing invalid timer ID to the kernel, but when I straced the test, the value was pretty much random.
I was also trying to understand the sign bit usage and bit shifting magic in this glibc code, causing this issue. Thanks for explaining this clearly. I have send revised patch -
https://lore.kernel.org/ltp/[email protected]/T/#u
Regards,
Avinesh
Quoted 18 lines
With that in mind, we can fix the test with passing the BOGUSID instead of random stack pointer:
testcases/open_posix_testsuite/conformance/interfaces/timer_gettime/speculative/6-1.c +1
-2@@ -21,8 +21,7 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED) {timer_t tid;struct itimerspec its; -int tval =BOGUSTID ; - tid = (timer_t) & tval; + tid = (timer_t )BOGUSTID ;if (timer_gettime (tid, &its) ==-1 ) {if (EINVAL == errno) {printf ("fcn returned -1 and errno==EINVAL\n" );
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp