blob: 31da2c78571fbbd4f7f20f76ce6a5a13582f35e8 [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)
David Benjaminbe557902015-04-01 14:40:31 -040019#define OPENSSL_ASAN
20#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
29// requires glibc. It's also disabled on ASan builds as this interferes with
30// ASan's malloc interceptor.
David Benjaminbe557902015-04-01 14:40:31 -040031//
David Benjamin81091d52015-05-15 15:50:22 -040032// 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) && \
David Benjaminbe557902015-04-01 14:40:31 -040035 !defined(OPENSSL_AARCH64) && !defined(OPENSSL_ASAN)
Adam Langley69a01602014-11-17 17:26:55 -080036
David Benjamin2b2b0f92015-06-12 18:08:51 -040037#include <errno.h>
Adam Langley69a01602014-11-17 17:26:55 -080038#include <stdint.h>
David Benjamin90fa69a2015-03-19 19:20:19 -040039#include <stdio.h>
Adam Langley69a01602014-11-17 17:26:55 -080040#include <stdlib.h>
41#include <unistd.h>
Adam Langley69a01602014-11-17 17:26:55 -080042
43#include <new>
44
45
46/* This file defines overrides for the standard allocation functions that allow
47 * a given allocation to be made to fail for testing. If the program is run
48 * with MALLOC_NUMBER_TO_FAIL set to a base-10 number then that allocation will
49 * return NULL. If MALLOC_ABORT_ON_FAIL is also defined then the allocation
50 * will abort() rather than return NULL.
51 *
52 * This code is not thread safe. */
53
54static uint64_t current_malloc_count = 0;
55static uint64_t malloc_number_to_fail = 0;
56static char failure_enabled = 0, abort_on_fail = 0;
57static int in_call = 0;
58
59extern "C" {
60/* These are other names for the standard allocation functions. */
61extern 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);
64}
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
77/* should_fail_allocation returns true if the current allocation should fail. */
78static int should_fail_allocation() {
79 static int init = 0;
80 char should_fail;
81
82 if (in_call) {
83 return 0;
84 }
85
86 in_call = 1;
87
88 if (!init) {
89 const char *env = getenv("MALLOC_NUMBER_TO_FAIL");
90 if (env != NULL && env[0] != 0) {
91 char *endptr;
92 malloc_number_to_fail = strtoull(env, &endptr, 10);
93 if (*endptr == 0) {
94 failure_enabled = 1;
95 atexit(exit_handler);
96 std::set_new_handler(cpp_new_handler);
97 }
98 }
99 abort_on_fail = (NULL != getenv("MALLOC_ABORT_ON_FAIL"));
100 init = 1;
101 }
102
103 in_call = 0;
104
105 if (!failure_enabled) {
106 return 0;
107 }
108
109 should_fail = (current_malloc_count == malloc_number_to_fail);
110 current_malloc_count++;
111
112 if (should_fail && abort_on_fail) {
113 abort();
114 }
115 return should_fail;
116}
117
118extern "C" {
119
120void *malloc(size_t size) {
121 if (should_fail_allocation()) {
David Benjamin2b2b0f92015-06-12 18:08:51 -0400122 errno = ENOMEM;
Adam Langley69a01602014-11-17 17:26:55 -0800123 return NULL;
124 }
125
126 return __libc_malloc(size);
127}
128
129void *calloc(size_t num_elems, size_t size) {
130 if (should_fail_allocation()) {
David Benjamin2b2b0f92015-06-12 18:08:51 -0400131 errno = ENOMEM;
Adam Langley69a01602014-11-17 17:26:55 -0800132 return NULL;
133 }
134
135 return __libc_calloc(num_elems, size);
136}
137
138void *realloc(void *ptr, size_t size) {
139 if (should_fail_allocation()) {
David Benjamin2b2b0f92015-06-12 18:08:51 -0400140 errno = ENOMEM;
Adam Langley69a01602014-11-17 17:26:55 -0800141 return NULL;
142 }
143
144 return __libc_realloc(ptr, size);
145}
146
147} // extern "C"
148
David Benjamin8a228f52015-06-01 13:41:57 -0400149#endif /* defined(linux) && GLIBC && !ARM && !AARCH64 && !ASAN */