File 1290-erts-Fix-overflow-when-calculating-monotonic-start_o.patch of Package erlang
From db9977b988489f0ed5369dfad5d9f932a76a8f2d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?John=20H=C3=B6gberg?= <john@erlang.org>
Date: Tue, 27 Feb 2024 09:11:12 +0100
Subject: [PATCH] erts: Fix overflow when calculating monotonic start_offset
---
erts/emulator/beam/erl_time_sup.c | 29 ++++++++++++++++++-----------
1 file changed, 18 insertions(+), 11 deletions(-)
diff --git a/erts/emulator/beam/erl_time_sup.c b/erts/emulator/beam/erl_time_sup.c
index 0dc85edd20..f63bda3ee9 100644
--- a/erts/emulator/beam/erl_time_sup.c
+++ b/erts/emulator/beam/erl_time_sup.c
@@ -992,17 +992,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
--
2.35.3