From 951b4d882fa2393369656e05efef3bb229e38c1d Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Wed, 29 Aug 2018 17:51:58 +0200 Subject: Circumvent GCC bugs in test code when optimised MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- config.mk | 4 ++-- libsimple.c | 2 +- test.c | 34 +++++++++++++++++++--------------- test.h | 18 +++++++++--------- 4 files changed, 31 insertions(+), 27 deletions(-) diff --git a/config.mk b/config.mk index 67b8e41..2751186 100644 --- a/config.mk +++ b/config.mk @@ -1,3 +1,3 @@ CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -CFLAGS = -std=c99 -Wall -Wextra -g $(CPPFLAGS) -LDFLAGS = +CFLAGS = -std=c99 -Wall -Wextra -O2 $(CPPFLAGS) +LDFLAGS = -s diff --git a/libsimple.c b/libsimple.c index 58565b5..d6eb2ce 100644 --- a/libsimple.c +++ b/libsimple.c @@ -90,7 +90,7 @@ test_vasprintfa(const char *expected, const char *format, ...) int main(void) { - struct allocinfo *info; + struct allocinfo *volatile info; void *ptr, *ptr2; struct timespec ts, ts1, ts2; struct timeval tv1, tv2; diff --git a/test.c b/test.c index 4d16ebb..4669716 100644 --- a/test.c +++ b/test.c @@ -10,17 +10,17 @@ char *argv0 = (char []){""}; -size_t alloc_fail_in = 0; -int exit_real = 0; -int exit_ok = 0; -int exit_status; +volatile size_t alloc_fail_in = 0; +volatile int exit_real = 0; +volatile int exit_ok = 0; +volatile int exit_status; jmp_buf exit_jmp; -char stderr_buf[8 << 10]; -size_t stderr_n = 0; -int stderr_real = 0; -int stderr_ok = 0; +volatile char stderr_buf[8 << 10]; +volatile size_t stderr_n = 0; +volatile int stderr_real = 0; +volatile int stderr_ok = 0; -static int custom_malloc = 0; +static volatile int custom_malloc = 0; size_t @@ -72,7 +72,7 @@ void * calloc(size_t nelem, size_t elsize) { struct allocinfo *info; - void *ret; + void *volatile ret; assert(nelem && elsize); /* unspecified behaviour otherwise */ if (nelem > SIZE_MAX / elsize) { errno = ENOMEM; @@ -92,7 +92,7 @@ void * realloc(void *ptr, size_t size) { struct allocinfo *info; - void *ret; + void *volatile ret; size_t n; assert(size); /* unspecified behaviour otherwise */ if (!ptr) @@ -113,7 +113,7 @@ void * memalign(size_t alignment, size_t size) { struct allocinfo *info; - void *ptr; + void *volatile ptr; size_t n; uintptr_t off; @@ -166,7 +166,7 @@ int posix_memalign(void **memptr, size_t alignment, size_t size) { int ret, saved_errno = errno; - void **volatile ptrp = memptr; + void *volatile *volatile ptrp = memptr; assert(!(alignment % sizeof(void *))); assert(ptrp); *memptr = memalign(alignment, size); @@ -264,6 +264,7 @@ fprintf(FILE *restrict stream, const char *restrict format, ...) int vfprintf(FILE *restrict stream, const char *restrict format, va_list ap) { + size_t old_alloc_fail_in = alloc_fail_in; va_list ap2; int r; char *buf; @@ -275,17 +276,20 @@ vfprintf(FILE *restrict stream, const char *restrict format, va_list ap) if (r >= 0) { n = (size_t)r; - buf = alloca(n + 1); + alloc_fail_in = 0; + assert((buf = malloc(n + 1))); n = vsnprintf(buf, n + 1, format, ap); if (fileno(stream) != STDERR_FILENO || stderr_real) { fwrite(buf, 1, n, stream); } else { assert(stderr_ok); assert(stderr_n + n <= sizeof(stderr_buf)); - memcpy(&stderr_buf[stderr_n], buf, n); + memcpy((char *)(void *)(&stderr_buf[stderr_n]), buf, n); stderr_n += n; } + free(buf); } + alloc_fail_in = old_alloc_fail_in; return r; } diff --git a/test.h b/test.h index 60d20ca..3aba488 100644 --- a/test.h +++ b/test.h @@ -51,7 +51,7 @@ len__ = sprintf(buf__, FMT, __VA_ARGS__);\ assert(len__ >= 0);\ assert((size_t)len__ == stderr_n);\ - assert(!memcmp(buf__, stderr_buf, stderr_n));\ + assert(!memcmp(buf__, (char **)(void *)(&stderr_buf), stderr_n)); \ } while (0); @@ -68,15 +68,15 @@ struct allocinfo { extern char *argv0; -extern size_t alloc_fail_in; -extern int exit_real; -extern int exit_ok; -extern int exit_status; +extern volatile size_t alloc_fail_in; +extern volatile int exit_real; +extern volatile int exit_ok; +extern volatile int exit_status; extern jmp_buf exit_jmp; -extern char stderr_buf[8 << 10]; -extern size_t stderr_n; -extern int stderr_real; -extern int stderr_ok; +extern volatile char stderr_buf[8 << 10]; +extern volatile size_t stderr_n; +extern volatile int stderr_real; +extern volatile int stderr_ok; size_t get_pagesize(void); -- cgit v1.2.3-70-g09d2