src: fix calculation of fuzzing time in statistics

When the computer is suspended during a fuzzing session,
the time spent in suspended state is counted as a "run time"
on a statistics screen.

The time returned by `gettimeofday(2)` is affected by discontinuous
jumps in the system time. It is better using `clock_gettime(2)`.

The patch replace `gettimeofday` with `clock_gettime` [1].
`clock_gettime` uses a CLOCK_MONOTONIC_COARSE clock type,
it is faster than CLOCK_MONOTONIC, but still has resolution (~1ms)
that is adequate for our purposes. However, CLOCK_MONOTONIC_COARSE
is a Linux-specific clock variant, so on macOS it is replaced
with CLOCK_MONOTONIC, and with CLOCK_MONOTONIC_FAST on FreeBSD [2].

Closes #1241

1. https://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_gettime.html
2. https://man.freebsd.org/cgi/man.cgi?query=clock_gettime
This commit is contained in:
Sergey Bronnikov
2024-03-30 11:26:38 +03:00
committed by Sergey Bronnikov
parent 775861ea94
commit 5ffc8c7076

View File

@ -34,6 +34,7 @@
#endif #endif
#include <string.h> #include <string.h>
#include <strings.h> #include <strings.h>
#include <time.h>
#include <math.h> #include <math.h>
#include <sys/mman.h> #include <sys/mman.h>
@ -58,6 +59,26 @@ u8 last_intr = 0;
#define AFL_PATH "/usr/local/lib/afl/" #define AFL_PATH "/usr/local/lib/afl/"
#endif #endif
/* - Some BSD (i.e.: FreeBSD) offer the FAST clock source as
* equivalent to Linux COARSE clock source. Aliasing COARSE to
* FAST on such systems when COARSE is not already defined.
* - macOS has no support of CLOCK_MONOTONIC_COARSE clock type.
*/
#if defined (OS_DARWIN) || defined (OS_SUNOS)
# define CLOCK_MONOTONIC_COARSE CLOCK_MONOTONIC
#elif defined (OS_FREEBSD)
# define CLOCK_MONOTONIC_COARSE CLOCK_MONOTONIC_FAST
#endif
/* Convert seconds to milliseconds. */
#define SEC_TO_MS(sec) ((sec)*1000)
/* Convert seconds to microseconds. */
#define SEC_TO_US(sec) ((sec)*1000000)
/* Convert nanoseconds to milliseconds. */
#define NS_TO_MS(ns) ((ns)/1000000)
/* Convert nanoseconds to microseconds. */
#define NS_TO_US(ns) ((ns)/1000)
void *afl_memmem(const void *haystack, size_t haystacklen, const void *needle, void *afl_memmem(const void *haystack, size_t haystacklen, const void *needle,
size_t needlelen) { size_t needlelen) {
@ -973,27 +994,27 @@ void read_bitmap(u8 *fname, u8 *map, size_t len) {
/* Get unix time in milliseconds */ /* Get unix time in milliseconds */
inline u64 get_cur_time(void) { inline u64 get_cur_time(void) {
struct timespec ts;
int rc = clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
if (rc == -1) {
PFATAL("Failed to obtain timestamp (errno = %i: %s)\n",
errno, strerror(errno));
}
struct timeval tv; return SEC_TO_MS((uint64_t)ts.tv_sec) + NS_TO_MS((uint64_t)ts.tv_nsec);
struct timezone tz;
gettimeofday(&tv, &tz);
return (tv.tv_sec * 1000ULL) + (tv.tv_usec / 1000);
} }
/* Get unix time in microseconds */ /* Get unix time in microseconds */
u64 get_cur_time_us(void) { u64 get_cur_time_us(void) {
struct timespec ts;
int rc = clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
if (rc == -1) {
PFATAL("Failed to obtain timestamp (errno = %i: %s)\n",
errno, strerror(errno));
}
struct timeval tv; return SEC_TO_US((uint64_t)ts.tv_sec) + NS_TO_US((uint64_t)ts.tv_nsec);
struct timezone tz;
gettimeofday(&tv, &tz);
return (tv.tv_sec * 1000000ULL) + tv.tv_usec;
} }
/* Describe integer. The buf should be /* Describe integer. The buf should be