aboutsummaryrefslogtreecommitdiff
path: root/src/chaos.c
diff options
context:
space:
mode:
authorGravatar Jonas Gunz <himself@jonasgunz.de> 2021-09-22 22:44:56 +0200
committerGravatar Jonas Gunz <himself@jonasgunz.de> 2021-09-22 22:44:56 +0200
commitc6d85babd9e60d830c347eb2650df10804ba7d40 (patch)
treeaff31b7b18b5a15c46eab4037fee3c0eaa2d9ca2 /src/chaos.c
parent3313fc410be0ffabae019de644f041064b054142 (diff)
downloaddns-c6d85babd9e60d830c347eb2650df10804ba7d40.tar.gz
add chaos testing
Diffstat (limited to 'src/chaos.c')
-rw-r--r--src/chaos.c43
1 files changed, 43 insertions, 0 deletions
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