Skip to content

sys: timeutil: inter-tick timespec values incorrectly rounded and misaligned to tick boundaries #92908

@cfriedt

Description

@cfriedt

Describe the bug

The function timespec_to_timeout() in sys/timeutil.h computes incorrect values where inter-tick timespec values should be aligned to tick boundaries.

Moreover, in some cases the returned timeouts are unsafe. Specifically, when a non-zero timespec is converted to a timeout, the current behaviour is to round down to K_NO_WAIT, which is most certainly not semantically equivalent to a minimal, non-zero time duration.

  • What target platform are you using? qemu_malta/qemu_malta_be, qemu_leon3/leon3, qemu_rx/r5f562n8, qemu_xtensa/dc233c/mmu, qemu_riscv32e, qemu_x86_64/atom, mps3_corstone300/an547, qemu_cortex_a53, qemu_arc/qemu_arc/hs6x
  • What have you tried to diagnose or workaround this issue? Fixed it in lib: os: timespec_util: reimplement and test with broad inputs #92709
  • Is this a regression? Technically no, it's a new bug that was not caught by testing

Regression

  • This is a regression.

Steps to reproduce

Steps to reproduce the behaviour:

  1. gh pr checkout 92709
  2. git reset --hard HEAD~2
  3. git rebase -i 38e6c758dbf~1 (switch pick to d to delete the code commit)
  4. twister -T tests/lib/timespec_util

Relevant log output

*** Booting Zephyr OS build v4.2.0-rc2-37-g9acc19366c73 ***
Running TESTSUITE timeutil_api
===================================================================
CONFIG_SYS_CLOCK_TICKS_PER_SEC=100
CONFIG_TIMEOUT_64BIT=y
K_TICK_MIN: 1
K_TICK_MAX: 9223372036854775807
K_TS_MIN: {0, 10000000}
K_TS_MAX: {92233720368547758, 70000000}
SYS_TIME_T_MIN: -9223372036854775808
SYS_TIME_T_MAX: 9223372036854775807
===================================================================
START - test_K_TICKS_TO_NSECS
 PASS - test_K_TICKS_TO_NSECS in 0.001 seconds
===================================================================
START - test_K_TICKS_TO_SECS
 PASS - test_K_TICKS_TO_SECS in 0.001 seconds
===================================================================
START - test_timespec_add
 PASS - test_timespec_add in 0.001 seconds
===================================================================
START - test_timespec_compare
 PASS - test_timespec_compare in 0.001 seconds
===================================================================
START - test_timespec_equal
 PASS - test_timespec_equal in 0.001 seconds
===================================================================
START - test_timespec_from_timeout
 PASS - test_timespec_from_timeout in 0.001 seconds
===================================================================
START - test_timespec_is_valid
 PASS - test_timespec_is_valid in 0.001 seconds
===================================================================
START - test_timespec_negate
 PASS - test_timespec_negate in 0.001 seconds
===================================================================
START - test_timespec_normalize
 PASS - test_timespec_normalize in 0.001 seconds
===================================================================
START - test_timespec_sub
 PASS - test_timespec_sub in 0.001 seconds
===================================================================
START - test_timespec_to_timeout

    Expectation failed at WEST_TOPDIR/zephyr/tests/lib/timespec_util/src/main.c:459: timeutil_api_test_timespec_to_timeout: (actual.ticks not equal to K_TICK_MIN)
7: {0} and {1} are unexpectedly different

    Expectation failed at WEST_TOPDIR/zephyr/tests/lib/timespec_util/src/main.c:459: timeutil_api_test_timespec_to_timeout: (actual.ticks not equal to K_TICK_MIN)
8: {0} and {1} are unexpectedly different

    Expectation failed at WEST_TOPDIR/zephyr/tests/lib/timespec_util/src/main.c:459: timeutil_api_test_timespec_to_timeout: (actual.ticks not equal to K_TICK_MIN)
10: {0} and {1} are unexpectedly different

    Expectation failed at WEST_TOPDIR/zephyr/tests/lib/timespec_util/src/main.c:459: timeutil_api_test_timespec_to_timeout: (actual.ticks not equal to K_TICK_MIN)
11: {0} and {1} are unexpectedly different

    Expectation failed at WEST_TOPDIR/zephyr/tests/lib/timespec_util/src/main.c:459: timeutil_api_test_timespec_to_timeout: (actual.ticks not equal to K_TICK_MIN)
12: {0} and {1} are unexpectedly different

    Expectation failed at WEST_TOPDIR/zephyr/tests/lib/timespec_util/src/main.c:448: timeutil_api_test_timespec_to_timeout: (actual.ticks not equal to tspec->timeout.ticks)
