aboutsummaryrefslogtreecommitdiffstats
path: root/test/mfail/mfail.c
blob: ec391c8e85ecb2f0bf3a35b03ee3a544f43f4fc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
 * Copyright 2026 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include "mfail.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/crypto.h>

#if defined(__has_feature)
#if __has_feature(address_sanitizer)
#define MFAIL_HAVE_ASAN 1
#endif
#endif
#if defined(__SANITIZE_ADDRESS__) && !defined(MFAIL_HAVE_ASAN)
#define MFAIL_HAVE_ASAN 1
#endif

#ifdef MFAIL_HAVE_ASAN
extern void __sanitizer_print_stack_trace(void);
#elif defined(__GLIBC__) || defined(__APPLE__)
#include <execinfo.h>
#define MFAIL_HAVE_BACKTRACE
#define MFAIL_BT_MAX 128
#endif

static struct {
    int installed;
    int skip_all;
    int skip_slow;
    int single_point;
    int start_point;
    int env_count;
    int count_only;
    int count_only_env;
    int sample_count;
    int slow_threshold;
    int print_bt;
    int mode;
    int phase;
    int seq;
    int next_point;
    int iter_index;
    int n;
    int total;
    int iterations;
    int started;
    int fail_after;
    int alloc_count;
    int triggered;
    int counting;
    int slow_skipped;
    int no_check;
} mf;

static int env_is_true(const char *name)
{
    const char *value = getenv(name);
    return value != NULL && *value != '\0' && strcmp(value, "0") != 0;
}

static int env_int(const char *name, int dflt)
{
    const char *value = getenv(name);
    return (value != NULL && *value != '\0') ? atoi(value) : dflt;
}

static void mfail_print_bt(void)
{
#ifdef MFAIL_HAVE_ASAN
    fprintf(stderr, "# MFAIL_BT (failure injection point)\n");
    __sanitizer_print_stack_trace();
#elif defined(MFAIL_HAVE_BACKTRACE)
    void *buf[MFAIL_BT_MAX];
    char **syms;
    int n, i;

    n = backtrace(buf, MFAIL_BT_MAX);
    syms = backtrace_symbols(buf, n);
    if (syms == NULL)
        return;

    fprintf(stderr, "# MFAIL_BT (failure injection point)\n");
    /* Skip frame 0 (this function) */
    for (i = 1; i < n; i++)
        fprintf(stderr, "#   %s\n", syms[i]);

    free(syms);
#endif
}

static int should_fail(const char *file)
{
    int idx;

    if (!mf.counting)
        return 0;
    /* skip if checking errors and file is not set (debug and error parts) */
    if (!mf.no_check && file == NULL)
        return 0;

    idx = mf.alloc_count++;

    if (mf.fail_after < 0 || mf.triggered)
        return 0;
    if (idx == mf.fail_after) {
        mf.triggered = 1;
        if (mf.print_bt)
            mfail_print_bt();
        return 1;
    }
    return 0;
}

static void *mf_malloc(size_t num, const char *file, int line)
{
    if (num == 0)
        return NULL;
    if (should_fail(file))
        return NULL;
    return malloc(num);
}

static void *mf_realloc(void *addr, size_t num, const char *file, int line)
{
    if (addr == NULL)
        return mf_malloc(num, file, line);
    if (num == 0) {
        free(addr);
        return NULL;
    }
    if (should_fail(file))
        return NULL;
    return realloc(addr, num);
}

static void mf_free(void *addr, const char *file, int line)
{
    free(addr);
}

int mfail_install(int optional)
{
    if (mf.installed)
        return 1;
    if (env_is_true("OPENSSL_TEST_MFAIL_DISABLE"))
        return 0;

    mf.skip_all = env_is_true("OPENSSL_TEST_MFAIL_SKIP_ALL");
    mf.skip_slow = env_is_true("OPENSSL_TEST_MFAIL_SKIP_SLOW");
    mf.single_point = env_int("OPENSSL_TEST_MFAIL_POINT", -1);
    mf.start_point = env_int("OPENSSL_TEST_MFAIL_START", 0);
    mf.env_count = env_int("OPENSSL_TEST_MFAIL_COUNT", 0);
    mf.count_only_env = env_is_true("OPENSSL_TEST_MFAIL_COUNT_ONLY");
    mf.slow_threshold = env_int("OPENSSL_TEST_MFAIL_SLOW", 1000);
    mf.print_bt = env_is_true("OPENSSL_TEST_MFAIL_BACKTRACE");

    /* if optional and nothing configured, then no point installing hooks */
    if (optional && mf.env_count <= 0 && mf.single_point < 0
        && !mf.count_only_env)
        return 0;

    if (!CRYPTO_set_mem_functions(mf_malloc, mf_realloc, mf_free))
        return -1;

    mf.installed = 1;
    mf.fail_after = -1;
    return 1;
}

int mfail_is_installed(void)
{
    return mf.installed;
}

int mfail_env_skip_all(void)
{
    return !mf.installed || mf.skip_all;
}

int mfail_env_skip_slow(void)
{
    return !mf.installed || mf.skip_slow;
}

/* The i-th of n points distributed over [start, total), rotated by seq */
static int compute_point(int i, int total, int n, int seq, int start)
{
    int range = total - start;
    int stride_int, stride_rem_x2, stride_rnd, offset, p;

    if (range <= 0 || n <= 0)
        return start;

    stride_int = range / n;
    stride_rem_x2 = (range - stride_int * n) * 2;
    stride_rnd = stride_int + (stride_rem_x2 >= n ? 1 : 0);
    offset = (stride_rnd > 0) ? (seq % stride_rnd) : 0;

    p = (int)(((long)i * range + n / 2) / n) + offset;
    if (p >= range)
        p = range - 1;
    if (p < 0)
        p = 0;
    return start + p;
}

