tests: kqueue: fix some issues with now() on ILP32 platforms

There were ultimately two separate problems here:
- a 32-bit long cannot represent microseconds since 1970 (noted by ian)
- time_t is 32-bit on i386, so now() was wrong anyways even with the correct
  return type.

For the first, just explicitly use a uint64_t for now() and all of the
callers. For the second, we need to explicitly cast tv_sec to uint64_t
before it gets multiplied in the SEC_TO_US macro. Casting this instance
rather than generally in the macro was arbitrarily chosen simply because all
other uses are converting small relative time values.

The tests now pass on i386, at least; presumably other ILP32 will be fine
now as well.
This commit is contained in:
Kyle Evans 2020-04-20 00:47:28 +00:00
parent 00d3723fb4
commit 0fbdc3726a
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=360108

View File

@ -30,13 +30,14 @@
/* Get the current time with microsecond precision. Used for /* Get the current time with microsecond precision. Used for
* sub-second timing to make some timer tests run faster. * sub-second timing to make some timer tests run faster.
*/ */
static long static uint64_t
now(void) now(void)
{ {
struct timeval tv; struct timeval tv;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
return SEC_TO_US(tv.tv_sec) + tv.tv_usec; /* Promote potentially 32-bit time_t to uint64_t before conversion. */
return SEC_TO_US((uint64_t)tv.tv_sec) + tv.tv_usec;
} }
/* Sleep for a given number of milliseconds. The timeout is assumed to /* Sleep for a given number of milliseconds. The timeout is assumed to
@ -216,7 +217,7 @@ test_abstime(void)
{ {
const char *test_id = "kevent(EVFILT_TIMER, EV_ONESHOT, NOTE_ABSTIME)"; const char *test_id = "kevent(EVFILT_TIMER, EV_ONESHOT, NOTE_ABSTIME)";
struct kevent kev; struct kevent kev;
long end, start, stop; uint64_t end, start, stop;
const int timeout_sec = 3; const int timeout_sec = 3;
test_begin(test_id); test_begin(test_id);
@ -252,7 +253,7 @@ test_update(void)
const char *test_id = "kevent(EVFILT_TIMER (UPDATE), EV_ADD | EV_ONESHOT)"; const char *test_id = "kevent(EVFILT_TIMER (UPDATE), EV_ADD | EV_ONESHOT)";
struct kevent kev; struct kevent kev;
long elapsed; long elapsed;
long start; uint64_t start;
test_begin(test_id); test_begin(test_id);
@ -297,7 +298,7 @@ test_update_equal(void)
const char *test_id = "kevent(EVFILT_TIMER (UPDATE=), EV_ADD | EV_ONESHOT)"; const char *test_id = "kevent(EVFILT_TIMER (UPDATE=), EV_ADD | EV_ONESHOT)";
struct kevent kev; struct kevent kev;
long elapsed; long elapsed;
long start; uint64_t start;
test_begin(test_id); test_begin(test_id);
@ -341,7 +342,7 @@ test_update_expired(void)
const char *test_id = "kevent(EVFILT_TIMER (UPDATE EXP), EV_ADD | EV_ONESHOT)"; const char *test_id = "kevent(EVFILT_TIMER (UPDATE EXP), EV_ADD | EV_ONESHOT)";
struct kevent kev; struct kevent kev;
long elapsed; long elapsed;
long start; uint64_t start;
test_begin(test_id); test_begin(test_id);
@ -392,8 +393,7 @@ test_update_periodic(void)
const char *test_id = "kevent(EVFILT_TIMER (UPDATE), periodic)"; const char *test_id = "kevent(EVFILT_TIMER (UPDATE), periodic)";
struct kevent kev; struct kevent kev;
long elapsed; long elapsed;
long start; uint64_t start, stop;
long stop;
test_begin(test_id); test_begin(test_id);
@ -450,8 +450,7 @@ test_update_timing(void)
int iteration; int iteration;
int sleeptime; int sleeptime;
long elapsed; long elapsed;
long start; uint64_t start, stop;
long stop;
test_begin(test_id); test_begin(test_id);