-
Notifications
You must be signed in to change notification settings - Fork 712
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CBMC proof for s2n_stuffer_printf (#4309)
- Loading branch information
Showing
6 changed files
with
147 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You may not use | ||
# this file except in compliance with the License. A copy of the License is | ||
# located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
# implied. See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
CHECKFLAGS += | ||
|
||
PROOF_UID = s2n_stuffer_printf | ||
HARNESS_ENTRY = $(PROOF_UID)_harness | ||
HARNESS_FILE = $(HARNESS_ENTRY).c | ||
|
||
PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE) | ||
PROOF_SOURCES += $(PROOF_SOURCE)/cbmc_utils.c | ||
PROOF_SOURCES += $(PROOF_SOURCE)/make_common_datastructures.c | ||
PROOF_SOURCES += $(PROOF_STUB)/madvise.c | ||
PROOF_SOURCES += $(PROOF_STUB)/mlock.c | ||
PROOF_SOURCES += $(PROOF_STUB)/munlock.c | ||
PROOF_SOURCES += $(PROOF_STUB)/posix_memalign_override.c | ||
PROOF_SOURCES += $(PROOF_STUB)/s2n_calculate_stacktrace.c | ||
PROOF_SOURCES += $(PROOF_STUB)/sysconf.c | ||
|
||
PROJECT_SOURCES += $(SRCDIR)/stuffer/s2n_stuffer.c | ||
PROJECT_SOURCES += $(SRCDIR)/stuffer/s2n_stuffer_text.c | ||
PROJECT_SOURCES += $(SRCDIR)/utils/s2n_blob.c | ||
PROJECT_SOURCES += $(SRCDIR)/utils/s2n_ensure.c | ||
PROJECT_SOURCES += $(SRCDIR)/utils/s2n_mem.c | ||
PROJECT_SOURCES += $(SRCDIR)/utils/s2n_safety.c | ||
PROJECT_SOURCES += $(SRCDIR)/utils/s2n_result.c | ||
|
||
include ../Makefile.common |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# This file marks this directory as containing a CBMC proof. |
66 changes: 66 additions & 0 deletions
66
tests/cbmc/proofs/s2n_stuffer_printf/s2n_stuffer_printf_harness.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
#include <assert.h> | ||
#include <cbmc_proof/cbmc_utils.h> | ||
#include <cbmc_proof/make_common_datastructures.h> | ||
|
||
#include "api/s2n.h" | ||
#include "stuffer/s2n_stuffer.h" | ||
#include "utils/s2n_mem.h" | ||
|
||
void s2n_stuffer_printf_harness() | ||
{ | ||
nondet_s2n_mem_init(); | ||
|
||
struct s2n_stuffer *stuffer = cbmc_allocate_s2n_stuffer(); | ||
__CPROVER_assume(s2n_result_is_ok(s2n_stuffer_validate(stuffer))); | ||
|
||
size_t format_len; | ||
char *format = ensure_c_str_is_allocated(format_len); | ||
|
||
/* CBMC defines va_list as void** */ | ||
size_t va_list_size; | ||
__CPROVER_assume(va_list_size % sizeof(void*) == 0); | ||
void** va_list_mem = malloc(va_list_size); | ||
|
||
/* Store the stuffer to compare after the write */ | ||
struct s2n_stuffer old_stuffer = *stuffer; | ||
struct store_byte_from_buffer old_byte; | ||
save_byte_from_array(old_stuffer.blob.data, old_stuffer.write_cursor, &old_byte); | ||
|
||
int result = s2n_stuffer_vprintf(stuffer, format, va_list_mem); | ||
assert(s2n_result_is_ok(s2n_stuffer_validate(stuffer))); | ||
|
||
/* The basic stuffer fields should NOT be updated */ | ||
assert(old_stuffer.growable == stuffer->growable); | ||
assert(old_stuffer.tainted == stuffer->tainted); | ||
assert(old_stuffer.alloced == stuffer->alloced); | ||
|
||
/* Any previously written data should NOT be updated */ | ||
if (old_stuffer.write_cursor > 0) { | ||
assert_byte_from_buffer_matches(stuffer->blob.data, &old_byte); | ||
} | ||
|
||
/* The read cursor should NOT be updated */ | ||
assert(old_stuffer.read_cursor == stuffer->read_cursor); | ||
|
||
/* The write cursor should only be updated on success */ | ||
if (result == S2N_SUCCESS) { | ||
assert(old_stuffer.write_cursor <= stuffer->write_cursor); | ||
} else { | ||
assert(old_stuffer.write_cursor == stuffer->write_cursor); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters