Coverage Report

Created: 2022-07-22 12:05

/libfido2/fuzz/clock.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2021 Yubico AB. All rights reserved.
3
 * Use of this source code is governed by a BSD-style
4
 * license that can be found in the LICENSE file.
5
 */
6
7
#include <stdint.h>
8
#include <time.h>
9
10
#include "mutator_aux.h"
11
12
/*
13
 * A pseudo-random monotonic clock with a probabilistic discontinuity to
14
 * the end of time (as measured by struct timespec).
15
 */
16
17
extern int prng_up;
18
extern int __wrap_clock_gettime(clockid_t, struct timespec *);
19
extern int __real_clock_gettime(clockid_t, struct timespec *);
20
extern int __wrap_usleep(unsigned int);
21
static TLS struct timespec fuzz_clock;
22
23
static void
24
tick(unsigned int usec)
25
523k
{
26
523k
        long long drift;
27
28
        /*
29
         * Simulate a jump to the end of time with 0.125% probability.
30
         * This condition should be gracefully handled by callers of
31
         * clock_gettime().
32
         */
33
523k
        if (uniform_random(800) < 1) {
34
871
                fuzz_clock.tv_sec = LLONG_MAX;
35
871
                fuzz_clock.tv_nsec = LONG_MAX;
36
871
                return;
37
871
        }
38
39
522k
        drift = usec * 1000LL + (long long)uniform_random(10000000); /* 10ms */
40
522k
        if (LLONG_MAX - drift < (long long)fuzz_clock.tv_nsec) {
41
723
                fuzz_clock_reset(); /* Not much we can do here. */
42
521k
        } else if (drift + (long long)fuzz_clock.tv_nsec < 1000000000) {
43
472k
                fuzz_clock.tv_nsec += (long)(drift);
44
472k
        } else {
45
49.1k
                fuzz_clock.tv_sec  += (long)(drift / 1000000000);
46
49.1k
                fuzz_clock.tv_nsec += (long)(drift % 1000000000);
47
49.1k
        }
48
522k
}
49
50
int
51
__wrap_clock_gettime(clockid_t clk_id, struct timespec *tp)
52
523k
{
53
523k
        if (!prng_up || clk_id != CLOCK_MONOTONIC)
54
0
                return __real_clock_gettime(clk_id, tp);
55
523k
        if (uniform_random(400) < 1)
56
1.13k
                return -1;
57
58
522k
        tick(0);
59
522k
        *tp = fuzz_clock;
60
61
522k
        return 0;
62
523k
}
63
64
int
65
__wrap_usleep(unsigned int usec)
66
754
{
67
754
        if (uniform_random(400) < 1)
68
3
                return -1;
69
70
751
        tick(usec);
71
72
751
        return 0;
73
754
}
74
75
void
76
fuzz_clock_reset(void)
77
12.1k
{
78
12.1k
        memset(&fuzz_clock, 0, sizeof(fuzz_clock));
79
12.1k
}