]> git.ozlabs.org Git - ccan/blobdiff - ccan/failtest/failtest.c
failtest: fix failpath on open.
[ccan] / ccan / failtest / failtest.c
index 0d52a7969b783ddb7bafd60b53d8f72d5cbdf92d..24c394aa993099024a804a8a1b71161ac9e015ee 100644 (file)
 #include <sys/wait.h>
 #include <sys/stat.h>
 #include <sys/time.h>
+#include <sys/mman.h>
 #include <signal.h>
 #include <assert.h>
 #include <ccan/time/time.h>
 #include <ccan/read_write_all/read_write_all.h>
 #include <ccan/failtest/failtest_proto.h>
 #include <ccan/build_assert/build_assert.h>
+#include <ccan/hash/hash.h>
+#include <ccan/htable/htable_type.h>
 #include <ccan/str/str.h>
 
 enum failtest_result (*failtest_hook)(struct tlist_calls *);
@@ -46,12 +49,44 @@ struct lock_info {
        int type;
 };
 
+/* We hash the call location together with its backtrace. */
+static size_t hash_call(const struct failtest_call *call)
+{
+       return hash(call->file, strlen(call->file),
+                   hash(&call->line, 1,
+                        hash(call->backtrace, call->backtrace_num,
+                             call->type)));
+}
+
+static bool call_eq(const struct failtest_call *call1,
+                   const struct failtest_call *call2)
+{
+       unsigned int i;
+
+       if (strcmp(call1->file, call2->file) != 0
+           || call1->line != call2->line
+           || call1->type != call2->type
+           || call1->backtrace_num != call2->backtrace_num)
+               return false;
+
+       for (i = 0; i < call1->backtrace_num; i++)
+               if (call1->backtrace[i] != call2->backtrace[i])
+                       return false;
+
+       return true;
+}
+
+/* Defines struct failtable. */
+HTABLE_DEFINE_TYPE(struct failtest_call, (struct failtest_call *), hash_call,
+                  call_eq, failtable);
+
 bool (*failtest_exit_check)(struct tlist_calls *history);
 
 static struct tlist_calls history = TLIST_INIT(history);
 static int control_fd = -1;
 static struct timeval start;
-static unsigned int probe_count = 0;
+static bool probing = false;
+static struct failtable failtable;
 
 static struct write_call *child_writes = NULL;
 static unsigned int child_writes_num = 0;
@@ -62,11 +97,39 @@ static unsigned int lock_num = 0;
 
 static pid_t orig_pid;
 
-static const char info_to_arg[] = "mceoxprwf";
+static const char info_to_arg[] = "mceoxprwfa";
 
 /* Dummy call used for failtest_undo wrappers. */
 static struct failtest_call unrecorded_call;
 
+#if HAVE_BACKTRACE
+#include <execinfo.h>
+
+static void **get_backtrace(unsigned int *num)
+{
+       static unsigned int max_back = 100;
+       void **ret;
+
+again:
+       ret = malloc(max_back * sizeof(void *));
+       *num = backtrace(ret, max_back);
+       if (*num == max_back) {
+               free(ret);
+               max_back *= 2;
+               goto again;
+       }
+       return ret;
+}
+#else
+/* This will test slightly less, since will consider all of the same
+ * calls as identical.  But, it's slightly faster! */
+static void **get_backtrace(unsigned int *num)
+{
+       *num = 0;
+       return NULL;
+}
+#endif /* HAVE_BACKTRACE */
+
 static struct failtest_call *add_history_(enum failtest_call_type type,
                                          const char *file,
                                          unsigned int line,
@@ -84,6 +147,7 @@ static struct failtest_call *add_history_(enum failtest_call_type type,
        call->file = file;
        call->line = line;
        call->cleanup = NULL;
+       call->backtrace = get_backtrace(&call->backtrace_num);
        memcpy(&call->u, elem, elem_size);
        tlist_add_tail(&history, call, list);
        return call;
@@ -381,6 +445,7 @@ static void free_call(struct failtest_call *call)
        /* We don't do this in cleanup: needed even for failed opens. */
        if (call->type == FAILTEST_OPEN)
                free((char *)call->u.open.pathname);
+       free(call->backtrace);
        tlist_del_from(&history, call, list);
        free(call);
 }
@@ -392,6 +457,8 @@ static void free_everything(void)
 
        while ((i = tlist_top(&history, struct failtest_call, list)) != NULL)
                free_call(i);
+
+       failtable_clear(&failtable);
 }
 
 static NORETURN void failtest_cleanup(bool forced_cleanup, int status)
@@ -421,10 +488,39 @@ static NORETURN void failtest_cleanup(bool forced_cleanup, int status)
        }
 
        free_everything();
-       tell_parent(SUCCESS);
+       if (status == 0)
+               tell_parent(SUCCESS);
+       else
+               tell_parent(FAILURE);
        exit(status);
 }
 
