From fc148563217e7bdd055668ac4376289a3ffa3303 Mon Sep 17 00:00:00 2001 From: Alessio Centazzo Date: Sun, 29 Sep 2019 15:08:48 -0700 Subject: [PATCH 1/3] Fix malloc alignment Make sure the malloc alignment is always aligned to the architecture's pointer size --- extras/fixture/src/unity_fixture.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/extras/fixture/src/unity_fixture.c b/extras/fixture/src/unity_fixture.c index 67807e1..4a064c3 100644 --- a/extras/fixture/src/unity_fixture.c +++ b/extras/fixture/src/unity_fixture.c @@ -188,13 +188,26 @@ typedef struct GuardBytes } Guard; +#define UNITY_MALLOC_ALIGNMENT (UNITY_POINTER_WIDTH / 8) static const char end[] = "END"; + +static size_t unity_size_round_up(size_t size) +{ + size_t rounded_size; + + rounded_size = ((size + UNITY_MALLOC_ALIGNMENT - 1) / UNITY_MALLOC_ALIGNMENT) * UNITY_MALLOC_ALIGNMENT; + + return rounded_size; +} + void* unity_malloc(size_t size) { char* mem; Guard* guard; - size_t total_size = size + sizeof(Guard) + sizeof(end); + size_t total_size; + + total_size = sizeof(Guard) + unity_size_round_up(size + sizeof(end)); if (malloc_fail_countdown != MALLOC_DONT_FAIL) { @@ -243,9 +256,13 @@ static void release_memory(void* mem) malloc_count--; #ifdef UNITY_EXCLUDE_STDLIB_MALLOC - if (mem == unity_heap + heap_index - guard->size - sizeof(end)) + size_t block_size; + + block_size = unity_size_round_up(guard->size + sizeof(end)); + + if (mem == unity_heap + heap_index - block_size) { - heap_index -= (guard->size + sizeof(Guard) + sizeof(end)); + heap_index -= (sizeof(Guard) + block_size); } #else UNITY_FIXTURE_FREE(guard); From 75e88a9bc47ec47646e9a26b3aa6c5b20a4c38bf Mon Sep 17 00:00:00 2001 From: Alessio Centazzo Date: Sun, 29 Sep 2019 21:45:51 -0700 Subject: [PATCH 2/3] Fix MallocThenReallocGrowsMemoryInPlace The realloc was not taking in account extra bytes needed for the the pointer proper alignment --- extras/fixture/src/unity_fixture.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/extras/fixture/src/unity_fixture.c b/extras/fixture/src/unity_fixture.c index 4a064c3..7272a8a 100644 --- a/extras/fixture/src/unity_fixture.c +++ b/extras/fixture/src/unity_fixture.c @@ -317,11 +317,15 @@ void* unity_realloc(void* oldMem, size_t size) if (guard->size >= size) return oldMem; #ifdef UNITY_EXCLUDE_STDLIB_MALLOC /* Optimization if memory is expandable */ - if (oldMem == unity_heap + heap_index - guard->size - sizeof(end) && - heap_index + size - guard->size <= UNITY_INTERNAL_HEAP_SIZE_BYTES) { - release_memory(oldMem); /* Not thread-safe, like unity_heap generally */ - return unity_malloc(size); /* No memcpy since data is in place */ + size_t old_total_size = unity_size_round_up(guard->size + sizeof(end)); + + if ((oldMem == unity_heap + heap_index - old_total_size) && + ((heap_index - old_total_size + unity_size_round_up(size + sizeof(end))) <= UNITY_INTERNAL_HEAP_SIZE_BYTES)) + { + release_memory(oldMem); /* Not thread-safe, like unity_heap generally */ + return unity_malloc(size); /* No memcpy since data is in place */ + } } #endif newMem = unity_malloc(size); From 7df6cca794743459b0fe274a81d24d587e5f1b88 Mon Sep 17 00:00:00 2001 From: Alessio Centazzo Date: Sun, 29 Sep 2019 22:01:03 -0700 Subject: [PATCH 3/3] Fix compiler warning Fix compiler complaint about declaration after statement --- extras/fixture/src/unity_fixture.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/extras/fixture/src/unity_fixture.c b/extras/fixture/src/unity_fixture.c index 7272a8a..958a37f 100644 --- a/extras/fixture/src/unity_fixture.c +++ b/extras/fixture/src/unity_fixture.c @@ -256,13 +256,15 @@ static void release_memory(void* mem) malloc_count--; #ifdef UNITY_EXCLUDE_STDLIB_MALLOC - size_t block_size; - - block_size = unity_size_round_up(guard->size + sizeof(end)); - - if (mem == unity_heap + heap_index - block_size) { - heap_index -= (sizeof(Guard) + block_size); + size_t block_size; + + block_size = unity_size_round_up(guard->size + sizeof(end)); + + if (mem == unity_heap + heap_index - block_size) + { + heap_index -= (sizeof(Guard) + block_size); + } } #else UNITY_FIXTURE_FREE(guard);