tvdiff.c 5.3 KB

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