| 1 |
// Copyright (c) 2014, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <openssl/base.h> |
| 5 |
|
| 6 |
#if defined(__GLIBC__) && !defined(__UCLIBC__) |
| 7 |
#define OPENSSL_GLIBC |
| 8 |
#endif |
| 9 |
|
| 10 |
// This file isn't built on ARM or Aarch64 because we link statically in those |
| 11 |
// builds and trying to override malloc in a static link doesn't work. It also |
| 12 |
// requires glibc. It's also disabled on ASan builds as this interferes with |
| 13 |
// ASan's malloc interceptor. |
| 14 |
// |
| 15 |
// TODO(davidben): See if this and ASan's and MSan's interceptors can be made to |
| 16 |
// coexist. |
| 17 |
#if defined(__linux__) && defined(OPENSSL_GLIBC) && !defined(OPENSSL_ARM) && \ |
| 18 |
!defined(OPENSSL_AARCH64) && !defined(OPENSSL_ASAN) && \ |
| 19 |
!defined(OPENSSL_MSAN) && !defined(OPENSSL_TSAN) |
| 20 |
|
| 21 |
#include <errno.h> |
| 22 |
#include <signal.h> |
| 23 |
#include <stdint.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <unistd.h> |
| 27 |
|
| 28 |
#include <new> |
| 29 |
|
| 30 |
|
| 31 |
// This file defines overrides for the standard allocation functions that allow |
| 32 |
// a given allocation to be made to fail for testing. If the program is run |
| 33 |
// with MALLOC_NUMBER_TO_FAIL set to a base-10 number then that allocation will |
| 34 |
// return NULL. If MALLOC_BREAK_ON_FAIL is also defined then the allocation |
| 35 |
// will signal SIGTRAP rather than return NULL. |
| 36 |
// |
| 37 |
// This code is not thread safe. |
| 38 |
|
| 39 |
static uint64_t current_malloc_count = 0; |
| 40 |
static uint64_t malloc_number_to_fail = 0; |
| 41 |
static bool failure_enabled = false, break_on_fail = false, in_call = false; |
| 42 |
|
| 43 |
extern "C" { |
| 44 |
// These are other names for the standard allocation functions. |
| 45 |
extern void *__libc_malloc(size_t size); |
| 46 |
extern void *__libc_calloc(size_t num_elems, size_t size); |
| 47 |
extern void *__libc_realloc(void *ptr, size_t size); |
| 48 |
} |
| 49 |
|
| 50 |
static void exit_handler(void) { |
| 51 |
if (failure_enabled && current_malloc_count > malloc_number_to_fail) { |
| 52 |
_exit(88); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
static void cpp_new_handler() { |
| 57 |
// Return to try again. It won't fail a second time. |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
// should_fail_allocation returns true if the current allocation should fail. |
| 62 |
static bool should_fail_allocation() { |
| 63 |
static bool init = false; |
| 64 |
|
| 65 |
if (in_call) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
in_call = true; |
| 70 |
|
| 71 |
if (!init) { |
| 72 |
const char *env = getenv("MALLOC_NUMBER_TO_FAIL"); |
| 73 |
if (env != NULL && env[0] != 0) { |
| 74 |
char *endptr; |
| 75 |
malloc_number_to_fail = strtoull(env, &endptr, 10); |
| 76 |
if (*endptr == 0) { |
| 77 |
failure_enabled = true; |
| 78 |
atexit(exit_handler); |
| 79 |
std::set_new_handler(cpp_new_handler); |
| 80 |
} |
| 81 |
} |
| 82 |
break_on_fail = (NULL != getenv("MALLOC_BREAK_ON_FAIL")); |
| 83 |
init = true; |
| 84 |
} |
| 85 |
|
| 86 |
in_call = false; |
| 87 |
|
| 88 |
if (!failure_enabled) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
bool should_fail = (current_malloc_count == malloc_number_to_fail); |
| 93 |
current_malloc_count++; |
| 94 |
|
| 95 |
if (should_fail && break_on_fail) { |
| 96 |
raise(SIGTRAP); |
| 97 |
} |
| 98 |
return should_fail; |
| 99 |
} |
| 100 |
|
| 101 |
extern "C" { |
| 102 |
|
| 103 |
void *malloc(size_t size) { |
| 104 |
if (should_fail_allocation()) { |
| 105 |
errno = ENOMEM; |
| 106 |
return NULL; |
| 107 |
} |
| 108 |
|
| 109 |
return __libc_malloc(size); |
| 110 |
} |
| 111 |
|
| 112 |
void *calloc(size_t num_elems, size_t size) { |
| 113 |
if (should_fail_allocation()) { |
| 114 |
errno = ENOMEM; |
| 115 |
return NULL; |
| 116 |
} |
| 117 |
|
| 118 |
return __libc_calloc(num_elems, size); |
| 119 |
} |
| 120 |
|
| 121 |
void *realloc(void *ptr, size_t size) { |
| 122 |
if (should_fail_allocation()) { |
| 123 |
errno = ENOMEM; |
| 124 |
return NULL; |
| 125 |
} |
| 126 |
|
| 127 |
return __libc_realloc(ptr, size); |
| 128 |
} |
| 129 |
|
| 130 |
} // extern "C" |
| 131 |
|
| 132 |
#endif // defined(linux) && GLIBC && !ARM && !AARCH64 && !ASAN && !TSAN |