From c6d85babd9e60d830c347eb2650df10804ba7d40 Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Wed, 22 Sep 2021 22:44:56 +0200 Subject: add chaos testing --- src/chaos.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/chaos.c (limited to 'src') diff --git a/src/chaos.c b/src/chaos.c new file mode 100644 index 0000000..231c94a --- /dev/null +++ b/src/chaos.c @@ -0,0 +1,43 @@ +/* + * src/chaos.c + * (c) 2021 Jonas Gunz + * License: MIT + */ + +/** + * Randomly inject errors in library functions to check resiliency + */ + +#if defined(_CHAOS) && defined(_GNU_SOURCE) + +#warning "CHAOS Mode enabled" + +#include +#include +#include +#include +#include +#include +#include +#include + +void* malloc (size_t size) { + int fd; + uint16_t rand; + + void* (*realmalloc)(size_t size) = + (void* (*)(size_t size)) dlsym(RTLD_NEXT, "malloc"); + + fd = open("/dev/urandom", O_RDONLY); + read(fd, &rand, 2); + close(fd); + + if( rand < 1000 ) { + errno = ENOMEM; + return NULL; + } + + return realmalloc(size); +} + +#endif -- cgit v1.2.3