+static bool following_path(void)
+{
+       if (!failpath)
+               return false;
+       /* + means continue after end, like normal. */
+       if (*failpath == '+') {
+               failpath = NULL;
+               return false;
+       }
+       return true;
+}
+
+static bool follow_path(struct failtest_call *call)
+{
+       if (*failpath == '\0') {
+               /* Continue, but don't inject errors. */
+               return call->fail = false;
+       }
+
+       if (tolower((unsigned char)*failpath) != info_to_arg[call->type])
+               errx(1, "Failpath expected '%s' got '%c'\n",
+                    failpath, info_to_arg[call->type]);
+       call->fail = cisupper(*(failpath++));
+       return call->fail;
+}
+
 static bool should_fail(struct failtest_call *call)
 {
        int status;
@@ -433,34 +529,22 @@ static bool should_fail(struct failtest_call *call)
        char *out = NULL;
        size_t outlen = 0;
        struct saved_file *files;
-
-       /* Are we probing? */
-       if (probe_count && --probe_count == 0 && control_fd != -1)
-               failtest_cleanup(true, 0);
+       struct failtest_call *dup;
 
        if (call == &unrecorded_call)
                return false;
 
-       if (failpath) {
-               /* + means continue after end, like normal. */
-               if (*failpath == '+')
-                       failpath = NULL;
-               else if (*failpath == '\0') {
-                       /* Continue, but don't inject errors. */
-                       return call->fail = false;
-               } else {
-                       if (tolower((unsigned char)*failpath)
-                           != info_to_arg[call->type])
-                               errx(1, "Failpath expected '%c' got '%c'\n",
-                                    info_to_arg[call->type], *failpath);
-                       call->fail = cisupper(*(failpath++));
-                       return call->fail;
-               }
-       }
+       if (following_path())
+               return follow_path(call);
 
        /* Attach debugger if they asked for it. */
        if (debugpath) {
-               char *path = failpath_string();
+               char *path;
+
+               /* Pretend this last call matches whatever path wanted:
+                * keeps valgrind happy. */
+               call->fail = cisupper(debugpath[strlen(debugpath)-1]);
+               path = failpath_string();
 
                if (streq(path, debugpath)) {
                        char str[80];
@@ -471,30 +555,34 @@ static bool should_fail(struct failtest_call *call)
                                getpid(), getpid());
                        if (system(str) == 0)
                                sleep(5);
-               } else if (!strstarts(path, debugpath)) {
-                       fprintf(stderr, "--debugpath not followed: %s\n", path);
-                       debugpath = NULL;
+               } else {
+                       /* Ignore last character: could be upper or lower. */
+                       path[strlen(path)-1] = '\0';
+                       if (!strstarts(debugpath, path)) {
+                               fprintf(stderr,
+                                       "--debugpath not followed: %s\n", path);
+                               debugpath = NULL;
+                       }
                }
                free(path);
        }
 
+       /* Are we probing?  If so, we never fail twice. */
+       if (probing)
+               return call->fail = false;
+
+       /* Don't more than once in the same place. */
+       dup = failtable_get(&failtable, call);
+       if (dup)
+               return call->fail = false;
+
        if (failtest_hook) {
                switch (failtest_hook(&history)) {
                case FAIL_OK:
                        break;
                case FAIL_PROBE:
-                       /* Already down probe path?  Stop now. */
-                       if (!probe_count) {
-                               /* FIXME: We should run *parent* and
-                                * run probe until calls match up again. */
-                               probe_count = 3;
-                               break;
-                       } else {
-                               /* Child should give up now. */
-                               if (control_fd != -1)
-                                       failtest_cleanup(true, 0);
-                               /* Parent, don't fail again. */
-                       }
+                       probing = true;
+                       break;
                case FAIL_DONT_FAIL:
                        call->fail = false;
                        return false;
@@ -503,6 +591,9 @@ static bool should_fail(struct failtest_call *call)
                }
        }
 
+       /* Add it to our table of calls. */
+       failtable_add(&failtable, call);
+
        files = save_files();
 
        /* We're going to fail in the child. */
@@ -624,6 +715,9 @@ static bool should_fail(struct failtest_call *call)
 
        restore_files(files);
 
+       /* Only child does probe. */
+       probing = false;
+
        /* We continue onwards without failing. */
        call->fail = false;
        return false;
@@ -768,6 +862,8 @@ int failtest_open(const char *pathname,
        p->u.open.ret = open(pathname, call.flags, call.mode);
 
        if (p->u.open.ret == -1) {
+               if (following_path())
+                       follow_path(p);
                p->fail = false;
                p->error = errno;
        } else if (should_fail(p)) {
@@ -782,6 +878,30 @@ int failtest_open(const char *pathname,
        return p->u.open.ret;
 }
 
+void *failtest_mmap(void *addr, size_t length, int prot, int flags,
+                   int fd, off_t offset, const char *file, unsigned line)
+{
+       struct failtest_call *p;
+       struct mmap_call call;
+
+       call.addr = addr;
+       call.length = length;
+       call.prot = prot;
+       call.flags = flags;
+       call.offset = offset;
+       call.fd = fd;
+
+       p = add_history(FAILTEST_MMAP, file, line, &call);
+       if (should_fail(p)) {
+               p->u.mmap.ret = MAP_FAILED;
+               p->error = ENOMEM;
+       } else {
+               p->u.mmap.ret = mmap(addr, length, prot, flags, fd, offset);
+       }
+       errno = p->error;
+       return p->u.mmap.ret;
+}
+
 static void cleanup_pipe(struct pipe_call *call)
 {
        if (!call->closed[0])
@@ -984,10 +1104,11 @@ int failtest_close(int fd, const char *file, unsigned line)
        p = add_history(FAILTEST_CLOSE, file, line, &call);
        p->fail = false;
 
-       /* Consume close from failpath. */
-       if (failpath)
-               if (should_fail(p))
+       /* Consume close from failpath (shouldn't tell us to fail). */
+       if (following_path()) {
+               if (follow_path(p))
                        abort();
+       }
 
        if (fd < 0)
                return close(fd);
@@ -1130,6 +1251,7 @@ void failtest_init(int argc, char *argv[])
                        debugpath = argv[i] + strlen("--debugpath=");
                }
        }
+       failtable_init(&failtable);
        start = time_now();
 }