From 478af5417f08f2c2db97e3f63ce5b3d683c0388b Mon Sep 17 00:00:00 2001 From: jvoisin Date: Tue, 4 Aug 2026 17:40:00 +0200 Subject: [PATCH] test: add coverage for untested allocator error paths Add two small-allocation test cases and wire them into the Makefile and test_smc.py harness: - invalid_free_aligned_sized_small: free_aligned_sized() with a non-power-of-two alignment, covering the "invalid sized deallocation alignment (small)" fatal path. - invalid_malloc_object_size_small_canary: malloc_object_size() queried at an offset past the usable region, covering the "invalid malloc_object_size (canary)" fatal path. --- test/Makefile | 2 ++ test/invalid_free_aligned_sized_small.c | 8 ++++++++ test/invalid_malloc_object_size_small_canary.c | 11 +++++++++++ test/test_smc.py | 14 ++++++++++++++ 4 files changed, 35 insertions(+) create mode 100644 test/invalid_free_aligned_sized_small.c create mode 100644 test/invalid_malloc_object_size_small_canary.c diff --git a/test/Makefile b/test/Makefile index fb680a4d..4169a44c 100644 --- a/test/Makefile +++ b/test/Makefile @@ -64,6 +64,7 @@ EXECUTABLES := \ invalid_aligned_sized_delete_small \ aligned_sized_delete_large \ invalid_aligned_sized_delete_large \ + invalid_free_aligned_sized_small \ free_sized_small \ free_sized_large \ unaligned_malloc_usable_size_small \ @@ -74,6 +75,7 @@ EXECUTABLES := \ malloc_object_size_zero \ invalid_malloc_object_size_small \ invalid_malloc_object_size_small_quarantine \ + invalid_malloc_object_size_small_canary \ impossibly_large_malloc \ realloc_init \ calloc_overflow \ diff --git a/test/invalid_free_aligned_sized_small.c b/test/invalid_free_aligned_sized_small.c new file mode 100644 index 00000000..becae23b --- /dev/null +++ b/test/invalid_free_aligned_sized_small.c @@ -0,0 +1,8 @@ +#include "../include/h_malloc.h" + +int main(void) { + void *p = malloc(16); + // alignment 3 is not a power of two + free_aligned_sized(p, 3, 16); + return 0; +} diff --git a/test/invalid_malloc_object_size_small_canary.c b/test/invalid_malloc_object_size_small_canary.c new file mode 100644 index 00000000..3ca22b64 --- /dev/null +++ b/test/invalid_malloc_object_size_small_canary.c @@ -0,0 +1,11 @@ +#include + +#include "test_util.h" + +size_t malloc_object_size(void *ptr); + +OPTNONE int main(void) { + char *p = malloc(16); + // offset past the usable size, into the slab canary region + return (int)malloc_object_size(p + 25); +} diff --git a/test/test_smc.py b/test/test_smc.py index c8a350d9..cecc0363 100644 --- a/test/test_smc.py +++ b/test/test_smc.py @@ -51,6 +51,13 @@ def test_invalid_aligned_sized_delete_large(self): self.assertEqual(stderr.decode( "utf-8"), "fatal allocator error: sized deallocation mismatch (large)\n") + def test_invalid_free_aligned_sized_small(self): + _stdout, stderr, returncode = self.run_test( + "invalid_free_aligned_sized_small") + self.assertEqual(returncode, -6) + self.assertEqual(stderr.decode( + "utf-8"), "fatal allocator error: invalid sized deallocation alignment (small)\n") + def test_free_sized_small(self): _stdout, _stderr, returncode = self.run_test("free_sized_small") self.assertEqual(returncode, 0) @@ -255,6 +262,13 @@ def test_invalid_malloc_object_size_small_quarantine(self): self.assertEqual(stderr.decode( "utf-8"), "fatal allocator error: invalid malloc_object_size (quarantine)\n") + def test_invalid_malloc_object_size_small_canary(self): + _stdout, stderr, returncode = self.run_test( + "invalid_malloc_object_size_small_canary") + self.assertEqual(returncode, -6) + self.assertEqual(stderr.decode( + "utf-8"), "fatal allocator error: invalid malloc_object_size (canary)\n") + def test_impossibly_large_malloc(self): _stdout, stderr, returncode = self.run_test( "impossibly_large_malloc")