tvdiff.c 5.1 KB

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