[a42d7d8] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Sean Bartell
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[0784869] | 29 | /** @cond internal */
|
---|
[a42d7d8] | 30 | /** @addtogroup bithenge
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | * Fake system call errors for testing.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <errno.h>
|
---|
[1a3b953] | 39 | #include <execinfo.h>
|
---|
[a42d7d8] | 40 | #include <fcntl.h>
|
---|
| 41 | #include <stdio.h>
|
---|
| 42 | #include <stdlib.h>
|
---|
[8d2dd7f2] | 43 | #include <stddef.h>
|
---|
[a42d7d8] | 44 | #include <sys/wait.h>
|
---|
| 45 | #define BITHENGE_FAILURE_DECLS_ONLY 1
|
---|
[6cd10ac] | 46 | #include "failure.h"
|
---|
| 47 | #include "common.h"
|
---|
[a42d7d8] | 48 |
|
---|
[7c3fb9b] | 49 | /*
|
---|
| 50 | * This file raises fake errors from system calls, to test that Bithenge
|
---|
[a42d7d8] | 51 | * handles the errors correctly. It has two primary modes of operation,
|
---|
| 52 | * depending on an environment variable:
|
---|
| 53 | *
|
---|
| 54 | * BITHENGE_FAILURE_INDEX not set: when a system call is made, a child process
|
---|
| 55 | * returns a fake error from that call. If the child process handles the error
|
---|
| 56 | * correctly (exit code is 1), the main process continues without errors. If
|
---|
| 57 | * the child process has a problem, the main process raises the fake error
|
---|
[1a3b953] | 58 | * again and shows all stdout and stderr output. For speed, errors are only
|
---|
| 59 | * raised when part of the backtrace has not been seen before.
|
---|
[a42d7d8] | 60 | *
|
---|
| 61 | * BITHENGE_FAILURE_INDEX set: the program runs normally until system call
|
---|
| 62 | * number BITHENGE_FAILURE_INDEX is made; a fake error is returned from this
|
---|
[7c3fb9b] | 63 | * call.
|
---|
| 64 | */
|
---|
[a42d7d8] | 65 |
|
---|
[1a3b953] | 66 | static int g_initialized = 0;
|
---|
[a42d7d8] | 67 | static int g_failure_index = -1;
|
---|
[1a3b953] | 68 | static int g_failure_index_selected = -1;
|
---|
[a42d7d8] | 69 |
|
---|
[1a3b953] | 70 | typedef struct backtrace_item {
|
---|
| 71 | struct backtrace_item *next;
|
---|
| 72 | void *backtrace_item;
|
---|
| 73 | } backtrace_item_t;
|
---|
| 74 |
|
---|
| 75 | static backtrace_item_t *g_backtrace_items = NULL;
|
---|
| 76 |
|
---|
| 77 | static void atexit_handler(void)
|
---|
[a42d7d8] | 78 | {
|
---|
[1a3b953] | 79 | while (g_backtrace_items) {
|
---|
| 80 | backtrace_item_t *first = g_backtrace_items;
|
---|
| 81 | g_backtrace_items = first->next;
|
---|
| 82 | free(first);
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
[a42d7d8] | 85 |
|
---|
[1a3b953] | 86 | static inline void initialize(void)
|
---|
| 87 | {
|
---|
| 88 | g_initialized = 1;
|
---|
[b7fd2a0] | 89 | errno_t rc = atexit(atexit_handler);
|
---|
[1a3b953] | 90 | if (rc)
|
---|
| 91 | exit(127);
|
---|
| 92 |
|
---|
| 93 | char *sel_str = getenv("BITHENGE_FAILURE_INDEX");
|
---|
| 94 | if (sel_str)
|
---|
| 95 | g_failure_index_selected = strtol(sel_str, NULL, 10);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[7c3fb9b] | 98 | /*
|
---|
| 99 | * Record a hit for a backtrace address and return whether this is the first
|
---|
| 100 | * hit.
|
---|
| 101 | */
|
---|
[b7fd2a0] | 102 | static inline errno_t backtrace_item_hit(void *addr)
|
---|
[1a3b953] | 103 | {
|
---|
| 104 | backtrace_item_t **bip;
|
---|
| 105 | for (bip = &g_backtrace_items; *bip; bip = &(*bip)->next) {
|
---|
| 106 | backtrace_item_t *bi = *bip;
|
---|
| 107 | if (bi->backtrace_item == addr) {
|
---|
| 108 | /* Keep frequently accessed items near the front. */
|
---|
| 109 | *bip = bi->next;
|
---|
| 110 | bi->next = g_backtrace_items;
|
---|
| 111 | g_backtrace_items = bi;
|
---|
| 112 | return 0;
|
---|
[a42d7d8] | 113 | }
|
---|
[1a3b953] | 114 | }
|
---|
| 115 |
|
---|
| 116 | /* No item found; create one. */
|
---|
| 117 | backtrace_item_t *i = malloc(sizeof(*i));
|
---|
| 118 | if (!i)
|
---|
| 119 | exit(127);
|
---|
| 120 | i->next = g_backtrace_items;
|
---|
| 121 | i->backtrace_item = addr;
|
---|
| 122 | g_backtrace_items = i;
|
---|
| 123 | return 1;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[b7fd2a0] | 126 | errno_t bithenge_should_fail(void)
|
---|
[1a3b953] | 127 | {
|
---|
| 128 | g_failure_index++;
|
---|
| 129 |
|
---|
| 130 | if (!g_initialized)
|
---|
| 131 | initialize();
|
---|
| 132 |
|
---|
| 133 | if (g_failure_index_selected != -1) {
|
---|
[a42d7d8] | 134 | if (g_failure_index == g_failure_index_selected)
|
---|
| 135 | return 1; /* breakpoint here */
|
---|
| 136 | return 0;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[7c3fb9b] | 139 | /*
|
---|
| 140 | * If all backtrace items have been seen already, there's no need to
|
---|
| 141 | * try raising an error.
|
---|
| 142 | */
|
---|
[1a3b953] | 143 | void *trace[256];
|
---|
| 144 | int size = backtrace(trace, 256);
|
---|
| 145 | int raise_error = 0;
|
---|
| 146 | for (int i = 0; i < size; i++) {
|
---|
| 147 | if (backtrace_item_hit(trace[i]))
|
---|
| 148 | raise_error = 1;
|
---|
[a42d7d8] | 149 | }
|
---|
[1a3b953] | 150 | if (!raise_error)
|
---|
| 151 | return 0;
|
---|
[a42d7d8] | 152 |
|
---|
| 153 | if (!fork()) {
|
---|
[1a3b953] | 154 | /* Child silently fails. */
|
---|
[a42d7d8] | 155 | int null = open("/dev/null", O_WRONLY);
|
---|
| 156 | if (null == -1)
|
---|
| 157 | exit(127);
|
---|
[fcab7ef] | 158 | vfs_clone(null, STDOUT_FILENO, false);
|
---|
| 159 | vfs_clone(null, STDERR_FILENO, false);
|
---|
[9c4cf0d] | 160 | vfs_put(null);
|
---|
[a42d7d8] | 161 | return 1;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[1a3b953] | 164 | /* Parent checks whether child failed correctly. */
|
---|
[a42d7d8] | 165 | int status;
|
---|
| 166 | wait(&status);
|
---|
| 167 | if (WIFEXITED(status) && WEXITSTATUS(status) == 1)
|
---|
| 168 | return 0;
|
---|
| 169 |
|
---|
[7c3fb9b] | 170 | /*
|
---|
| 171 | * The child had an error! We couldn't easily debug it because it was
|
---|
[1a3b953] | 172 | * in a separate process with redirected stdout and stderr. Do it again
|
---|
[7c3fb9b] | 173 | * without redirecting or forking.
|
---|
| 174 | */
|
---|
[a42d7d8] | 175 | fprintf(stderr, "** Fake error raised here (BITHENGE_FAILURE_INDEX=%d)\n",
|
---|
| 176 | g_failure_index);
|
---|
| 177 | return 1;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | void *bithenge_failure_malloc(size_t size)
|
---|
| 181 | {
|
---|
[1a3b953] | 182 | if (bithenge_should_fail())
|
---|
[a42d7d8] | 183 | return NULL;
|
---|
| 184 | return malloc(size);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | void *bithenge_failure_realloc(void *ptr, size_t size)
|
---|
| 188 | {
|
---|
[1a3b953] | 189 | if (bithenge_should_fail())
|
---|
[a42d7d8] | 190 | return NULL;
|
---|
| 191 | return realloc(ptr, size);
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | ssize_t bithenge_failure_read(int fd, void *buf, size_t count)
|
---|
| 195 | {
|
---|
[1a3b953] | 196 | if (bithenge_should_fail()) {
|
---|
[a42d7d8] | 197 | errno = EIO;
|
---|
| 198 | return -1;
|
---|
| 199 | }
|
---|
| 200 | return read(fd, buf, count);
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | off_t bithenge_failure_lseek(int fd, off_t offset, int whither)
|
---|
| 204 | {
|
---|
[1a3b953] | 205 | if (bithenge_should_fail()) {
|
---|
[a42d7d8] | 206 | errno = EINVAL;
|
---|
| 207 | return (off_t) -1;
|
---|
| 208 | }
|
---|
| 209 | return lseek(fd, offset, whither);
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[b7fd2a0] | 212 | errno_t bithenge_failure_ferror(FILE *stream)
|
---|
[1a3b953] | 213 | {
|
---|
| 214 | if (bithenge_should_fail())
|
---|
| 215 | return 1;
|
---|
| 216 | return ferror(stream);
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | char *bithenge_failure_str_ndup(const char *s, size_t max_len)
|
---|
| 220 | {
|
---|
| 221 | if (bithenge_should_fail())
|
---|
| 222 | return NULL;
|
---|
| 223 | return str_ndup(s, max_len);
|
---|
| 224 | }
|
---|
| 225 |
|
---|
[b7fd2a0] | 226 | errno_t bithenge_failure_open(const char *pathname, int flags)
|
---|
[1a3b953] | 227 | {
|
---|
| 228 | if (bithenge_should_fail()) {
|
---|
| 229 | errno = EACCES;
|
---|
| 230 | return -1;
|
---|
| 231 | }
|
---|
| 232 | return open(pathname, flags);
|
---|
| 233 | }
|
---|
| 234 |
|
---|
[39330200] | 235 | errno_t bithenge_failure_fstat(int fd, vfs_stat_t *buf)
|
---|
[1a3b953] | 236 | {
|
---|
| 237 | if (bithenge_should_fail()) {
|
---|
| 238 | errno = EIO;
|
---|
| 239 | return -1;
|
---|
| 240 | }
|
---|
| 241 | return fstat(fd, buf);
|
---|
| 242 | }
|
---|
| 243 |
|
---|
[a42d7d8] | 244 | /** @}
|
---|
| 245 | */
|
---|
[0784869] | 246 | /** @endcond */
|
---|