diff options
author | Jonas Gunz <himself@jonasgunz.de> | 2021-09-22 22:44:56 +0200 |
---|---|---|
committer | Jonas Gunz <himself@jonasgunz.de> | 2021-09-22 22:44:56 +0200 |
commit | c6d85babd9e60d830c347eb2650df10804ba7d40 (patch) | |
tree | aff31b7b18b5a15c46eab4037fee3c0eaa2d9ca2 | |
parent | 3313fc410be0ffabae019de644f041064b054142 (diff) | |
download | dns-c6d85babd9e60d830c347eb2650df10804ba7d40.tar.gz |
add chaos testing
-rw-r--r-- | Makefile | 4 | ||||
-rwxr-xr-x | scripts/chaos_dumps.sh | 32 | ||||
-rw-r--r-- | src/chaos.c | 43 |
3 files changed, 79 insertions, 0 deletions
@@ -84,6 +84,10 @@ valgrind: $(DEFAULT) @echo [EXEC] valgrind $(BUILDDIR)/$(OUTPUT) $(RUNARGS) valgrind --leak-check=full $(BUILDDIR)/$(OUTPUT) $(RUNARGS) +chaos: LDFLAGS += -ldl +chaos: CFLAGS += -D_CHAOS -D_GNU_SOURCE +chaos: clean debug + .PHONY: clean clean: @echo [ RM ] $(OBJ) $(TOBJS) diff --git a/scripts/chaos_dumps.sh b/scripts/chaos_dumps.sh new file mode 100755 index 0000000..d853e83 --- /dev/null +++ b/scripts/chaos_dumps.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +CRASHCNT=0 +GOODCRASHES=0 +PROG=build/dnsd + +make chaos + +ulimit -S -c unlimited $PROG +mkdir -p dumps + +while true; do + $PROG -p 5333 -z tests/zone.file > /dev/null & + PID=$! + + for i in $(seq 50); do + dig -p 5333 test.example.com @localhost > /dev/null & + done + + sleep 5 + + if ! kill -s 15 $PID ; then + CRASHCNT=$(( CRASHCNT + 1 )) + + echo -n "Crash #$CRASHCNT" + + if ! coredumpctl -o dumps/$CRASHCNT.core dump COREDUMP_PID=$PID "COREDUMP_EXE=$(realpath $PROG)" > /dev/null 2>&1; then + GOODCRASHES=$(( GOODCRASHES + 1 )) + echo " Good crash #$GOODCRASHES" + fi + fi +done 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 <himself@jonasgunz.de> + * License: MIT + */ + +/** + * Randomly inject errors in library functions to check resiliency + */ + +#if defined(_CHAOS) && defined(_GNU_SOURCE) + +#warning "CHAOS Mode enabled" + +#include <dlfcn.h> +#include <stdlib.h> +#include <stdio.h> +#include <stdint.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <errno.h> + +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 |