| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "testlib/s2n_mem_testlib.h" |
| 17 |
|
| 18 |
#include "stuffer/s2n_stuffer.h" |
| 19 |
#include "utils/s2n_mem.h" |
| 20 |
#include "utils/s2n_safety.h" |
| 21 |
|
| 22 |
struct s2n_mem_test_cb_ctx { |
| 23 |
struct s2n_stuffer mallocs; |
| 24 |
} s2n_mem_test_ctx = { 0 }; |
| 25 |
|
| 26 |
static s2n_mem_malloc_callback s2n_mem_malloc_cb_backup = NULL; |
| 27 |
static s2n_mem_free_callback s2n_mem_free_cb_backup = NULL; |
| 28 |
|
| 29 |
static int s2n_mem_test_malloc_cb(void **ptr, uint32_t requested, uint32_t *allocated); |
| 30 |
static int s2n_mem_test_free_cb(void *ptr, uint32_t size); |
| 31 |
|
| 32 |
static S2N_RESULT s2n_mem_test_set_callbacks() |
| 33 |
{ |
| 34 |
s2n_mem_init_callback mem_init_cb = NULL; |
| 35 |
s2n_mem_cleanup_callback mem_cleanup_cb = NULL; |
| 36 |
s2n_mem_malloc_callback mem_malloc_cb = NULL; |
| 37 |
s2n_mem_free_callback mem_free_cb = NULL; |
| 38 |
RESULT_GUARD(s2n_mem_get_callbacks(&mem_init_cb, &mem_cleanup_cb, &mem_malloc_cb, &mem_free_cb)); |
| 39 |
|
| 40 |
if (mem_malloc_cb != s2n_mem_test_malloc_cb) { |
| 41 |
s2n_mem_malloc_cb_backup = mem_malloc_cb; |
| 42 |
mem_malloc_cb = s2n_mem_test_malloc_cb; |
| 43 |
} |
| 44 |
if (mem_free_cb != s2n_mem_test_free_cb) { |
| 45 |
s2n_mem_free_cb_backup = mem_free_cb; |
| 46 |
mem_free_cb = s2n_mem_test_free_cb; |
| 47 |
} |
| 48 |
|
| 49 |
RESULT_GUARD(s2n_mem_override_callbacks(mem_init_cb, mem_cleanup_cb, mem_malloc_cb, mem_free_cb)); |
| 50 |
|
| 51 |
return S2N_RESULT_OK; |
| 52 |
} |
| 53 |
|
| 54 |
static S2N_RESULT s2n_mem_test_unset_callbacks() |
| 55 |
{ |
| 56 |
s2n_mem_init_callback mem_init_cb = NULL; |
| 57 |
s2n_mem_cleanup_callback mem_cleanup_cb = NULL; |
| 58 |
s2n_mem_malloc_callback mem_malloc_cb = NULL; |
| 59 |
s2n_mem_free_callback mem_free_cb = NULL; |
| 60 |
RESULT_GUARD(s2n_mem_get_callbacks(&mem_init_cb, &mem_cleanup_cb, &mem_malloc_cb, &mem_free_cb)); |
| 61 |
|
| 62 |
if (s2n_mem_malloc_cb_backup != NULL) { |
| 63 |
mem_malloc_cb = s2n_mem_malloc_cb_backup; |
| 64 |
} |
| 65 |
if (s2n_mem_free_cb_backup != NULL) { |
| 66 |
mem_free_cb = s2n_mem_free_cb_backup; |
| 67 |
} |
| 68 |
|
| 69 |
RESULT_GUARD(s2n_mem_override_callbacks(mem_init_cb, mem_cleanup_cb, mem_malloc_cb, mem_free_cb)); |
| 70 |
|
| 71 |
return S2N_RESULT_OK; |
| 72 |
} |
| 73 |
|
| 74 |
static int s2n_mem_test_malloc_cb(void **ptr, uint32_t requested, uint32_t *allocated) |
| 75 |
{ |
| 76 |
int result = s2n_mem_malloc_cb_backup(ptr, requested, allocated); |
| 77 |
POSIX_GUARD(result); |
| 78 |
|
| 79 |
struct s2n_mem_test_malloc new_info = { |
| 80 |
.requested = requested, |
| 81 |
.ptr = *ptr, |
| 82 |
.allocated = *allocated, |
| 83 |
.freed = false, |
| 84 |
}; |
| 85 |
|
| 86 |
POSIX_GUARD_RESULT(s2n_mem_test_unset_callbacks()); |
| 87 |
POSIX_GUARD(s2n_stuffer_write_bytes(&s2n_mem_test_ctx.mallocs, |
| 88 |
(uint8_t *) &new_info, sizeof(new_info))); |
| 89 |
POSIX_GUARD_RESULT(s2n_mem_test_set_callbacks()); |
| 90 |
|
| 91 |
return result; |
| 92 |
} |
| 93 |
|
| 94 |
static int s2n_mem_test_free_cb(void *ptr, uint32_t size) |
| 95 |
{ |
| 96 |
int result = s2n_mem_free_cb_backup(ptr, size); |
| 97 |
POSIX_GUARD(result); |
| 98 |
|
| 99 |
struct s2n_stuffer read_copy = s2n_mem_test_ctx.mallocs; |
| 100 |
while (s2n_stuffer_data_available(&read_copy)) { |
| 101 |
uint8_t *mem = s2n_stuffer_raw_read(&read_copy, sizeof(struct s2n_mem_test_malloc)); |
| 102 |
struct s2n_mem_test_malloc *info = (struct s2n_mem_test_malloc *) (void *) mem; |
| 103 |
if (info->ptr == ptr) { |
| 104 |
info->freed = true; |
| 105 |
} |
| 106 |
} |
| 107 |
return result; |
| 108 |
} |
| 109 |
|
| 110 |
S2N_RESULT s2n_mem_test_init_callbacks(void *ctx) |
| 111 |
{ |
| 112 |
RESULT_GUARD(s2n_mem_test_free_callbacks(ctx)); |
| 113 |
RESULT_GUARD_POSIX(s2n_stuffer_growable_alloc(&s2n_mem_test_ctx.mallocs, 0)); |
| 114 |
RESULT_GUARD(s2n_mem_test_set_callbacks()); |
| 115 |
return S2N_RESULT_OK; |
| 116 |
} |
| 117 |
|
| 118 |
S2N_CLEANUP_RESULT s2n_mem_test_free_callbacks(void *ctx) |
| 119 |
{ |
| 120 |
(void) ctx; |
| 121 |
RESULT_GUARD(s2n_mem_test_unset_callbacks()); |
| 122 |
RESULT_GUARD_POSIX(s2n_stuffer_free(&s2n_mem_test_ctx.mallocs)); |
| 123 |
return S2N_RESULT_OK; |
| 124 |
} |
| 125 |
|
| 126 |
S2N_RESULT s2n_mem_test_wipe_callbacks() |
| 127 |
{ |
| 128 |
RESULT_GUARD_POSIX(s2n_stuffer_wipe(&s2n_mem_test_ctx.mallocs)); |
| 129 |
return S2N_RESULT_OK; |
| 130 |
} |
| 131 |
|
| 132 |
static S2N_RESULT s2n_mem_test_get_malloc_count(uint32_t *count) |
| 133 |
{ |
| 134 |
RESULT_ENSURE_REF(count); |
| 135 |
size_t size = s2n_stuffer_data_available(&s2n_mem_test_ctx.mallocs); |
| 136 |
const size_t sizeof_malloc = sizeof(struct s2n_mem_test_malloc); |
| 137 |
*count = size / sizeof_malloc; |
| 138 |
RESULT_ENSURE_EQ(size % sizeof_malloc, 0); |
| 139 |
return S2N_RESULT_OK; |
| 140 |
} |
| 141 |
|
| 142 |
static S2N_RESULT s2n_mem_test_get_malloc(uint32_t idx, struct s2n_mem_test_malloc *info) |
| 143 |
{ |
| 144 |
RESULT_ENSURE_REF(info); |
| 145 |
struct s2n_stuffer read_copy = s2n_mem_test_ctx.mallocs; |
| 146 |
const size_t sizeof_malloc = sizeof(struct s2n_mem_test_malloc); |
| 147 |
RESULT_GUARD_POSIX(s2n_stuffer_skip_read(&read_copy, idx * sizeof_malloc)); |
| 148 |
RESULT_GUARD_POSIX(s2n_stuffer_read_bytes(&read_copy, (uint8_t *) info, sizeof_malloc)); |
| 149 |
return S2N_RESULT_OK; |
| 150 |
} |
| 151 |
|
| 152 |
S2N_RESULT s2n_mem_test_assert_malloc_count(uint32_t count) |
| 153 |
{ |
| 154 |
uint32_t actual_count = 0; |
| 155 |
RESULT_GUARD(s2n_mem_test_get_malloc_count(&actual_count)); |
| 156 |
RESULT_ENSURE(count == actual_count, S2N_ERR_TEST_ASSERTION); |
| 157 |
return S2N_RESULT_OK; |
| 158 |
} |
| 159 |
|
| 160 |
S2N_RESULT s2n_mem_test_assert_malloc(uint32_t requested) |
| 161 |
{ |
| 162 |
uint32_t count = 0; |
| 163 |
RESULT_GUARD(s2n_mem_test_get_malloc_count(&count)); |
| 164 |
|
| 165 |
struct s2n_mem_test_malloc mem_info = { 0 }; |
| 166 |
for (size_t i = 0; i < count; i++) { |
| 167 |
RESULT_GUARD(s2n_mem_test_get_malloc(i, &mem_info)); |
| 168 |
if (mem_info.requested == requested) { |
| 169 |
return S2N_RESULT_OK; |
| 170 |
} |
| 171 |
} |
| 172 |
RESULT_BAIL(S2N_ERR_TEST_ASSERTION); |
| 173 |
} |
| 174 |
|
| 175 |
S2N_RESULT s2n_mem_test_assert_all_freed() |
| 176 |
{ |
| 177 |
uint32_t count = 0; |
| 178 |
RESULT_GUARD(s2n_mem_test_get_malloc_count(&count)); |
| 179 |
|
| 180 |
struct s2n_mem_test_malloc mem_info = { 0 }; |
| 181 |
for (size_t i = 0; i < count; i++) { |
| 182 |
RESULT_GUARD(s2n_mem_test_get_malloc(i, &mem_info)); |
| 183 |
RESULT_ENSURE(mem_info.freed, S2N_ERR_TEST_ASSERTION); |
| 184 |
} |
| 185 |
return S2N_RESULT_OK; |
| 186 |
} |