blob: bb1a6a645111317ac397037638def75ffc876353 [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 Benjaminbe557902015-04-01 14:40:31 -040017#if defined(__has_feature)
David Benjamin81091d52015-05-15 15:50:22 -040018#if __has_feature(address_sanitizer) || __has_feature(memory_sanitizer)
Adam Langley208e2392017-05-08 12:53:15 -070019#define OPENSSL_ASAN_OR_MSAN
David Benjaminbe557902015-04-01 14:40:31 -040020#endif
21#endif
22
David Benjamin8a228f52015-06-01 13:41:57 -040023#if defined(__GLIBC__) && !defined(__UCLIBC__)
24#define OPENSSL_GLIBC
25#endif
26
Adam Langley3e652652015-01-09 15:44:37 -080027// This file isn't built on ARM or Aarch64 because we link statically in those
David Benjamin8a228f52015-06-01 13:41:57 -040028// builds and trying to override malloc in a static link doesn't work. It also
David Benjamin52db0eb2016-10-14 11:20:07 -040029// requires glibc. It's also disabled on ASan builds as this interferes with
30// ASan's malloc interceptor.
31//
32// TODO(davidben): See if this and ASan's and MSan's interceptors can be made to
33// coexist.
David Benjamin8a228f52015-06-01 13:41:57 -040034#if defined(__linux__) && defined(OPENSSL_GLIBC) && !defined(OPENSSL_ARM) && \
Adam Langley208e2392017-05-08 12:53:15 -070035 !defined(OPENSSL_AARCH64) && !defined(OPENSSL_ASAN_OR_MSAN)
Adam Langley69a01602014-11-17 17:26:55 -080036
David Benjamin2b2b0f92015-06-12 18:08:51 -040037#include <errno.h>
David Benjamin184494d2015-06-12 18:23:47 -040038#include <signal.h>
Adam Langley69a01602014-11-17 17:26:55 -080039#include <stdint.h>
David Benjamin90fa69a2015-03-19 19:20:19 -040040#include <stdio.h>
Adam Langley69a01602014-11-17 17:26:55 -080041#include <stdlib.h>
42#include <unistd.h>
Adam Langley69a01602014-11-17 17:26:55 -080043
44#include <new>
45
46
David Benjamincef36f02016-10-14 10:49:48 -040047// This file defines overrides for the standard allocation functions that allow
48// a given allocation to be made to fail for testing. If the program is run
49// with MALLOC_NUMBER_TO_FAIL set to a base-10 number then that allocation will
50// return NULL. If MALLOC_BREAK_ON_FAIL is also defined then the allocation
51// will signal SIGTRAP rather than return NULL.
52//
53// This code is not thread safe.
Adam Langley69a01602014-11-17 17:26:55 -080054
55static uint64_t current_malloc_count = 0;
56static uint64_t malloc_number_to_fail = 0;
David Benjamincef36f02016-10-14 10:49:48 -040057static bool failure_enabled = false, break_on_fail = false, in_call = false;
Adam Langley69a01602014-11-17 17:26:55 -080058
59extern "C" {
David Benjamincef36f02016-10-14 10:49:48 -040060// These are other names for the standard allocation functions.
David Benjamin52db0eb2016-10-14 11:20:07 -040061extern void *__libc_malloc(size_t size);
62extern void *__libc_calloc(size_t num_elems, size_t size);
63extern void *__libc_realloc(void *ptr, size_t size);
Adam Langley69a01602014-11-17 17:26:55 -080064}
65
66static void exit_handler(void) {
67 if (failure_enabled && current_malloc_count > malloc_number_to_fail) {
68 _exit(88);
69 }
70}
71
72static void cpp_new_handler() {
73 // Return to try again. It won't fail a second time.
74 return;
75}
76
David Benjamincef36f02016-10-14 10:49:48 -040077// should_fail_allocation returns true if the current allocation should fail.
78static bool should_fail_allocation() {
79 static bool init = false;
Adam Langley69a01602014-11-17 17:26:55 -080080
81 if (in_call) {
David Benjamincef36f02016-10-14 10:49:48 -040082 return false;
Adam Langley69a01602014-11-17 17:26:55 -080083 }
84
David Benjamincef36f02016-10-14 10:49:48 -040085 in_call = true;
Adam Langley69a01602014-11-17 17:26:55 -080086
87 if (!init) {
88 const char *env = getenv("MALLOC_NUMBER_TO_FAIL");
89 if (env != NULL && env[0] != 0) {
90 char *endptr;
91 malloc_number_to_fail = strtoull(env, &endptr, 10);
92 if (*endptr == 0) {
David Benjamincef36f02016-10-14 10:49:48 -040093 failure_enabled = true;
Adam Langley69a01602014-11-17 17:26:55 -080094 atexit(exit_handler);
95 std::set_new_handler(cpp_new_handler);
96 }
97 }
David Benjamin184494d2015-06-12 18:23:47 -040098 break_on_fail = (NULL != getenv("MALLOC_BREAK_ON_FAIL"));
David Benjamincef36f02016-10-14 10:49:48 -040099 init = true;
Adam Langley69a01602014-11-17 17:26:55 -0800100 }
101
David Benjamincef36f02016-10-14 10:49:48 -0400102 in_call = false;
Adam Langley69a01602014-11-17 17:26:55 -0800103
104 if (!failure_enabled) {
David Benjamincef36f02016-10-14 10:49:48 -0400105 return false;
Adam Langley69a01602014-11-17 17:26:55 -0800106 }
107
David Benjamincef36f02016-10-14 10:49:48 -0400108 bool should_fail = (current_malloc_count == malloc_number_to_fail);
Adam Langley69a01602014-11-17 17:26:55 -0800109 current_malloc_count++;
110
David Benjamin184494d2015-06-12 18:23:47 -0400111 if (should_fail && break_on_fail) {
112 raise(SIGTRAP);
Adam Langley69a01602014-11-17 17:26:55 -0800113 }
114 return should_fail;
115}
116
117extern "C" {
118
119void *malloc(size_t size) {
120 if (should_fail_allocation()) {
David Benjamin2b2b0f92015-06-12 18:08:51 -0400121 errno = ENOMEM;
Adam Langley69a01602014-11-17 17:26:55 -0800122 return NULL;
123 }
124
David Benjamin52db0eb2016-10-14 11:20:07 -0400125 return __libc_malloc(size);
Adam Langley69a01602014-11-17 17:26:55 -0800126}
127
128void *calloc(size_t num_elems, size_t size) {
129 if (should_fail_allocation()) {
David Benjamin2b2b0f92015-06-12 18:08:51 -0400130 errno = ENOMEM;
Adam Langley69a01602014-11-17 17:26:55 -0800131 return NULL;
132 }
133
David Benjamin52db0eb2016-10-14 11:20:07 -0400134 return __libc_calloc(num_elems, size);
Adam Langley69a01602014-11-17 17:26:55 -0800135}
136
137void *realloc(void *ptr, size_t size) {
138 if (should_fail_allocation()) {
David Benjamin2b2b0f92015-06-12 18:08:51 -0400139 errno = ENOMEM;
Adam Langley69a01602014-11-17 17:26:55 -0800140 return NULL;
141 }
142
David Benjamin52db0eb2016-10-14 11:20:07 -0400143 return __libc_realloc(ptr, size);
Adam Langley69a01602014-11-17 17:26:55 -0800144}
145
146} // extern "C"
147
David Benjamin8a228f52015-06-01 13:41:57 -0400148#endif /* defined(linux) && GLIBC && !ARM && !AARCH64 && !ASAN */