From 309597bbdac98485752ea938cf9c33f18ffa24be Mon Sep 17 00:00:00 2001 From: Alexander Boettcher Date: Thu, 18 Jun 2020 14:31:38 +0200 Subject: [PATCH] timeout fw: be robust against past _timestamp() _timestamp() returns CPU local values which may not be in sync with _ts taken from another CPU. Be robust and don't produce wraparound/negative timeout values. Issue #3657 --- repos/base/src/lib/timeout/timer_connection_time.cc | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/repos/base/src/lib/timeout/timer_connection_time.cc b/repos/base/src/lib/timeout/timer_connection_time.cc index 8c8f8408b3..83caf09e55 100644 --- a/repos/base/src/lib/timeout/timer_connection_time.cc +++ b/repos/base/src/lib/timeout/timer_connection_time.cc @@ -166,16 +166,19 @@ Duration Timer::Connection::curr_time() mutex_guard.destruct(); /* interpolate time difference since the last real time update */ - Timestamp const ts_diff = _timestamp() - ts; - uint64_t const us_diff = _ts_to_us_ratio(ts_diff, us_to_ts_factor, - us_to_ts_factor_shift); + Timestamp const ts_current_cpu = _timestamp(); + Timestamp const ts_diff = (ts_current_cpu < ts) ? 0 : ts_current_cpu - ts; + uint64_t const us_diff = _ts_to_us_ratio(ts_diff, us_to_ts_factor, + us_to_ts_factor_shift); interpolated_time.add(Microseconds(us_diff)); } else { + Timestamp const us = elapsed_us(); + Timestamp const us_diff = (us < _us) ? 0 : us - _us; /* use remote timer instead of timestamps */ - interpolated_time.add(Microseconds(elapsed_us() - _us)); + interpolated_time.add(Microseconds(us_diff)); mutex_guard.destruct(); }