Skip to content

Commit b1bb5dc

Browse files
authored
refactor: make memmove vs memcpy behavior clearer (#4447)
1 parent dea5534 commit b1bb5dc

File tree

15 files changed

+25
-66
lines changed

15 files changed

+25
-66
lines changed

scripts/s2n_safety_macros.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ def cmp_check(op):
539539
* The size of the data pointed to by both the `destination` and `source` parameters,
540540
shall be at least `len` bytes.
541541
''',
542-
impl='__S2N_ENSURE_SAFE_MEMCPY((destination), (source), (len), {prefix}ENSURE_REF)',
542+
impl='__S2N_ENSURE_SAFE_MEMMOVE((destination), (source), (len), {prefix}ENSURE_REF)',
543543
harness='''
544544
static {ret} {prefix}CHECKED_MEMCPY_harness(uint32_t* dest, uint32_t* source, size_t len)
545545
{{

tests/cbmc/proofs/s2n_alloc/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ PROJECT_SOURCES += $(SRCDIR)/utils/s2n_result.c
4040
# We abstract these functions because manual inspection demonstrates they are unreachable.
4141
REMOVE_FUNCTION_BODY += __CPROVER_file_local_s2n_mem_c_s2n_mem_cleanup_impl
4242
REMOVE_FUNCTION_BODY += s2n_blob_slice
43-
REMOVE_FUNCTION_BODY += s2n_ensure_memcpy_trace
43+
REMOVE_FUNCTION_BODY += s2n_ensure_memmove_trace
4444

4545
UNWINDSET +=
4646

tests/cbmc/proofs/s2n_array_init/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ PROJECT_SOURCES += $(SRCDIR)/utils/s2n_safety.c
3535

3636
# We abstract these functions because manual inspection demonstrates they are unreachable.
3737
REMOVE_FUNCTION_BODY += s2n_blob_slice
38-
REMOVE_FUNCTION_BODY += s2n_ensure_memcpy_trace
38+
REMOVE_FUNCTION_BODY += s2n_ensure_memmove_trace
3939
REMOVE_FUNCTION_BODY += __CPROVER_file_local_s2n_mem_c_s2n_mem_cleanup_impl
4040

4141
UNWINDSET +=

tests/cbmc/proofs/s2n_array_new/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ PROJECT_SOURCES += $(SRCDIR)/utils/s2n_safety.c
3838

3939
# We abstract these functions because manual inspection demonstrates they are unreachable.
4040
REMOVE_FUNCTION_BODY += s2n_blob_slice
41-
REMOVE_FUNCTION_BODY += s2n_ensure_memcpy_trace
41+
REMOVE_FUNCTION_BODY += s2n_ensure_memmove_trace
4242
REMOVE_FUNCTION_BODY += __CPROVER_file_local_s2n_mem_c_s2n_mem_cleanup_impl
4343

4444
UNWINDSET +=

tests/cbmc/proofs/s2n_set_new/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ PROJECT_SOURCES += $(SRCDIR)/utils/s2n_set.c
4242

4343
# We abstract these functions because manual inspection demonstrates they are unreachable.
4444
REMOVE_FUNCTION_BODY += s2n_blob_slice
45-
REMOVE_FUNCTION_BODY += s2n_ensure_memcpy_trace
45+
REMOVE_FUNCTION_BODY += s2n_ensure_memmove_trace
4646
REMOVE_FUNCTION_BODY += __CPROVER_file_local_s2n_mem_c_s2n_mem_cleanup_impl
4747

4848
UNWINDSET +=

tests/cbmc/proofs/s2n_stuffer_alloc/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ PROJECT_SOURCES += $(SRCDIR)/utils/s2n_safety.c
4040
# We abstract these functions because manual inspection demonstrates they are unreachable.
4141
REMOVE_FUNCTION_BODY += __CPROVER_file_local_s2n_mem_c_s2n_mem_cleanup_impl
4242
REMOVE_FUNCTION_BODY += s2n_blob_slice
43-
REMOVE_FUNCTION_BODY += s2n_ensure_memcpy_trace
43+
REMOVE_FUNCTION_BODY += s2n_ensure_memmove_trace
4444

4545
UNWINDSET +=
4646

tests/cbmc/proofs/s2n_stuffer_growable_alloc/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ PROJECT_SOURCES += $(SRCDIR)/utils/s2n_safety.c
3838
# We abstract these functions because manual inspection demonstrates they are unreachable.
3939
REMOVE_FUNCTION_BODY += __CPROVER_file_local_s2n_mem_c_s2n_mem_cleanup_impl
4040
REMOVE_FUNCTION_BODY += s2n_blob_slice
41-
REMOVE_FUNCTION_BODY += s2n_ensure_memcpy_trace
41+
REMOVE_FUNCTION_BODY += s2n_ensure_memmove_trace
4242

4343
UNWINDSET +=
4444

tests/cbmc/proofs/s2n_stuffer_resize_if_empty/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ PROJECT_SOURCES += $(SRCDIR)/utils/s2n_result.c
3535
# We abstract these functions because manual inspection demonstrates they are unreachable.
3636
REMOVE_FUNCTION_BODY += s2n_calculate_stacktrace
3737
REMOVE_FUNCTION_BODY += s2n_blob_slice
38-
REMOVE_FUNCTION_BODY += s2n_ensure_memcpy_trace
38+
REMOVE_FUNCTION_BODY += s2n_ensure_memmove_trace
3939
REMOVE_FUNCTION_BODY += __CPROVER_file_local_s2n_mem_c_s2n_mem_cleanup_impl
4040

4141
UNWINDSET +=

tests/cbmc/stubs/s2n_ensure.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#include "utils/s2n_safety.h"
1717

18-
void* s2n_ensure_memcpy_trace(void *restrict to, const void *restrict from, size_t size)
18+
void* s2n_ensure_memmove_trace(void *to, const void *from, size_t size)
1919
{
2020
if (to == NULL || from == NULL) {
2121
return NULL;

tests/features/S2N___RESTRICT__SUPPORTED.c

-27
This file was deleted.

tests/features/S2N___RESTRICT__SUPPORTED.flags

-1
This file was deleted.

tests/sidetrail/working/stubs/s2n_ensure.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ void *s2n_sidetrail_memset(void * ptr, int value, size_t num);
3131
#define __S2N_ENSURE_PRECONDITION( result ) S2N_RESULT_OK
3232
#define __S2N_ENSURE_POSTCONDITION( result ) S2N_RESULT_OK
3333

34-
#define __S2N_ENSURE_SAFE_MEMCPY( d , s , n , guard ) do { memcpy((d), (s), (n)); } while(0)
34+
/* memmove isn't supported, so use memcpy instead.
35+
* For the purposes of these proofs, there should be no useful difference.
36+
*/
37+
#define __S2N_ENSURE_SAFE_MEMMOVE( d , s , n , guard ) do { memcpy((d), (s), (n)); } while(0)
3538

3639
#define __S2N_ENSURE_SAFE_MEMSET( d , c , n , guard ) \
3740
do { \

utils/s2n_ensure.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#include "utils/s2n_safety.h"
1717

18-
void *s2n_ensure_memcpy_trace(void *restrict to, const void *restrict from, size_t size)
18+
void *s2n_ensure_memmove_trace(void *to, const void *from, size_t size)
1919
{
2020
PTR_ENSURE_REF(to);
2121
PTR_ENSURE_REF(from);

utils/s2n_ensure.h

+8-24
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@
6060
#define __S2N_ENSURE_POSTCONDITION(result) (s2n_likely(s2n_result_is_ok(result)) ? S2N_RESULT_OK : S2N_RESULT_ERROR)
6161
#endif
6262

63-
#define __S2N_ENSURE_SAFE_MEMCPY(d, s, n, guard) \
64-
do { \
65-
__typeof(n) __tmp_n = (n); \
66-
if (s2n_likely(__tmp_n)) { \
67-
void *r = s2n_ensure_memcpy_trace((d), (s), (__tmp_n)); \
68-
guard(r); \
69-
} \
63+
#define __S2N_ENSURE_SAFE_MEMMOVE(d, s, n, guard) \
64+
do { \
65+
__typeof(n) __tmp_n = (n); \
66+
if (s2n_likely(__tmp_n)) { \
67+
void *r = s2n_ensure_memmove_trace((d), (s), (__tmp_n)); \
68+
guard(r); \
69+
} \
7070
} while (0)
7171

7272
#define __S2N_ENSURE_SAFE_MEMSET(d, c, n, guard) \
@@ -90,23 +90,7 @@
9090
#define __S2N_ENSURE_CHECKED_RETURN(v) return v
9191
#endif
9292

93-
/**
94-
* `restrict` is a part of the c99 standard and will work with any C compiler. If you're trying to
95-
* compile with a C++ compiler `restrict` is invalid. However some C++ compilers support the behavior
96-
* of `restrict` using the `__restrict__` keyword. Therefore if the compiler supports `__restrict__`
97-
* use it.
98-
*
99-
* This is helpful for the benchmarks in tests/benchmark which use Google's Benchmark library and
100-
* are all written in C++.
101-
*
102-
* https://gcc.gnu.org/onlinedocs/gcc/Restricted-Pointers.html
103-
*
104-
*/
105-
#if defined(S2N___RESTRICT__SUPPORTED)
106-
void *s2n_ensure_memcpy_trace(void *__restrict__ to, const void *__restrict__ from, size_t size);
107-
#else
108-
void *s2n_ensure_memcpy_trace(void *restrict to, const void *restrict from, size_t size);
109-
#endif
93+
void *s2n_ensure_memmove_trace(void *to, const void *from, size_t size);
11094

11195
/**
11296
* These macros should not be used in validate functions.

utils/s2n_safety_macros.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@
161161
* * The size of the data pointed to by both the `destination` and `source` parameters,
162162
* shall be at least `len` bytes.
163163
*/
164-
#define RESULT_CHECKED_MEMCPY(destination, source, len) __S2N_ENSURE_SAFE_MEMCPY((destination), (source), (len), RESULT_ENSURE_REF)
164+
#define RESULT_CHECKED_MEMCPY(destination, source, len) __S2N_ENSURE_SAFE_MEMMOVE((destination), (source), (len), RESULT_ENSURE_REF)
165165

166166
/**
167167
* Performs a safer memset
@@ -357,7 +357,7 @@
357357
* * The size of the data pointed to by both the `destination` and `source` parameters,
358358
* shall be at least `len` bytes.
359359
*/
360-
#define POSIX_CHECKED_MEMCPY(destination, source, len) __S2N_ENSURE_SAFE_MEMCPY((destination), (source), (len), POSIX_ENSURE_REF)
360+
#define POSIX_CHECKED_MEMCPY(destination, source, len) __S2N_ENSURE_SAFE_MEMMOVE((destination), (source), (len), POSIX_ENSURE_REF)
361361

362362
/**
363363
* DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
@@ -563,7 +563,7 @@
563563
* * The size of the data pointed to by both the `destination` and `source` parameters,
564564
* shall be at least `len` bytes.
565565
*/
566-
#define PTR_CHECKED_MEMCPY(destination, source, len) __S2N_ENSURE_SAFE_MEMCPY((destination), (source), (len), PTR_ENSURE_REF)
566+
#define PTR_CHECKED_MEMCPY(destination, source, len) __S2N_ENSURE_SAFE_MEMMOVE((destination), (source), (len), PTR_ENSURE_REF)
567567

568568
/**
569569
* DEPRECATED: all methods (except those in s2n.h) should return s2n_result.

0 commit comments

Comments
 (0)