From 97cf8529cbd5af0498b44f7d96d930aa0480e7e1 Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Thu, 8 Oct 2026 07:55:24 +0200 Subject: [PATCH] rtems: Fix the counter conversion overflows The 64-bit product of the counter ticks and the scaler overflowed. rtems_counter_ticks_to_sbintime() returned a wrong value for more than 2 s. rtems_counter_ticks_to_nanoseconds() returned a wrong value for more than 4.29 s, where the product overflowed and the cast to 32 bits truncated the result. The test framework measures durations with rtems_counter_ticks_to_sbintime(). On a 25 MHz counter, the performance test suite reported 107 s for a run of 731 s. Compute the 96-bit product from two 32-bit by 32-bit products. The result is the same as before for each value which did not overflow. Return the nanoseconds with 64 bits. The test spcpucounter01 checked the conversions at one second alone. Check them at three seconds and at the maximum counter ticks. Close #5806. Assisted-by: Claude:claude-opus-5-5 claude-code Signed-off-by: Sebastian Huber --- cpukit/sapi/src/cpucounterconverter.c | 25 ++++++++++++- testsuites/sptests/spcpucounter01/init.c | 47 ++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/cpukit/sapi/src/cpucounterconverter.c b/cpukit/sapi/src/cpucounterconverter.c index 08e43ac2d6..11a85f0e6c 100644 --- a/cpukit/sapi/src/cpucounterconverter.c +++ b/cpukit/sapi/src/cpucounterconverter.c @@ -50,9 +50,30 @@ static uint64_t to_sbt_scaler; static uint64_t from_sbt_scaler; +/* + * Returns the product of the value and the scaler shifted right by the shift + * value. The product of the 32-bit value and the 64-bit scaler has 96 bits. + * The two partial products fit into 64 bits each. The shift value is at + * most 32. + */ +static uint64_t rtems_counter_scale( + uint32_t value, + uint64_t scaler, + unsigned int shift +) +{ + uint64_t high; + uint64_t low; + + high = (uint64_t) value * (uint32_t) ( scaler >> 32 ); + low = (uint64_t) value * (uint32_t) scaler; + + return ( high << ( 32 - shift ) ) + ( low >> shift ); +} + uint64_t rtems_counter_ticks_to_nanoseconds( rtems_counter_ticks ticks ) { - return (uint32_t) ( ( ticks * to_ns_scaler ) >> 32 ); + return rtems_counter_scale( ticks, to_ns_scaler, 32 ); } rtems_counter_ticks rtems_counter_nanoseconds_to_ticks( uint32_t nanoseconds ) @@ -62,7 +83,7 @@ rtems_counter_ticks rtems_counter_nanoseconds_to_ticks( uint32_t nanoseconds ) int64_t rtems_counter_ticks_to_sbintime( rtems_counter_ticks ticks ) { - return (int64_t) ( ( ticks * to_sbt_scaler ) >> 31 ); + return (int64_t) rtems_counter_scale( ticks, to_sbt_scaler, 31 ); } rtems_counter_ticks rtems_counter_sbintime_to_ticks( int64_t sbt ) diff --git a/testsuites/sptests/spcpucounter01/init.c b/testsuites/sptests/spcpucounter01/init.c index c6c7abec40..bdb6c1b890 100644 --- a/testsuites/sptests/spcpucounter01/init.c +++ b/testsuites/sptests/spcpucounter01/init.c @@ -64,6 +64,52 @@ static rtems_interval sync_with_clock_tick( void ) return current; } +/* + * The conversion rounds up. A result below the exact value yields a large + * error. + */ +static uint64_t nanoseconds_error( + rtems_counter_ticks ticks, + uint32_t frequency +) +{ + uint64_t expected; + + expected = (uint64_t) ticks * UINT64_C( 1000000000 ) / frequency; + + return rtems_counter_ticks_to_nanoseconds( ticks ) - expected; +} + +static uint64_t sbintime_error( rtems_counter_ticks ticks, uint32_t frequency ) +{ + uint64_t expected; + + expected = ( (uint64_t) ticks << 32 ) / frequency; + + return (uint64_t) rtems_counter_ticks_to_sbintime( ticks ) - expected; +} + +static void test_converter_large_values( void ) +{ + rtems_counter_ticks max; + rtems_counter_ticks three_seconds; + uint32_t frequency; + + max = (rtems_counter_ticks) -1; + frequency = rtems_counter_frequency(); + + if ( frequency <= max / 3 ) { + three_seconds = 3 * frequency; + } else { + three_seconds = max; + } + + rtems_test_assert( nanoseconds_error( three_seconds, frequency ) <= 1 ); + rtems_test_assert( nanoseconds_error( max, frequency ) <= 1 ); + rtems_test_assert( sbintime_error( three_seconds, frequency ) <= 2 ); + rtems_test_assert( sbintime_error( max, frequency ) <= 2 ); +} + static void test_converter( void ) { CPU_Counter_ticks frequency; @@ -282,6 +328,7 @@ static void Init( rtems_task_argument arg ) test_delay_ticks( ctx ); test_overheads( ctx ); test_converter(); + test_converter_large_values(); test_report( ctx ); TEST_END();