Skip to content

Commit

Permalink
Merge branch 'john/erts/fix-win64-monotonic-time-overflow/GH-8186/OTP…
Browse files Browse the repository at this point in the history
…-19036' into maint-26

* john/erts/fix-win64-monotonic-time-overflow/GH-8186/OTP-19036:
  erts: Fix overflow when calculating monotonic start_offset
  • Loading branch information
Erlang/OTP committed Apr 12, 2024
2 parents b2e8d7b + db9977b commit 5bb3833
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions erts/emulator/beam/erl_time_sup.c
Original file line number Diff line number Diff line change
Expand Up @@ -1001,17 +1001,24 @@ erts_init_time_sup(int time_correction, ErtsTimeWarpMode time_warp_mode)
native_offset = erts_time_sup__.r.o.start - ERTS_MONOTONIC_BEGIN;
abs_native_offset = native_offset;
#else /* ARCH_64 */
if (ERTS_MONOTONIC_TIME_UNIT <= 10*1000*1000) {
erts_time_sup__.r.o.start = 0;
native_offset = -ERTS_MONOTONIC_BEGIN;
abs_native_offset = ERTS_MONOTONIC_BEGIN;
}
else {
erts_time_sup__.r.o.start = ((ErtsMonotonicTime) MIN_SMALL);
erts_time_sup__.r.o.start /= ERTS_MONOTONIC_TIME_UNIT;
erts_time_sup__.r.o.start *= ERTS_MONOTONIC_TIME_UNIT;
native_offset = erts_time_sup__.r.o.start - ERTS_MONOTONIC_BEGIN;
abs_native_offset = -1*native_offset;
{
/* To keep monotonic times as smalls as long as possible, we want to
* have a start offset as close to MIN_SMALL as possible without it
* overflowing a small when converted to nanoseconds. */
static const Uint64 nano = (1000 * 1000 * 1000);

if (ERTS_MONOTONIC_TIME_UNIT < nano) {
erts_time_sup__.r.o.start =
-(ErtsMonotonicTime)((MAX_SMALL / nano) *
ERTS_MONOTONIC_TIME_UNIT);
} else {
erts_time_sup__.r.o.start = (ErtsMonotonicTime)MIN_SMALL;
erts_time_sup__.r.o.start /= ERTS_MONOTONIC_TIME_UNIT;
erts_time_sup__.r.o.start *= ERTS_MONOTONIC_TIME_UNIT;
}

native_offset = erts_time_sup__.r.o.start - ERTS_MONOTONIC_BEGIN;
abs_native_offset = -native_offset;
}
#endif

Expand Down

0 comments on commit 5bb3833

Please sign in to comment.