| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <assert.h> |
| 17 |
#include <cbmc_proof/cbmc_utils.h> |
| 18 |
#include <cbmc_proof/make_common_datastructures.h> |
| 19 |
|
| 20 |
#include "api/s2n.h" |
| 21 |
#include "stuffer/s2n_stuffer.h" |
| 22 |
|
| 23 |
void s2n_stuffer_write_harness() |
| 24 |
{ |
| 25 |
|
| 26 |
struct s2n_stuffer *stuffer = cbmc_allocate_s2n_stuffer(); |
| 27 |
__CPROVER_assume(s2n_result_is_ok(s2n_stuffer_validate(stuffer))); |
| 28 |
struct s2n_blob *blob = cbmc_allocate_s2n_blob(); |
| 29 |
__CPROVER_assume(s2n_result_is_ok(s2n_blob_validate(blob))); |
| 30 |
uint32_t idx; |
| 31 |
|
| 32 |
nondet_s2n_mem_init(); |
| 33 |
|
| 34 |
|
| 35 |
struct s2n_stuffer old_stuffer = *stuffer; |
| 36 |
|
| 37 |
|
| 38 |
__CPROVER_assume(idx < stuffer->blob.size); |
| 39 |
if (__CPROVER_overflow_plus(old_stuffer.write_cursor, blob->size)) { |
| 40 |
__CPROVER_assume(idx < old_stuffer.write_cursor); |
| 41 |
} else { |
| 42 |
__CPROVER_assume(idx < old_stuffer.write_cursor || idx >= old_stuffer.write_cursor + blob->size); |
| 43 |
} |
| 44 |
uint8_t untouched_byte = stuffer->blob.data[ idx ]; |
| 45 |
|
| 46 |
|
| 47 |
struct s2n_blob old_blob = *blob; |
| 48 |
struct store_byte_from_buffer old_byte_from_blob; |
| 49 |
save_byte_from_blob(blob, &old_byte_from_blob); |
| 50 |
|
| 51 |
|
| 52 |
if (s2n_stuffer_write(stuffer, blob) == S2N_SUCCESS) { |
| 53 |
assert(stuffer->write_cursor == old_stuffer.write_cursor + blob->size); |
| 54 |
assert(stuffer->blob.data[ idx ] == untouched_byte); |
| 55 |
assert(stuffer->high_water_mark == S2N_MAX(old_stuffer.write_cursor + blob->size, old_stuffer.high_water_mark)); |
| 56 |
assert(s2n_result_is_ok(s2n_stuffer_validate(stuffer))); |
| 57 |
} else { |
| 58 |
assert(stuffer->write_cursor == old_stuffer.write_cursor); |
| 59 |
assert(stuffer->high_water_mark == old_stuffer.high_water_mark); |
| 60 |
} |
| 61 |
assert(stuffer->read_cursor == old_stuffer.read_cursor); |
| 62 |
assert_blob_equivalence(blob, &old_blob, &old_byte_from_blob); |
| 63 |
} |