13: {1} and {2} are unexpectedly different

    Expectation failed at WEST_TOPDIR/zephyr/tests/lib/timespec_util/src/main.c:448: timeutil_api_test_timespec_to_timeout: (actual.ticks not equal to tspec->timeout.ticks)
14: {1} and {2} are unexpectedly different

    Expectation failed at WEST_TOPDIR/zephyr/tests/lib/timespec_util/src/main.c:448: timeutil_api_test_timespec_to_timeout: (actual.ticks not equal to tspec->timeout.ticks)
15: {1} and {2} are unexpectedly different

    Expectation failed at WEST_TOPDIR/zephyr/tests/lib/timespec_util/src/main.c:448: timeutil_api_test_timespec_to_timeout: (actual.ticks not equal to tspec->timeout.ticks)
23: {999} and {1000} are unexpectedly different

    Expectation failed at WEST_TOPDIR/zephyr/tests/lib/timespec_util/src/main.c:448: timeutil_api_test_timespec_to_timeout: (actual.ticks not equal to tspec->timeout.ticks)
24: {999} and {1000} are unexpectedly different

    Expectation failed at WEST_TOPDIR/zephyr/tests/lib/timespec_util/src/main.c:448: timeutil_api_test_timespec_to_timeout: (actual.ticks not equal to tspec->timeout.ticks)
25: {999} and {1000} are unexpectedly different
timespec_to_timeout():
ts: {92233720368547758, 70000000} => to: {9223372036854775807}
 FAIL - test_timespec_to_timeout in 0.010 seconds
===================================================================
TESTSUITE timeutil_api failed.

------ TESTSUITE SUMMARY START ------

SUITE FAIL -  90.91% [timeutil_api]: pass = 10, fail = 1, skip = 0, total = 11 duration = 0.020 seconds
 - PASS - [timeutil_api.test_K_TICKS_TO_NSECS] duration = 0.001 seconds
 - PASS - [timeutil_api.test_K_TICKS_TO_SECS] duration = 0.001 seconds
 - PASS - [timeutil_api.test_timespec_add] duration = 0.001 seconds
 - PASS - [timeutil_api.test_timespec_compare] duration = 0.001 seconds
 - PASS - [timeutil_api.test_timespec_equal] duration = 0.001 seconds
 - PASS - [timeutil_api.test_timespec_from_timeout] duration = 0.001 seconds
 - PASS - [timeutil_api.test_timespec_is_valid] duration = 0.001 seconds
 - PASS - [timeutil_api.test_timespec_negate] duration = 0.001 seconds
 - PASS - [timeutil_api.test_timespec_normalize] duration = 0.001 seconds
 - PASS - [timeutil_api.test_timespec_sub] duration = 0.001 seconds
 - FAIL - [timeutil_api.test_timespec_to_timeout] duration = 0.010 seconds

------ TESTSUITE SUMMARY END ------

===================================================================
RunID: cd7397ab787586d16efe49c01f3307c8
PROJECT EXECUTION FAILED



