17 #if defined(__GLIBC__) && !defined(__UCLIBC__)
28 #if defined(__linux__) && defined(OPENSSL_GLIBC) && !defined(OPENSSL_ARM) && \
29 !defined(OPENSSL_AARCH64) && !defined(OPENSSL_ASAN) && \
30 !defined(OPENSSL_MSAN) && !defined(OPENSSL_TSAN)
50 static uint64_t current_malloc_count = 0;
51 static uint64_t malloc_number_to_fail = 0;
52 static bool failure_enabled =
false, break_on_fail =
false, in_call =
false;
56 extern void *__libc_malloc(
size_t size);
57 extern void *__libc_calloc(
size_t num_elems,
size_t size);
58 extern void *__libc_realloc(
void *
ptr,
size_t size);
61 static void exit_handler(
void) {
62 if (failure_enabled && current_malloc_count > malloc_number_to_fail) {
67 static void cpp_new_handler() {
73 static bool should_fail_allocation() {
74 static bool init =
false;
83 const char *
env =
getenv(
"MALLOC_NUMBER_TO_FAIL");
84 if (
env != NULL &&
env[0] != 0) {
86 malloc_number_to_fail = strtoull(
env, &endptr, 10);
88 failure_enabled =
true;
90 std::set_new_handler(cpp_new_handler);
93 break_on_fail = (NULL !=
getenv(
"MALLOC_BREAK_ON_FAIL"));
99 if (!failure_enabled) {
103 bool should_fail = (current_malloc_count == malloc_number_to_fail);
104 current_malloc_count++;
106 if (should_fail && break_on_fail) {
114 void *malloc(
size_t size) {
115 if (should_fail_allocation()) {
120 return __libc_malloc(
size);
123 void *calloc(
size_t num_elems,
size_t size) {
124 if (should_fail_allocation()) {
129 return __libc_calloc(num_elems,
size);
132 void *realloc(
void *
ptr,
size_t size) {
133 if (should_fail_allocation()) {
138 return __libc_realloc(
ptr,
size);
143 #endif // defined(linux) && GLIBC && !ARM && !AARCH64 && !ASAN && !TSAN