| 1 |
#include <assert.h> |
| 2 |
#include <ck_ec.h> |
| 3 |
#include <ck_stdbool.h> |
| 4 |
|
| 5 |
#include "fuzz_harness.h" |
| 6 |
|
| 7 |
static int gettime(const struct ck_ec_ops *, struct timespec *out); |
| 8 |
static void wake32(const struct ck_ec_ops *, const uint32_t *); |
| 9 |
static void wait32(const struct ck_ec_wait_state *, const uint32_t *, |
| 10 |
uint32_t, const struct timespec *); |
| 11 |
static void wake64(const struct ck_ec_ops *, const uint64_t *); |
| 12 |
static void wait64(const struct ck_ec_wait_state *, const uint64_t *, |
| 13 |
uint64_t, const struct timespec *); |
| 14 |
|
| 15 |
static const struct ck_ec_ops test_ops = { |
| 16 |
.gettime = gettime, |
| 17 |
.wait32 = wait32, |
| 18 |
.wait64 = wait64, |
| 19 |
.wake32 = wake32, |
| 20 |
.wake64 = wake64 |
| 21 |
}; |
| 22 |
|
| 23 |
static const struct ck_ec_mode modes[] = { |
| 24 |
{ |
| 25 |
.single_producer = true, |
| 26 |
.ops = &test_ops |
| 27 |
}, |
| 28 |
{ |
| 29 |
.single_producer = false, |
| 30 |
.ops = &test_ops |
| 31 |
}, |
| 32 |
}; |
| 33 |
|
| 34 |
static bool woken = false; |
| 35 |
|
| 36 |
static int gettime(const struct ck_ec_ops *ops, struct timespec *out) |
| 37 |
{ |
| 38 |
(void)out; |
| 39 |
|
| 40 |
assert(ops == &test_ops); |
| 41 |
return -1; |
| 42 |
} |
| 43 |
|
| 44 |
static void wait32(const struct ck_ec_wait_state *state, const uint32_t *addr, |
| 45 |
uint32_t expected, const struct timespec *deadline) |
| 46 |
{ |
| 47 |
(void)addr; |
| 48 |
(void)expected; |
| 49 |
(void)deadline; |
| 50 |
|
| 51 |
assert(state->ops == &test_ops); |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
static void wait64(const struct ck_ec_wait_state *state, const uint64_t *addr, |
| 56 |
uint64_t expected, const struct timespec *deadline) |
| 57 |
{ |
| 58 |
(void)addr; |
| 59 |
(void)expected; |
| 60 |
(void)deadline; |
| 61 |
|
| 62 |
assert(state->ops == &test_ops); |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
static void wake32(const struct ck_ec_ops *ops, const uint32_t *addr) |
| 67 |
{ |
| 68 |
(void)addr; |
| 69 |
|
| 70 |
assert(ops == &test_ops); |
| 71 |
woken = true; |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
static void wake64(const struct ck_ec_ops *ops, const uint64_t *addr) |
| 76 |
{ |
| 77 |
(void)addr; |
| 78 |
|
| 79 |
assert(ops == &test_ops); |
| 80 |
woken = true; |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
/* |
| 85 |
* Check that adding a value calls the wake function when the sign bit |
| 86 |
* is set, and does not call it when the sign bit is unset (modulo |
| 87 |
* wrap-around). |
| 88 |
*/ |
| 89 |
struct example { |
| 90 |
uint64_t initial; |
| 91 |
uint64_t increment; |
| 92 |
}; |
| 93 |
|
| 94 |
const struct example examples[] = { |
| 95 |
{ INT32_MAX, 0 }, |
| 96 |
{ INT32_MAX, 1 }, |
| 97 |
{ 0 + (0U << 31), 0 }, |
| 98 |
{ 1 + (0U << 31), 0 }, |
| 99 |
{ 0 + (1U << 31), 0 }, |
| 100 |
{ 1 + (1U << 31), 0 }, |
| 101 |
|
| 102 |
{ 0 + (0U << 31), 1 }, |
| 103 |
{ 1 + (0U << 31), 1 }, |
| 104 |
{ 0 + (1U << 31), 1 }, |
| 105 |
{ 1 + (1U << 31), 1 }, |
| 106 |
|
| 107 |
{ 0 + (0U << 31), INT32_MAX }, |
| 108 |
{ 1 + (0U << 31), INT32_MAX }, |
| 109 |
{ 0 + (1U << 31), INT32_MAX }, |
| 110 |
{ 1 + (1U << 31), INT32_MAX }, |
| 111 |
|
| 112 |
{ INT64_MAX, 0 }, |
| 113 |
{ INT64_MAX, 1 }, |
| 114 |
{ 0 + (0ULL << 63), 0 }, |
| 115 |
{ 1 + (0ULL << 63), 0 }, |
| 116 |
{ 0 + (1ULL << 63), 0 }, |
| 117 |
{ 1 + (1ULL << 63), 0 }, |
| 118 |
|
| 119 |
{ 0 + (0ULL << 63), 1 }, |
| 120 |
{ 1 + (0ULL << 63), 1 }, |
| 121 |
{ 0 + (1ULL << 63), 1 }, |
| 122 |
{ 1 + (1ULL << 63), 1 }, |
| 123 |
|
| 124 |
{ 0 + (0ULL << 63), INT64_MAX }, |
| 125 |
{ 1 + (0ULL << 63), INT64_MAX }, |
| 126 |
{ 0 + (1ULL << 63), INT64_MAX }, |
| 127 |
{ 1 + (1ULL << 63), INT64_MAX }, |
| 128 |
}; |
| 129 |
|
| 130 |
static inline int test_wakeup(const struct example *example) |
| 131 |
{ |
| 132 |
for (size_t i = 0; i < 2; i++) { |
| 133 |
const struct ck_ec_mode *mode = &modes[i]; |
| 134 |
const uint32_t increment = example->increment & INT32_MAX; |
| 135 |
struct ck_ec32 ec; |
| 136 |
bool should_wake; |
| 137 |
bool may_wake; |
| 138 |
|
| 139 |
ec.counter = example->initial; |
| 140 |
should_wake = increment != 0 && (ec.counter & (1U << 31)); |
| 141 |
may_wake = should_wake || (ec.counter & (1U << 31)); |
| 142 |
|
| 143 |
woken = false; |
| 144 |
ck_ec32_add(&ec, mode, increment); |
| 145 |
assert(!should_wake || woken); |
| 146 |
assert(may_wake || !woken); |
| 147 |
assert(!woken || ck_ec32_has_waiters(&ec) == false); |
| 148 |
|
| 149 |
/* Test inc now. */ |
| 150 |
ec.counter = example->initial + increment; |
| 151 |
should_wake = ec.counter & (1U << 31); |
| 152 |
may_wake = should_wake || ((ec.counter + 1) & (1U << 31)); |
| 153 |
|
| 154 |
woken = false; |
| 155 |
ck_ec32_inc(&ec, mode); |
| 156 |
assert(!should_wake || woken); |
| 157 |
assert(may_wake || !woken); |
| 158 |
assert(!woken || ck_ec32_has_waiters(&ec) == false); |
| 159 |
} |
| 160 |
|
| 161 |
#ifdef CK_F_EC64 |
| 162 |
for (size_t i = 0; i < 2; i++) { |
| 163 |
const struct ck_ec_mode *mode = &modes[i]; |
| 164 |
const uint64_t increment = example->increment & INT64_MAX; |
| 165 |
struct ck_ec64 ec; |
| 166 |
bool should_wake; |
| 167 |
bool may_wake; |
| 168 |
|
| 169 |
ec.counter = example->initial; |
| 170 |
should_wake = increment != 0 && (ec.counter & 1); |
| 171 |
may_wake = should_wake || (ec.counter & 1); |
| 172 |
|
| 173 |
woken = false; |
| 174 |
ck_ec64_add(&ec, mode, increment); |
| 175 |
assert(!should_wake || woken); |
| 176 |
assert(may_wake || !woken); |
| 177 |
assert(!woken || ck_ec64_has_waiters(&ec) == false); |
| 178 |
|
| 179 |
/* Test inc now. */ |
| 180 |
ec.counter = example->initial + increment; |
| 181 |
should_wake = ec.counter & 1; |
| 182 |
|
| 183 |
woken = false; |
| 184 |
ck_ec64_inc(&ec, mode); |
| 185 |
assert(should_wake == woken); |
| 186 |
assert(!woken || ck_ec64_has_waiters(&ec) == false); |
| 187 |
} |
| 188 |
#endif /* CK_F_EC64 */ |
| 189 |
|
| 190 |
return 0; |
| 191 |
} |
| 192 |
|
| 193 |
TEST(test_wakeup, examples) |