Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2014, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
Adam Langley | 3e65265 | 2015-01-09 15:44:37 -0800 | [diff] [blame^] | 15 | #include <openssl/base.h> |
| 16 | |
| 17 | // This file isn't built on ARM or Aarch64 because we link statically in those |
| 18 | // builds and trying to override malloc in a static link doesn't work. |
| 19 | #if defined(__linux__) && !defined(OPENSSL_ARM) && !defined(OPENSSL_AARCH64) |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame] | 20 | |
| 21 | #include <stdint.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
| 24 | #include <unistd.h> |
| 25 | #include <stdio.h> |
| 26 | |
| 27 | #include <new> |
| 28 | |
| 29 | |
| 30 | /* This file defines overrides for the standard allocation functions that allow |
| 31 | * a given allocation to be made to fail for testing. If the program is run |
| 32 | * with MALLOC_NUMBER_TO_FAIL set to a base-10 number then that allocation will |
| 33 | * return NULL. If MALLOC_ABORT_ON_FAIL is also defined then the allocation |
| 34 | * will abort() rather than return NULL. |
| 35 | * |
| 36 | * This code is not thread safe. */ |
| 37 | |
| 38 | static uint64_t current_malloc_count = 0; |
| 39 | static uint64_t malloc_number_to_fail = 0; |
| 40 | static char failure_enabled = 0, abort_on_fail = 0; |
| 41 | static int in_call = 0; |
| 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 int should_fail_allocation() { |
| 63 | static int init = 0; |
| 64 | char should_fail; |
| 65 | |
| 66 | if (in_call) { |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | in_call = 1; |
| 71 | |
| 72 | if (!init) { |
| 73 | const char *env = getenv("MALLOC_NUMBER_TO_FAIL"); |
| 74 | if (env != NULL && env[0] != 0) { |
| 75 | char *endptr; |
| 76 | malloc_number_to_fail = strtoull(env, &endptr, 10); |
| 77 | if (*endptr == 0) { |
| 78 | failure_enabled = 1; |
| 79 | atexit(exit_handler); |
| 80 | std::set_new_handler(cpp_new_handler); |
| 81 | } |
| 82 | } |
| 83 | abort_on_fail = (NULL != getenv("MALLOC_ABORT_ON_FAIL")); |
| 84 | init = 1; |
| 85 | } |
| 86 | |
| 87 | in_call = 0; |
| 88 | |
| 89 | if (!failure_enabled) { |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | should_fail = (current_malloc_count == malloc_number_to_fail); |
| 94 | current_malloc_count++; |
| 95 | |
| 96 | if (should_fail && abort_on_fail) { |
| 97 | abort(); |
| 98 | } |
| 99 | return should_fail; |
| 100 | } |
| 101 | |
| 102 | extern "C" { |
| 103 | |
| 104 | void *malloc(size_t size) { |
| 105 | if (should_fail_allocation()) { |
| 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 | return NULL; |
| 115 | } |
| 116 | |
| 117 | return __libc_calloc(num_elems, size); |
| 118 | } |
| 119 | |
| 120 | void *realloc(void *ptr, size_t size) { |
| 121 | if (should_fail_allocation()) { |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | return __libc_realloc(ptr, size); |
| 126 | } |
| 127 | |
| 128 | } // extern "C" |
| 129 | |
Adam Langley | 3e65265 | 2015-01-09 15:44:37 -0800 | [diff] [blame^] | 130 | #endif /* defined(linux) && !ARM && !AARCH64 */ |