[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 |
|
---|
| 49 | /* This file raises fake errors from system calls, to test that Bithenge
|
---|
| 50 | * handles the errors correctly. It has two primary modes of operation,
|
---|
| 51 | * depending on an environment variable:
|
---|
| 52 | *
|
---|
| 53 | * BITHENGE_FAILURE_INDEX not set: when a system call is made, a child process
|
---|
| 54 | * returns a fake error from that call. If the child process handles the error
|
---|
| 55 | * correctly (exit code is 1), the main process continues without errors. If
|
---|
| 56 | * the child process has a problem, the main process raises the fake error
|
---|
[1a3b953] | 57 | * again and shows all stdout and stderr output. For speed, errors are only
|
---|
| 58 | * raised when part of the backtrace has not been seen before.
|
---|
[a42d7d8] | 59 | *
|
---|
| 60 | * BITHENGE_FAILURE_INDEX set: the program runs normally until system call
|
---|
| 61 | * number BITHENGE_FAILURE_INDEX is made; a fake error is returned from this
|
---|
| 62 | * call. */
|
---|
| 63 |
|
---|
[1a3b953] | 64 | static int g_initialized = 0;
|
---|
[a42d7d8] | 65 | static int g_failure_index = -1;
|
---|
[1a3b953] | 66 | static int g_failure_index_selected = -1;
|
---|
[a42d7d8] | 67 |
|
---|
[1a3b953] | 68 | typedef struct backtrace_item {
|
---|
| 69 | struct backtrace_item *next;
|
---|
| 70 | void *backtrace_item;
|
---|
| 71 | } backtrace_item_t;
|
---|
| 72 |
|
---|
| 73 | static backtrace_item_t *g_backtrace_items = NULL;
|
---|
| 74 |
|
---|
| 75 | static void atexit_handler(void)
|
---|
[a42d7d8] | 76 | {
|
---|
[1a3b953] | 77 | while (g_backtrace_items) {
|
---|
| 78 | backtrace_item_t *first = g_backtrace_items;
|
---|
| 79 | g_backtrace_items = first->next;
|
---|
| 80 | free(first);
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
[a42d7d8] | 83 |
|
---|
[1a3b953] | 84 | static inline void initialize(void)
|
---|
| 85 | {
|
---|
| 86 | g_initialized = 1;
|
---|
[b7fd2a0] | 87 | errno_t rc = atexit(atexit_handler);
|
---|
[1a3b953] | 88 | if (rc)
|
---|
| 89 | exit(127);
|
---|
| 90 |
|
---|
| 91 | char *sel_str = getenv("BITHENGE_FAILURE_INDEX");
|
---|
| 92 | if (sel_str)
|
---|
| 93 | g_failure_index_selected = strtol(sel_str, NULL, 10);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | /* Record a hit for a backtrace address and return whether this is the first
|
---|
| 97 | * hit. */
|
---|
[b7fd2a0] | 98 | static inline errno_t backtrace_item_hit(void *addr)
|
---|
[1a3b953] | 99 | {
|
---|
| 100 | backtrace_item_t **bip;
|
---|
| 101 | for (bip = &g_backtrace_items; *bip; bip = &(*bip)->next) {
|
---|
| 102 | backtrace_item_t *bi = *bip;
|
---|
| 103 | if (bi->backtrace_item == addr) {
|
---|
| 104 | /* Keep frequently accessed items near the front. */
|
---|
| 105 | *bip = bi->next;
|
---|
| 106 | bi->next = g_backtrace_items;
|
---|
| 107 | g_backtrace_items = bi;
|
---|
| 108 | return 0;
|
---|
[a42d7d8] | 109 | }
|
---|
[1a3b953] | 110 | }
|
---|
| 111 |
|
---|
| 112 | /* No item found; create one. */
|
---|
| 113 | backtrace_item_t *i = malloc(sizeof(*i));
|
---|
| 114 | if (!i)
|
---|
| 115 | exit(127);
|
---|
| 116 | i->next = g_backtrace_items;
|
---|
| 117 | i->backtrace_item = addr;
|
---|
| 118 | g_backtrace_items = i;
|
---|
| 119 | return 1;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[b7fd2a0] | 122 | errno_t bithenge_should_fail(void)
|
---|
[1a3b953] | 123 | {
|
---|
| 124 | g_failure_index++;
|
---|
| 125 |
|
---|
| 126 | if (!g_initialized)
|
---|
| 127 | initialize();
|
---|
| 128 |
|
---|
| 129 | if (g_failure_index_selected != -1) {
|
---|
[a42d7d8] | 130 | if (g_failure_index == g_failure_index_selected)
|
---|
| 131 | return 1; /* breakpoint here */
|
---|
| 132 | return 0;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[1a3b953] | 135 | /* If all backtrace items have been seen already, there's no need to
|
---|
| 136 | * try raising an error. */
|
---|
| 137 | void *trace[256];
|
---|
| 138 | int size = backtrace(trace, 256);
|
---|
| 139 | int raise_error = 0;
|
---|
| 140 | for (int i = 0; i < size; i++) {
|
---|
| 141 | if (backtrace_item_hit(trace[i]))
|
---|
| 142 | raise_error = 1;
|
---|
[a42d7d8] | 143 | }
|
---|
[1a3b953] | 144 | if (!raise_error)
|
---|
| 145 | return 0;
|
---|
[a42d7d8] | 146 |
|
---|
| 147 | if (!fork()) {
|
---|
[1a3b953] | 148 | /* Child silently fails. */
|
---|
[a42d7d8] | 149 | int null = open("/dev/null", O_WRONLY);
|
---|
| 150 | if (null == -1)
|
---|
| 151 | exit(127);
|
---|
[fcab7ef] | 152 | vfs_clone(null, STDOUT_FILENO, false);
|
---|
| 153 | vfs_clone(null, STDERR_FILENO, false);
|
---|
[9c4cf0d] | 154 | vfs_put(null);
|
---|
[a42d7d8] | 155 | return 1;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[1a3b953] | 158 | /* Parent checks whether child failed correctly. */
|
---|
[a42d7d8] | 159 | int status;
|
---|
| 160 | wait(&status);
|
---|
| 161 | if (WIFEXITED(status) && WEXITSTATUS(status) == 1)
|
---|
| 162 | return 0;
|
---|
| 163 |
|
---|
[1a3b953] | 164 | /* The child had an error! We couldn't easily debug it because it was
|
---|
| 165 | * in a separate process with redirected stdout and stderr. Do it again
|
---|
| 166 | * without redirecting or forking. */
|
---|
[a42d7d8] | 167 | fprintf(stderr, "** Fake error raised here (BITHENGE_FAILURE_INDEX=%d)\n",
|
---|
| 168 | g_failure_index);
|
---|
| 169 | return 1;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | void *bithenge_failure_malloc(size_t size)
|
---|
| 173 | {
|
---|
[1a3b953] | 174 | if (bithenge_should_fail())
|
---|
[a42d7d8] | 175 | return NULL;
|
---|
| 176 | return malloc(size);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | void *bithenge_failure_realloc(void *ptr, size_t size)
|
---|
| 180 | {
|
---|
[1a3b953] | 181 | if (bithenge_should_fail())
|
---|
[a42d7d8] | 182 | return NULL;
|
---|
| 183 | return realloc(ptr, size);
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | ssize_t bithenge_failure_read(int fd, void *buf, size_t count)
|
---|
| 187 | {
|
---|
[1a3b953] | 188 | if (bithenge_should_fail()) {
|
---|
[a42d7d8] | 189 | errno = EIO;
|
---|
| 190 | return -1;
|
---|
| 191 | }
|
---|
| 192 | return read(fd, buf, count);
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | off_t bithenge_failure_lseek(int fd, off_t offset, int whither)
|
---|
| 196 | {
|
---|
[1a3b953] | 197 | if (bithenge_should_fail()) {
|
---|
[a42d7d8] | 198 | errno = EINVAL;
|
---|
| 199 | return (off_t) -1;
|
---|
| 200 | }
|
---|
| 201 | return lseek(fd, offset, whither);
|
---|
| 202 | }
|
---|
| 203 |
|
---|
[b7fd2a0] | 204 | errno_t bithenge_failure_ferror(FILE *stream)
|
---|
[1a3b953] | 205 | {
|
---|
| 206 | if (bithenge_should_fail())
|
---|
| 207 | return 1;
|
---|
| 208 | return ferror(stream);
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | char *bithenge_failure_str_ndup(const char *s, size_t max_len)
|
---|
| 212 | {
|
---|
| 213 | if (bithenge_should_fail())
|
---|
| 214 | return NULL;
|
---|
| 215 | return str_ndup(s, max_len);
|
---|
| 216 | }
|
---|
| 217 |
|
---|
[b7fd2a0] | 218 | errno_t bithenge_failure_open(const char *pathname, int flags)
|
---|
[1a3b953] | 219 | {
|
---|
| 220 | if (bithenge_should_fail()) {
|
---|
| 221 | errno = EACCES;
|
---|
| 222 | return -1;
|
---|
| 223 | }
|
---|
| 224 | return open(pathname, flags);
|
---|
| 225 | }
|
---|
| 226 |
|
---|
[39330200] | 227 | errno_t bithenge_failure_fstat(int fd, vfs_stat_t *buf)
|
---|
[1a3b953] | 228 | {
|
---|
| 229 | if (bithenge_should_fail()) {
|
---|
| 230 | errno = EIO;
|
---|
| 231 | return -1;
|
---|
| 232 | }
|
---|
| 233 | return fstat(fd, buf);
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[a42d7d8] | 236 | /** @}
|
---|
| 237 | */
|
---|
[0784869] | 238 | /** @endcond */
|
---|