blob: 5f0bc6e2eadab2aea97ddaa31735e3f266602b8f [file] [log] [blame]
Adam Langley69a01602014-11-17 17:26:55 -08001/* 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 Langley3e652652015-01-09 15:44:37 -080015#include <openssl/base.h>
16
David Benjamin8a228f52015-06-01 13:41:57 -040017#if defined(__GLIBC__) && !defined(__UCLIBC__)
18#define OPENSSL_GLIBC
19#endif
20
Adam Langley3e652652015-01-09 15:44:37 -080021// This file isn't built on ARM or Aarch64 because we link statically in those
David Benjamin8a228f52015-06-01 13:41:57 -040022// builds and trying to override malloc in a static link doesn't work. It also
David Benjamin52db0eb2016-10-14 11:20:07 -040023// requires glibc. It's also disabled on ASan builds as this interferes with
24// ASan's malloc interceptor.
25//
26// TODO(davidben): See if this and ASan's and MSan's interceptors can be made to
27// coexist.
David Benjamin8a228f52015-06-01 13:41:57 -040028#if defined(__linux__) && defined(OPENSSL_GLIBC) && !defined(OPENSSL_ARM) && \
Adam Langleyf64a6ee2017-05-17 13:05:50 -070029 !defined(OPENSSL_AARCH64) && !defined(OPENSSL_ASAN) && \
30 !defined(OPENSSL_MSAN)
Adam Langley69a01602014-11-17 17:26:55 -080031
David Benjamin2b2b0f92015-06-12 18:08:51 -040032#include <errno.h>
David Benjamin184494d2015-06-12 18:23:47 -040033#include <signal.h>
Adam Langley69a01602014-11-17 17:26:55 -080034#include <stdint.h>
David Benjamin90fa69a2015-03-19 19:20:19 -040035#include <stdio.h>
Adam Langley69a01602014-11-17 17:26:55 -080036#include <stdlib.h>
37#include <unistd.h>
Adam Langley69a01602014-11-17 17:26:55 -080038
39#include <new>
40
41
David Benjamincef36f02016-10-14 10:49:48 -040042// This file defines overrides for the standard allocation functions that allow
43// a given allocation to be made to fail for testing. If the program is run
44// with MALLOC_NUMBER_TO_FAIL set to a base-10 number then that allocation will
45// return NULL. If MALLOC_BREAK_ON_FAIL is also defined then the allocation
46// will signal SIGTRAP rather than return NULL.
47//
48// This code is not thread safe.
Adam Langley69a01602014-11-17 17:26:55 -080049
50static uint64_t current_malloc_count = 0;
51static uint64_t malloc_number_to_fail = 0;
David Benjamincef36f02016-10-14 10:49:48 -040052static bool failure_enabled = false, break_on_fail = false, in_call = false;
Adam Langley69a01602014-11-17 17:26:55 -080053
54extern "C" {
David Benjamincef36f02016-10-14 10:49:48 -040055// These are other names for the standard allocation functions.
David Benjamin52db0eb2016-10-14 11:20:07 -040056extern void *__libc_malloc(size_t size);
57extern void *__libc_calloc(size_t num_elems, size_t size);
58extern void *__libc_realloc(void *ptr, size_t size);
Adam Langley69a01602014-11-17 17:26:55 -080059}
60
61static void exit_handler(void) {
62 if (failure_enabled && current_malloc_count > malloc_number_to_fail) {
63 _exit(88);
64 }
65}
66
67static void cpp_new_handler() {
68 // Return to try again. It won't fail a second time.
69 return;
70}
71
David Benjamincef36f02016-10-14 10:49:48 -040072// should_fail_allocation returns true if the current allocation should fail.
73static bool should_fail_allocation() {
74 static bool init = false;
Adam Langley69a01602014-11-17 17:26:55 -080075
76 if (in_call) {
David Benjamincef36f02016-10-14 10:49:48 -040077 return false;
Adam Langley69a01602014-11-17 17:26:55 -080078 }
79
David Benjamincef36f02016-10-14 10:49:48 -040080 in_call = true;
Adam Langley69a01602014-11-17 17:26:55 -080081
82 if (!init) {
83 const char *env = getenv("MALLOC_NUMBER_TO_FAIL");
84 if (env != NULL && env[0] != 0) {
85 char *endptr;
86 malloc_number_to_fail = strtoull(env, &endptr, 10);
87 if (*endptr == 0) {
David Benjamincef36f02016-10-14 10:49:48 -040088 failure_enabled = true;
Adam Langley69a01602014-11-17 17:26:55 -080089 atexit(exit_handler);
90 std::set_new_handler(cpp_new_handler);
91 }
92 }
David Benjamin184494d2015-06-12 18:23:47 -040093 break_on_fail = (NULL != getenv("MALLOC_BREAK_ON_FAIL"));
David Benjamincef36f02016-10-14 10:49:48 -040094 init = true;
Adam Langley69a01602014-11-17 17:26:55 -080095 }
96
David Benjamincef36f02016-10-14 10:49:48 -040097 in_call = false;
Adam Langley69a01602014-11-17 17:26:55 -080098
99 if (!failure_enabled) {
David Benjamincef36f02016-10-14 10:49:48 -0400100 return false;
Adam Langley69a01602014-11-17 17:26:55 -0800101 }
102
David Benjamincef36f02016-10-14 10:49:48 -0400103 bool should_fail = (current_malloc_count == malloc_number_to_fail);
Adam Langley69a01602014-11-17 17:26:55 -0800104 current_malloc_count++;
105
David Benjamin184494d2015-06-12 18:23:47 -0400106 if (should_fail && break_on_fail) {
107 raise(SIGTRAP);
Adam Langley69a01602014-11-17 17:26:55 -0800108 }
109 return should_fail;
110}
111
112extern "C" {
113
114void *malloc(size_t size) {
115 if (should_fail_allocation()) {
David Benjamin2b2b0f92015-06-12 18:08:51 -0400116 errno = ENOMEM;
Adam Langley69a01602014-11-17 17:26:55 -0800117 return NULL;
118 }
119
David Benjamin52db0eb2016-10-14 11:20:07 -0400120 return __libc_malloc(size);
Adam Langley69a01602014-11-17 17:26:55 -0800121}
122
123void *calloc(size_t num_elems, size_t size) {
124 if (should_fail_allocation()) {
David Benjamin2b2b0f92015-06-12 18:08:51 -0400125 errno = ENOMEM;
Adam Langley69a01602014-11-17 17:26:55 -0800126 return NULL;
127 }
128
David Benjamin52db0eb2016-10-14 11:20:07 -0400129 return __libc_calloc(num_elems, size);
Adam Langley69a01602014-11-17 17:26:55 -0800130}
131
132void *realloc(void *ptr, size_t size) {
133 if (should_fail_allocation()) {
David Benjamin2b2b0f92015-06-12 18:08:51 -0400134 errno = ENOMEM;
Adam Langley69a01602014-11-17 17:26:55 -0800135 return NULL;
136 }
137
David Benjamin52db0eb2016-10-14 11:20:07 -0400138 return __libc_realloc(ptr, size);
Adam Langley69a01602014-11-17 17:26:55 -0800139}
140
141} // extern "C"
142
David Benjamin808f8322017-08-18 14:06:02 -0400143#endif // defined(linux) && GLIBC && !ARM && !AARCH64 && !ASAN