twister -T tests/lib/timespec_util/
Renaming output directory to /home/cfriedt/zephyrproject/zephyr/twister-out.71
INFO    - Using Ninja..
INFO    - Zephyr version: v4.2.0-rc2-37-g9acc19366c73
INFO    - Using 'zephyr' toolchain.
INFO    - Selecting default platforms per testsuite scenario
INFO    - Building initial testsuite list...
INFO    - Writing JSON report /home/cfriedt/zephyrproject/zephyr/twister-out/testplan.json
INFO    - JOBS: 32
INFO    - Adding tasks to the queue...
INFO    - Added initial list of jobs to queue
ERROR   - qemu_malta/qemu_malta/be  tests/lib/timespec_util/libraries.timespec_utils.timeout_32bit FAILED: Testsuite failed
ERROR   - see: /home/cfriedt/zephyrproject/zephyr/twister-out/qemu_malta_qemu_malta_be/zephyr/tests/lib/timespec_util/libraries.timespec_utils.timeout_32bit/handler.log
ERROR   - qemu_leon3/leon3          tests/lib/timespec_util/libraries.timespec_utils   FAILED: Testsuite failed
ERROR   - see: /home/cfriedt/zephyrproject/zephyr/twister-out/qemu_leon3_leon3/zephyr/tests/lib/timespec_util/libraries.timespec_utils/handler.log
ERROR   - qemu_malta/qemu_malta/be  tests/lib/timespec_util/libraries.timespec_utils   FAILED: Testsuite failed
ERROR   - see: /home/cfriedt/zephyrproject/zephyr/twister-out/qemu_malta_qemu_malta_be/zephyr/tests/lib/timespec_util/libraries.timespec_utils/handler.log
ERROR   - qemu_rx/r5f562n8          tests/lib/timespec_util/libraries.timespec_utils.timeout_32bit FAILED: Testsuite failed
ERROR   - see: /home/cfriedt/zephyrproject/zephyr/twister-out/qemu_rx_r5f562n8/zephyr/tests/lib/timespec_util/libraries.timespec_utils.timeout_32bit/handler.log
ERROR   - qemu_xtensa/dc233c/mmu    tests/lib/timespec_util/libraries.timespec_utils.timeout_32bit FAILED: Testsuite failed
ERROR   - see: /home/cfriedt/zephyrproject/zephyr/twister-out/qemu_xtensa_dc233c_mmu/zephyr/tests/lib/timespec_util/libraries.timespec_utils.timeout_32bit/handler.log
ERROR   - qemu_riscv32e/qemu_virt_riscv32e tests/lib/timespec_util/libraries.timespec_utils FAILED: Testsuite failed
ERROR   - see: /home/cfriedt/zephyrproject/zephyr/twister-out/qemu_riscv32e_qemu_virt_riscv32e/zephyr/tests/lib/timespec_util/libraries.timespec_utils/handler.log
ERROR   - qemu_x86_64/atom          tests/lib/timespec_util/libraries.timespec_utils   FAILED: Testsuite failed
ERROR   - see: /home/cfriedt/zephyrproject/zephyr/twister-out/qemu_x86_64_atom/zephyr/tests/lib/timespec_util/libraries.timespec_utils/handler.log
ERROR   - qemu_xtensa/dc233c/mmu    tests/lib/timespec_util/libraries.timespec_utils   FAILED: Testsuite failed
ERROR   - see: /home/cfriedt/zephyrproject/zephyr/twister-out/qemu_xtensa_dc233c_mmu/zephyr/tests/lib/timespec_util/libraries.timespec_utils/handler.log
ERROR   - qemu_leon3/leon3          tests/lib/timespec_util/libraries.timespec_utils.timeout_32bit FAILED: Testsuite failed
ERROR   - see: /home/cfriedt/zephyrproject/zephyr/twister-out/qemu_leon3_leon3/zephyr/tests/lib/timespec_util/libraries.timespec_utils.timeout_32bit/handler.log
ERROR   - qemu_riscv32e/qemu_virt_riscv32e tests/lib/timespec_util/libraries.timespec_utils.timeout_32bit FAILED: Testsuite failed
ERROR   - see: /home/cfriedt/zephyrproject/zephyr/twister-out/qemu_riscv32e_qemu_virt_riscv32e/zephyr/tests/lib/timespec_util/libraries.timespec_utils.timeout_32bit/handler.log
ERROR   - mps3/corstone300/an547    tests/lib/timespec_util/libraries.timespec_utils   FAILED: Testsuite failed
ERROR   - see: /home/cfriedt/zephyrproject/zephyr/twister-out/mps3_corstone300_an547/zephyr/tests/lib/timespec_util/libraries.timespec_utils/handler.log
ERROR   - qemu_x86_64/atom          tests/lib/timespec_util/libraries.timespec_utils.timeout_32bit FAILED: Testsuite failed
ERROR   - see: /home/cfriedt/zephyrproject/zephyr/twister-out/qemu_x86_64_atom/zephyr/tests/lib/timespec_util/libraries.timespec_utils.timeout_32bit/handler.log
ERROR   - mps3/corstone300/an547    tests/lib/timespec_util/libraries.timespec_utils.timeout_32bit FAILED: Testsuite failed
ERROR   - see: /home/cfriedt/zephyrproject/zephyr/twister-out/mps3_corstone300_an547/zephyr/tests/lib/timespec_util/libraries.timespec_utils.timeout_32bit/handler.log
ERROR   - qemu_rx/r5f562n8          tests/lib/timespec_util/libraries.timespec_utils   FAILED: Testsuite failed
ERROR   - see: /home/cfriedt/zephyrproject/zephyr/twister-out/qemu_rx_r5f562n8/zephyr/tests/lib/timespec_util/libraries.timespec_utils/handler.log
ERROR   - qemu_cortex_a53/qemu_cortex_a53/smp tests/lib/timespec_util/libraries.timespec_utils FAILED: Testsuite failed
ERROR   - see: /home/cfriedt/zephyrproject/zephyr/twister-out/qemu_cortex_a53_qemu_cortex_a53_smp/zephyr/tests/lib/timespec_util/libraries.timespec_utils/handler.log
ERROR   - qemu_cortex_a53/qemu_cortex_a53/smp tests/lib/timespec_util/libraries.timespec_utils.timeout_32bit FAILED: Testsuite failed
ERROR   - see: /home/cfriedt/zephyrproject/zephyr/twister-out/qemu_cortex_a53_qemu_cortex_a53_smp/zephyr/tests/lib/timespec_util/libraries.timespec_utils.timeout_32bit/handler.log
ERROR   - qemu_arc/qemu_arc_hs6x    tests/lib/timespec_util/libraries.timespec_utils   FAILED: Testsuite failed
ERROR   - see: /home/cfriedt/zephyrproject/zephyr/twister-out/qemu_arc_qemu_arc_hs6x/zephyr/tests/lib/timespec_util/libraries.timespec_utils/handler.log
ERROR   - qemu_arc/qemu_arc_hs6x    tests/lib/timespec_util/libraries.timespec_utils.timeout_32bit FAILED: Testsuite failed
ERROR   - see: /home/cfriedt/zephyrproject/zephyr/twister-out/qemu_arc_qemu_arc_hs6x/zephyr/tests/lib/timespec_util/libraries.timespec_utils.timeout_32bit/handler.log
INFO    - Total complete:   18/  18  100%  built (not run):    0, filtered:   86, failed:   18, error:    0
INFO    - 12 test scenarios (104 configurations) selected, 86 configurations filtered (86 by static filter, 0 at runtime).
INFO    - 0 of 18 executed test configurations passed (0.00%), 0 built (not run), 18 failed, 0 errored, with no warnings in 26.81 seconds.
INFO    - 180 of 198 executed test cases passed (90.91%), 18 failed on 9 out of total 1115 platforms (0.81%).
INFO    - 18 test configurations executed on platforms, 0 test configurations were only built.
INFO    - Saving reports...
INFO    - Writing JSON report /home/cfriedt/zephyrproject/zephyr/twister-out/twister.json
INFO    - Writing xunit report /home/cfriedt/zephyrproject/zephyr/twister-out/twister.xml...
INFO    - Writing xunit report /home/cfriedt/zephyrproject/zephyr/twister-out/twister_report.xml...
INFO    - -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
INFO    - The following issues were found (showing the top 10 items):
INFO    - 1) tests/lib/timespec_util/libraries.timespec_utils on mps3/corstone300/an547 failed (Testsuite failed)
INFO    - 2) tests/lib/timespec_util/libraries.timespec_utils on qemu_xtensa/dc233c/mmu failed (Testsuite failed)
INFO    - 3) tests/lib/timespec_util/libraries.timespec_utils on qemu_riscv32e/qemu_virt_riscv32e failed (Testsuite failed)
INFO    - 4) tests/lib/timespec_util/libraries.timespec_utils on qemu_malta/qemu_malta/be failed (Testsuite failed)
INFO    - 5) tests/lib/timespec_util/libraries.timespec_utils on qemu_arc/qemu_arc_hs6x failed (Testsuite failed)
INFO    - 6) tests/lib/timespec_util/libraries.timespec_utils on qemu_rx/r5f562n8 failed (Testsuite failed)
INFO    - 7) tests/lib/timespec_util/libraries.timespec_utils on qemu_leon3/leon3 failed (Testsuite failed)
INFO    - 8) tests/lib/timespec_util/libraries.timespec_utils on qemu_x86_64/atom failed (Testsuite failed)
INFO    - 9) tests/lib/timespec_util/libraries.timespec_utils on qemu_cortex_a53/qemu_cortex_a53/smp failed (Testsuite failed)
INFO    - 10) tests/lib/timespec_util/libraries.timespec_utils.timeout_32bit on mps3/corstone300/an547 failed (Testsuite failed)
INFO    - 
INFO    - To rerun the tests, call twister using the following commandline:
INFO    - west twister -p <PLATFORM> -s <TEST ID>, for example:
INFO    - 
INFO    - west twister -p mps3/corstone300/an547 -s tests/lib/timespec_util/libraries.timespec_utils.timeout_32bit
INFO    - or with west:
INFO    - west build -p -b mps3/corstone300/an547 tests/lib/timespec_util -T libraries.timespec_utils.timeout_32bit
INFO    - -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
INFO    - Run completed

Impact

Showstopper – Prevents release or major functionality; system unusable.

Environment

  • OS: all
  • Toolchain: Zephyr SDK v0.17.2
  • v4.2.0-rc2

Additional Context

No response

Metadata

Metadata

Assignees

Labels

area: Base OSBase OS Library (lib/os)bugThe issue is a bug, or the PR is fixing a bugpriority: highHigh impact/importance bug

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions