From 7b91cea54a5f5e4e58a9064795c467eaf573e544 Mon Sep 17 00:00:00 2001 From: Szabolcs Nagy Date: Sat, 25 Jul 2020 00:37:21 +0000 Subject: [PATCH 1/1] fix flockfile-list regression test this test should check if freed stdio memory is clobbered after an funlockfile. the old method was very fragile: it tried to reuse the freed memory via a malloc and check if that allocation is clobbered. now musl supports malloc interposition so we can use that to directly check the required property. --- src/regression/flockfile-list.c | 101 +++++++++++++++++++++++++------- 1 file changed, 79 insertions(+), 22 deletions(-) diff --git a/src/regression/flockfile-list.c b/src/regression/flockfile-list.c index 09f18ff..a269cb8 100644 --- a/src/regression/flockfile-list.c +++ b/src/regression/flockfile-list.c @@ -6,6 +6,84 @@ #include #include "test.h" +#define t_fatal(...) (t_error(__VA_ARGS__), _Exit(t_status)) +#define length(a) (sizeof(a)/sizeof*(a)) + +// interpose malloc functions +// freed memory is not reused, it is checked for clobber. + +static unsigned char buf[1<<20]; +static size_t pos; +static struct { + size_t pos; + size_t n; + int freed; +} alloc[100]; +static int idx; + +void *malloc(size_t n) +{ + if (n == 0) n++; + if (n > sizeof buf - pos) + t_fatal("test buffer is small, pos: %zu, need: %zu\n", pos, n); + if (idx >= length(alloc)) + t_fatal("test buffer is small, idx: %d\n", idx); + void *p = buf + pos; + alloc[idx].pos = pos; + alloc[idx].n = n; + pos += n; + idx++; + return p; +} + +void *calloc(size_t n, size_t m) +{ + return memset(malloc(n*m), 0, n*m); +} + +void *aligned_alloc(size_t a, size_t n) +{ + t_fatal("aligned_alloc is unsupported\n"); +} + +static int findidx(void *p) +{ + size_t pos = (unsigned char *)p - buf; + for (int i=0; i