Skip to content

Commit

Permalink
Fix the build with gcc-12 -Wuse-after-free
Browse files Browse the repository at this point in the history
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
  • Loading branch information
gilles-peskine-arm committed Nov 22, 2023
1 parent b9c7058 commit e9616fd
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions programs/test/metatest.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@
*/
volatile int false_but_the_compiler_does_not_know = 0;

/* Hide calls to calloc/free from static checkers such as
* `gcc-12 -Wuse-after-free`, to avoid compile-time complaints about
* code where we do mean to cause a runtime error. */
void * (* volatile calloc_but_the_compiler_does_not_know)(size_t, size_t) = mbedtls_calloc;
void(*volatile free_but_the_compiler_does_not_know)(void *) = mbedtls_free;

/* Set n bytes at the address p to all-bits-zero, in such a way that
* the compiler should not know that p is all-bits-zero. */
static void set_to_zero_but_the_compiler_does_not_know(volatile void *p, size_t n)
Expand Down Expand Up @@ -98,21 +104,21 @@ void null_pointer_call(const char *name)
void read_after_free(const char *name)
{
(void) name;
volatile char *p = mbedtls_calloc(1, 1);
volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
*p = 'a';
mbedtls_free((void *) p);
free_but_the_compiler_does_not_know((void *) p);
/* Undefined behavior (read after free) */
mbedtls_printf("%u\n", (unsigned) *p);
}

void double_free(const char *name)
{
(void) name;
volatile char *p = mbedtls_calloc(1, 1);
volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
*p = 'a';
mbedtls_free((void *) p);
free_but_the_compiler_does_not_know((void *) p);
/* Undefined behavior (double free) */
mbedtls_free((void *) p);
free_but_the_compiler_does_not_know((void *) p);
}

void read_uninitialized_stack(const char *name)
Expand All @@ -132,7 +138,7 @@ void read_uninitialized_stack(const char *name)
void memory_leak(const char *name)
{
(void) name;
volatile char *p = mbedtls_calloc(1, 1);
volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
mbedtls_printf("%u\n", (unsigned) *p);
/* Leak of a heap object */
}
Expand Down

0 comments on commit e9616fd

Please sign in to comment.