source: mainline/uspace/lib/bithenge/src/failure.c

Last change on this file was 7c3fb9b, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix block comment formatting (ccheck).

  • Property mode set to 100644
File size: 6.2 KB
Line 
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
29/** @cond internal */
30/** @addtogroup bithenge
31 * @{
32 */
33/**
34 * @file
35 * Fake system call errors for testing.
36 */
37
38#include <errno.h>
39#include <execinfo.h>
40#include <fcntl.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <stddef.h>
44#include <sys/wait.h>
45#define BITHENGE_FAILURE_DECLS_ONLY 1
46#include "failure.h"
47#include "common.h"
48
49/*
50 * This file raises fake errors from system calls, to test that Bithenge
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
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.
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
63 * call.
64 */
65
66static int g_initialized = 0;
67static int g_failure_index = -1;
68static int g_failure_index_selected = -1;
69
70typedef struct backtrace_item {
71 struct backtrace_item *next;
72 void *backtrace_item;
73} backtrace_item_t;
74
75static backtrace_item_t *g_backtrace_items = NULL;
76
77static void atexit_handler(void)
78{
79 while (g_backtrace_items) {
80 backtrace_item_t *first = g_backtrace_items;
81 g_backtrace_items = first->next;
82 free(first);
83 }
84}
85
86static inline void initialize(void)
87{
88 g_initialized = 1;
89 errno_t 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/*
99 * Record a hit for a backtrace address and return whether this is the first
100 * hit.
101 */
102static inline errno_t backtrace_item_hit(void *addr)
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;
113 }
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
126errno_t bithenge_should_fail(void)
127{
128 g_failure_index++;
129
130 if (!g_initialized)
131 initialize();
132
133 if (g_failure_index_selected != -1) {
134 if (g_failure_index == g_failure_index_selected)
135 return 1; /* breakpoint here */
136 return 0;
137 }
138
139 /*
140 * If all backtrace items have been seen already, there's no need to
141 * try raising an error.
142 */
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;
149 }
150 if (!raise_error)
151 return 0;
152
153 if (!fork()) {
154 /* Child silently fails. */
155 int null = open("/dev/null", O_WRONLY);
156 if (null == -1)
157 exit(127);
158 vfs_clone(null, STDOUT_FILENO, false);
159 vfs_clone(null, STDERR_FILENO, false);
160 vfs_put(null);
161 return 1;
162 }
163
164 /* Parent checks whether child failed correctly. */
165 int status;
166 wait(&status);
167 if (WIFEXITED(status) && WEXITSTATUS(status) == 1)
168 return 0;
169
170 /*
171 * The child had an error! We couldn't easily debug it because it was
172 * in a separate process with redirected stdout and stderr. Do it again
173 * without redirecting or forking.
174 */
175 fprintf(stderr, "** Fake error raised here (BITHENGE_FAILURE_INDEX=%d)\n",
176 g_failure_index);
177 return 1;
178}
179
180void *bithenge_failure_malloc(size_t size)
181{
182 if (bithenge_should_fail())
183 return NULL;
184 return malloc(size);
185}
186
187void *bithenge_failure_realloc(void *ptr, size_t size)
188{
189 if (bithenge_should_fail())
190 return NULL;
191 return realloc(ptr, size);
192}
193
194ssize_t bithenge_failure_read(int fd, void *buf, size_t count)
195{
196 if (bithenge_should_fail()) {
197 errno = EIO;
198 return -1;
199 }
200 return read(fd, buf, count);
201}
202
203off_t bithenge_failure_lseek(int fd, off_t offset, int whither)
204{
205 if (bithenge_should_fail()) {
206 errno = EINVAL;
207 return (off_t) -1;
208 }
209 return lseek(fd, offset, whither);
210}
211
212errno_t bithenge_failure_ferror(FILE *stream)
213{
214 if (bithenge_should_fail())
215 return 1;
216 return ferror(stream);
217}
218
219char *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
226errno_t bithenge_failure_open(const char *pathname, int flags)
227{
228 if (bithenge_should_fail()) {
229 errno = EACCES;
230 return -1;
231 }
232 return open(pathname, flags);
233}
234
235errno_t bithenge_failure_fstat(int fd, vfs_stat_t *buf)
236{
237 if (bithenge_should_fail()) {
238 errno = EIO;
239 return -1;
240 }
241 return fstat(fd, buf);
242}
243
244/** @}
245 */
246/** @endcond */
Note: See TracBrowser for help on using the repository browser.