| 1 |
#include <assert.h> |
| 2 |
#include <ck_ec.h> |
| 3 |
|
| 4 |
#include "fuzz_harness.h" |
| 5 |
|
| 6 |
static int gettime(const struct ck_ec_ops *, struct timespec *out); |
| 7 |
static void wake32(const struct ck_ec_ops *, const uint32_t *); |
| 8 |
static void wait32(const struct ck_ec_wait_state *, const uint32_t *, |
| 9 |
uint32_t, const struct timespec *); |
| 10 |
static void wake64(const struct ck_ec_ops *, const uint64_t *); |
| 11 |
static void wait64(const struct ck_ec_wait_state *, const uint64_t *, |
| 12 |
uint64_t, const struct timespec *); |
| 13 |
|
| 14 |
static const struct ck_ec_ops test_ops = { |
| 15 |
.gettime = gettime, |
| 16 |
.wait32 = wait32, |
| 17 |
.wait64 = wait64, |
| 18 |
.wake32 = wake32, |
| 19 |
.wake64 = wake64 |
| 20 |
}; |
| 21 |
|
| 22 |
static const struct ck_ec_mode modes[] = { |
| 23 |
{ |
| 24 |
.single_producer = true, |
| 25 |
.ops = &test_ops |
| 26 |
}, |
| 27 |
{ |
| 28 |
.single_producer = false, |
| 29 |
.ops = &test_ops |
| 30 |
}, |
| 31 |
}; |
| 32 |
|
| 33 |
static int gettime(const struct ck_ec_ops *ops, struct timespec *out) |
| 34 |
{ |
| 35 |
(void)out; |
| 36 |
|
| 37 |
assert(ops == &test_ops); |
| 38 |
return -1; |
| 39 |
} |
| 40 |
|
| 41 |
static void wait32(const struct ck_ec_wait_state *wait_state, |
| 42 |
const uint32_t *addr, uint32_t expected, |
| 43 |
const struct timespec *deadline) |
| 44 |
{ |
| 45 |
(void)addr; |
| 46 |
(void)expected; |
| 47 |
(void)deadline; |
| 48 |
|
| 49 |
assert(wait_state->ops == &test_ops); |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
static void wait64(const struct ck_ec_wait_state *wait_state, |
| 54 |
const uint64_t *addr, uint64_t expected, |
| 55 |
const struct timespec *deadline) |
| 56 |
{ |
| 57 |
(void)addr; |
| 58 |
(void)expected; |
| 59 |
(void)deadline; |
| 60 |
|
| 61 |
assert(wait_state->ops == &test_ops); |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
static void wake32(const struct ck_ec_ops *ops, const uint32_t *addr) |
| 66 |
{ |
| 67 |
(void)addr; |
| 68 |
|
| 69 |
assert(ops == &test_ops); |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
static void wake64(const struct ck_ec_ops *ops, const uint64_t *addr) |
| 74 |
{ |
| 75 |
(void)addr; |
| 76 |
|
| 77 |
assert(ops == &test_ops); |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
/* |
| 82 |
* Check that adding a value correctly updates the counter, and that |
| 83 |
* incrementing after that also works. |
| 84 |
*/ |
| 85 |
struct example { |
| 86 |
uint64_t value[2]; |
| 87 |
}; |
| 88 |
|
| 89 |
static const struct example examples[] = { |
| 90 |
{ { 0, 0 } }, |
| 91 |
{ { 1, 2 } }, |
| 92 |
{ { 0, INT32_MAX - 2 } }, |
| 93 |
{ { 0, INT32_MAX - 1 } }, |
| 94 |
{ { 0, INT32_MAX } }, |
| 95 |
{ { 0, INT64_MAX - 2 } }, |
| 96 |
{ { 0, INT64_MAX - 1 } }, |
| 97 |
{ { 0, INT64_MAX } }, |
| 98 |
}; |
| 99 |
|
| 100 |
static inline int test_value(const struct example *example) |
| 101 |
{ |
| 102 |
for (size_t i = 0; i < 2; i++) { |
| 103 |
const struct ck_ec_mode *mode = &modes[i]; |
| 104 |
const uint32_t value0 = example->value[0] & INT32_MAX; |
| 105 |
const uint32_t value1 = example->value[1] & INT32_MAX; |
| 106 |
struct ck_ec32 ec; |
| 107 |
|
| 108 |
ck_ec32_init(&ec, 0); |
| 109 |
assert(ck_ec32_value(&ec) == 0); |
| 110 |
|
| 111 |
ck_ec32_add(&ec, mode, value0); |
| 112 |
assert(ck_ec32_value(&ec) == value0); |
| 113 |
|
| 114 |
ck_ec32_add(&ec, mode, value1); |
| 115 |
assert(ck_ec32_value(&ec) == |
| 116 |
((value0 + value1) & INT32_MAX)); |
| 117 |
|
| 118 |
|
| 119 |
ck_ec32_inc(&ec, mode); |
| 120 |
assert(ck_ec32_value(&ec) == |
| 121 |
((value0 + value1 + 1) & INT32_MAX)); |
| 122 |
} |
| 123 |
|
| 124 |
#ifdef CK_F_EC64 |
| 125 |
for (size_t i = 0; i < 2; i++) { |
| 126 |
const struct ck_ec_mode *mode = &modes[i]; |
| 127 |
const uint64_t value0 = example->value[0] & INT64_MAX; |
| 128 |
const uint64_t value1 = example->value[1] & INT64_MAX; |
| 129 |
struct ck_ec64 ec; |
| 130 |
|
| 131 |
ck_ec64_init(&ec, 0); |
| 132 |
assert(ck_ec64_value(&ec) == 0); |
| 133 |
|
| 134 |
ck_ec64_add(&ec, mode, value0); |
| 135 |
assert(ck_ec64_value(&ec) == value0); |
| 136 |
|
| 137 |
ck_ec64_add(&ec, mode, value1); |
| 138 |
assert(ck_ec64_value(&ec) == |
| 139 |
((value0 + value1) & INT64_MAX)); |
| 140 |
|
| 141 |
ck_ec64_inc(&ec, mode); |
| 142 |
assert(ck_ec64_value(&ec) == |
| 143 |
((value0 + value1 + 1) & INT64_MAX)); |
| 144 |
} |
| 145 |
#endif /* CK_F_EC64 */ |
| 146 |
|
| 147 |
return 0; |
| 148 |
} |
| 149 |
|
| 150 |
TEST(test_value, examples) |