]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Fix a completely wrong calculation in mach monotime_init_internal()
authorNick Mathewson <nickm@torproject.org>
Thu, 13 Oct 2022 17:40:10 +0000 (13:40 -0400)
committerNick Mathewson <nickm@torproject.org>
Thu, 13 Oct 2022 17:40:10 +0000 (13:40 -0400)
Bug 1: We were purporting to calculate milliseconds per tick, when we
*should* have been computing ticks per millisecond.

Bug 2: Instead of computing either one of those, we were _actually_
computing femtoseconds per tick.

These two bugs covered for one another on x86 hardware, where 1 tick
== 1 nanosecond.  But on M1 OSX, 1 tick is about 41 nanoseconds,
causing surprising results.

Fixes bug 40684; bugfix on 0.3.3.1-alpha.

changes/bug40684 [new file with mode: 0644]
src/lib/time/compat_time.c

diff --git a/changes/bug40684 b/changes/bug40684
new file mode 100644 (file)
index 0000000..8c751ed
--- /dev/null
@@ -0,0 +1,6 @@
+  o Major bugfixes (OSX):
+    - Fix coarse-time computation on Apple platforms (like Mac M1) where
+      the Mach absolute time ticks do not correspond directly to
+      nanoseconds.  Previously, we computed our shift value wrong, which
+      led us to give incorrect timing results.
+      Fixes bug 40684; bugfix on 0.3.3.1-alpha.
index 6bbad4f98a03f27fba75d289a279488b9f23dfa0..9a5ce062d702796a3dfe54dbfa63df6f1be7bcca 100644 (file)
@@ -253,11 +253,14 @@ monotime_init_internal(void)
   tor_assert(mach_time_info.denom != 0);
 
   {
-    // approximate only.
-    uint64_t ns_per_tick = mach_time_info.numer / mach_time_info.denom;
-    uint64_t ms_per_tick = ns_per_tick * ONE_MILLION;
+    // We want to compute this, approximately:
+    //   uint64_t ns_per_tick = mach_time_info.numer / mach_time_info.denom;
+    //   uint64_t ticks_per_ms = ONE_MILLION / ns_per_tick;
+    // This calculation multiplies first, though, to improve accuracy.
+    uint64_t ticks_per_ms = (ONE_MILLION * mach_time_info.denom)
+      / mach_time_info.numer;
     // requires that tor_log2(0) == 0.
-    monotime_shift = tor_log2(ms_per_tick);
+    monotime_shift = tor_log2(ticks_per_ms);
   }
   {
     // For converting ticks to milliseconds in a 32-bit-friendly way, we