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