tvdiff.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "lib/time/tvdiff.h"
  6. #include "lib/cc/compat_compiler.h"
  7. #include "lib/log/torlog.h"
  8. #ifdef _WIN32
  9. #include <winsock2.h>
  10. #endif
  11. #ifdef HAVE_SYS_TIME_H
  12. #include <sys/time.h>
  13. #endif
  14. #define TOR_USEC_PER_SEC 1000000
  15. /** Return the difference between start->tv_sec and end->tv_sec.
  16. * Returns INT64_MAX on overflow and underflow.
  17. */
  18. static int64_t
  19. tv_secdiff_impl(const struct timeval *start, const struct timeval *end)
  20. {
  21. const int64_t s = (int64_t)start->tv_sec;
  22. const int64_t e = (int64_t)end->tv_sec;
  23. /* This may not be the most efficient way of implemeting this check,
  24. * but it's easy to see that it's correct and doesn't overflow */
  25. if (s > 0 && e < INT64_MIN + s) {
  26. /* s is positive: equivalent to e - s < INT64_MIN, but without any
  27. * overflow */
  28. return INT64_MAX;
  29. } else if (s < 0 && e > INT64_MAX + s) {
  30. /* s is negative: equivalent to e - s > INT64_MAX, but without any
  31. * overflow */
  32. return INT64_MAX;
  33. }
  34. return e - s;
  35. }
  36. /** Return the number of microseconds elapsed between *start and *end.
  37. * Returns LONG_MAX on overflow and underflow.
  38. */
  39. long
  40. tv_udiff(const struct timeval *start, const struct timeval *end)
  41. {
  42. /* Sanity check tv_usec */
  43. if (start->tv_usec > TOR_USEC_PER_SEC || start->tv_usec < 0) {
  44. log_warn(LD_GENERAL, "comparing times on microsecond detail with bad "
  45. "start tv_usec: %"PRId64 " microseconds",
  46. (int64_t)start->tv_usec);
  47. return LONG_MAX;
  48. }
  49. if (end->tv_usec > TOR_USEC_PER_SEC || end->tv_usec < 0) {
  50. log_warn(LD_GENERAL, "comparing times on microsecond detail with bad "
  51. "end tv_usec: %"PRId64 " microseconds",
  52. (int64_t)end->tv_usec);
  53. return LONG_MAX;
  54. }
  55. /* Some BSDs have struct timeval.tv_sec 64-bit, but time_t (and long) 32-bit
  56. */
  57. int64_t udiff;
  58. const int64_t secdiff = tv_secdiff_impl(start, end);
  59. /* end->tv_usec - start->tv_usec can be up to 1 second either way */
  60. if (secdiff > (int64_t)(LONG_MAX/1000000 - 1) ||
  61. secdiff < (int64_t)(LONG_MIN/1000000 + 1)) {
  62. log_warn(LD_GENERAL, "comparing times on microsecond detail too far "
  63. "apart: %"PRId64 " seconds", (secdiff));
  64. return LONG_MAX;
  65. }
  66. /* we'll never get an overflow here, because we check that both usecs are
  67. * between 0 and TV_USEC_PER_SEC. */
  68. udiff = secdiff*1000000 + ((int64_t)end->tv_usec - (int64_t)start->tv_usec);
  69. /* Some compilers are smart enough to work out this is a no-op on L64 */
  70. #if SIZEOF_LONG < 8
  71. if (udiff > (int64_t)LONG_MAX || udiff < (int64_t)LONG_MIN) {
  72. return LONG_MAX;
  73. }
  74. #endif
  75. return (long)udiff;
  76. }
  77. /** Return the number of milliseconds elapsed between *start and *end.
  78. * If the tv_usec difference is 500, rounds away from zero.
  79. * Returns LONG_MAX on overflow and underflow.
  80. */
  81. long
  82. tv_mdiff(const struct timeval *start, const struct timeval *end)
  83. {
  84. /* Sanity check tv_usec */
  85. if (start->tv_usec > TOR_USEC_PER_SEC || start->tv_usec < 0) {
  86. log_warn(LD_GENERAL, "comparing times on millisecond detail with bad "
  87. "start tv_usec: %"PRId64 " microseconds",
  88. (int64_t)start->tv_usec);
  89. return LONG_MAX;
  90. }
  91. if (end->tv_usec > TOR_USEC_PER_SEC || end->tv_usec < 0) {
  92. log_warn(LD_GENERAL, "comparing times on millisecond detail with bad "
  93. "end tv_usec: %"PRId64 " microseconds",
  94. (int64_t)end->tv_usec);
  95. return LONG_MAX;
  96. }
  97. /* Some BSDs have struct timeval.tv_sec 64-bit, but time_t (and long) 32-bit
  98. */
  99. int64_t mdiff;
  100. const int64_t secdiff = tv_secdiff_impl(start, end);
  101. /* end->tv_usec - start->tv_usec can be up to 1 second either way, but the
  102. * mdiff calculation may add another temporary second for rounding.
  103. * Whether this actually causes overflow depends on the compiler's constant
  104. * folding and order of operations. */
  105. if (secdiff > (int64_t)(LONG_MAX/1000 - 2) ||
  106. secdiff < (int64_t)(LONG_MIN/1000 + 1)) {
  107. log_warn(LD_GENERAL, "comparing times on millisecond detail too far "
  108. "apart: %"PRId64 " seconds", (int64_t)secdiff);
  109. return LONG_MAX;
  110. }
  111. /* Subtract and round */
  112. mdiff = secdiff*1000 +
  113. /* We add a million usec here to ensure that the result is positive,
  114. * so that the round-towards-zero behavior of the division will give
  115. * the right result for rounding to the nearest msec. Later we subtract
  116. * 1000 in order to get the correct result.
  117. * We'll never get an overflow here, because we check that both usecs are
  118. * between 0 and TV_USEC_PER_SEC. */
  119. ((int64_t)end->tv_usec - (int64_t)start->tv_usec + 500 + 1000000) / 1000
  120. - 1000;
  121. /* Some compilers are smart enough to work out this is a no-op on L64 */
  122. #if SIZEOF_LONG < 8
  123. if (mdiff > (int64_t)LONG_MAX || mdiff < (int64_t)LONG_MIN) {
  124. return LONG_MAX;
  125. }
  126. #endif
  127. return (long)mdiff;
  128. }
  129. /**
  130. * Converts timeval to milliseconds.
  131. */
  132. int64_t
  133. tv_to_msec(const struct timeval *tv)
  134. {
  135. int64_t conv = ((int64_t)tv->tv_sec)*1000L;
  136. /* Round ghetto-style */
  137. conv += ((int64_t)tv->tv_usec+500)/1000L;
  138. return conv;
  139. }