void mfail_init(int seq, int flags)
{
    mfail_init_ex(seq, flags, 0);
}

void mfail_init_ex(int seq, int flags, int count)
{
    mf.seq = seq;
    mf.iter_index = 0;
    mf.n = 0;
    mf.total = 0;
    mf.iterations = 0;
    mf.started = 0;
    mf.fail_after = -1;
    mf.alloc_count = 0;
    mf.triggered = 0;
    mf.counting = 0;
    mf.slow_skipped = 0;
    mf.no_check = flags & MFAIL_FLAG_NO_CHECK;
    mf.count_only = mf.count_only_env || (flags & MFAIL_FLAG_COUNT_ONLY) != 0;
    /* env count overrides the requested per-test sample count */
    mf.sample_count = mf.env_count > 0 ? mf.env_count : count;

    if (mf.single_point >= 0) {
        mf.mode = MFAIL_MODE_SINGLE;
    } else if ((flags & MFAIL_FLAG_COUNT) && mf.sample_count > 0) {
        mf.mode = MFAIL_MODE_SAMPLED;
    } else {
        mf.mode = MFAIL_MODE_EXHAUSTIVE;
    }
    mf.phase = MFAIL_PHASE_COUNTING;
    mf.next_point = -1;
}

int mfail_has_next(void)
{
    if (mf.started) {
        mf.iterations++;
        switch (mf.phase) {
        case MFAIL_PHASE_COUNTING:
            mf.total = mf.alloc_count;
            if (mf.count_only) {
                mf.phase = MFAIL_PHASE_DONE;
                break;
            }
            /* sampled runs are bounded, so slow-skip only applies otherwise */
            if (mf.skip_slow && mf.mode != MFAIL_MODE_SAMPLED
                && mf.total > mf.slow_threshold) {
                mf.slow_skipped = 1;
                mf.phase = MFAIL_PHASE_DONE;
                break;
            }
            if (mf.mode == MFAIL_MODE_SINGLE) {
                mf.phase = MFAIL_PHASE_INJECTING;
                mf.next_point = mf.single_point;
            } else if (mf.mode == MFAIL_MODE_EXHAUSTIVE) {
                if (mf.total > mf.start_point) {
                    mf.phase = MFAIL_PHASE_INJECTING;
                    mf.next_point = mf.start_point;
                } else {
                    mf.phase = MFAIL_PHASE_DONE;
                }
            } else { /* mf.mode is MFAIL_MODE_SAMPLED */
                mf.n = mf.sample_count;
                if (mf.n > mf.total)
                    mf.n = mf.total;
                if (mf.n > 0 && mf.total > mf.start_point) {
                    mf.phase = MFAIL_PHASE_INJECTING;
                    mf.iter_index = 0;
                    mf.next_point = compute_point(0, mf.total, mf.n, mf.seq,
                        mf.start_point);
                } else {
                    mf.phase = MFAIL_PHASE_DONE;
                }
            }
            break;
        case MFAIL_PHASE_INJECTING:
            if (mf.mode == MFAIL_MODE_SINGLE) {
                mf.phase = MFAIL_PHASE_DONE;
            } else if (mf.mode == MFAIL_MODE_EXHAUSTIVE) {
                if (++mf.next_point >= mf.total)
                    mf.phase = MFAIL_PHASE_DONE;
            } else { /* SAMPLED */
                if (++mf.iter_index >= mf.n)
                    mf.phase = MFAIL_PHASE_DONE;
                else
                    mf.next_point = compute_point(mf.iter_index, mf.total,
                        mf.n, mf.seq, mf.start_point);
            }
            break;
        case MFAIL_PHASE_DONE:
        default:
            break;
        }
    } else {
        mf.started = 1;
    }

    if (mf.phase == MFAIL_PHASE_DONE)
        return 0;

    mf.alloc_count = 0;
    mf.counting = 0;
    mf.triggered = 0;
    mf.fail_after = (mf.phase == MFAIL_PHASE_INJECTING) ? mf.next_point : -1;
    return 1;
}

void mfail_start(void)
{
    mf.alloc_count = 0;
    mf.counting = 1;
}

void mfail_end(void)
{
    mf.counting = 0;
}

void mfail_arm_once(int point)
{
    mf.fail_after = point;
    mf.alloc_count = 0;
    mf.triggered = 0;
}

void mfail_disarm(void)
{
    mf.fail_after = -1;
    mf.alloc_count = 0;
    mf.triggered = 0;
}

int mfail_was_triggered(void)
{
    return mf.triggered;
}

int mfail_was_slow_skipped(void)
{
    return mf.slow_skipped;
}

int mfail_is_count_only(void)
{
    return mf.count_only;
}

int mfail_get_count(void)
{
    return mf.alloc_count;
}

int mfail_get_total(void)
{
    return mf.total;
}

int mfail_get_phase(void)
{
    return mf.phase;
}

int mfail_get_mode(void)
{
    return mf.mode;
}

int mfail_iterations(void)
{
    return mf.iterations;
}

int mfail_get_slow_threshold(void)
{
    return mf.slow_threshold;
}

int mfail_get_point(void)
{
    return (mf.phase == MFAIL_PHASE_INJECTING) ? mf.next_point : -1;
}