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