blob: 898f2a7c955ab32dbb5ec955234c1bfa25ac52cb [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -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
15#include <openssl/base.h>
16
Adam Langleye9ada862015-05-11 17:20:37 -070017#if defined(__has_feature)
Adam Langleyf4e42722015-06-04 17:45:09 -070018#if __has_feature(address_sanitizer) || __has_feature(memory_sanitizer)
Adam Langleye9ada862015-05-11 17:20:37 -070019#define OPENSSL_ASAN
20#endif
21#endif
22
Adam Langleyf4e42722015-06-04 17:45:09 -070023#if defined(__GLIBC__) && !defined(__UCLIBC__)
24#define OPENSSL_GLIBC
25#endif
26
Adam Langleyd9e397b2015-01-22 14:27:53 -080027// This file isn't built on ARM or Aarch64 because we link statically in those
Adam Langleyf4e42722015-06-04 17:45:09 -070028// 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.
Adam Langleye9ada862015-05-11 17:20:37 -070031//
Adam Langleyf4e42722015-06-04 17:45:09 -070032// TODO(davidben): See if this and ASan's and MSan's interceptors can be made to
33// coexist.
34#if defined(__linux__) && defined(OPENSSL_GLIBC) && !defined(OPENSSL_ARM) && \
Adam Langleye9ada862015-05-11 17:20:37 -070035 !defined(OPENSSL_AARCH64) && !defined(OPENSSL_ASAN)
Adam Langleyd9e397b2015-01-22 14:27:53 -080036
Kenny Rootb8494592015-09-25 02:29:14 +000037#include <errno.h>
38#include <signal.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080039#include <stdint.h>
Adam Langleye9ada862015-05-11 17:20:37 -070040#include <stdio.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080041#include <stdlib.h>
42#include <unistd.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080043
44#include <new>
45
46
47/* 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
Kenny Rootb8494592015-09-25 02:29:14 +000050 * return NULL. If MALLOC_BREAK_ON_FAIL is also defined then the allocation
51 * will signal SIGTRAP rather than return NULL.
Adam Langleyd9e397b2015-01-22 14:27:53 -080052 *
53 * This code is not thread safe. */
54
55static uint64_t current_malloc_count = 0;
56static uint64_t malloc_number_to_fail = 0;
Kenny Rootb8494592015-09-25 02:29:14 +000057static char failure_enabled = 0, break_on_fail = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -080058static int in_call = 0;
59
60extern "C" {
61/* These are other names for the standard allocation functions. */
62extern void *__libc_malloc(size_t size);
63extern void *__libc_calloc(size_t num_elems, size_t size);
64extern void *__libc_realloc(void *ptr, size_t size);
65}
66
67static void exit_handler(void) {
68 if (failure_enabled && current_malloc_count > malloc_number_to_fail) {
69 _exit(88);
70 }
71}
72
73static void cpp_new_handler() {
74 // Return to try again. It won't fail a second time.
75 return;
76}
77
78/* should_fail_allocation returns true if the current allocation should fail. */
79static int should_fail_allocation() {
80 static int init = 0;
81 char should_fail;
82
83 if (in_call) {
84 return 0;
85 }
86
87 in_call = 1;
88
89 if (!init) {
90 const char *env = getenv("MALLOC_NUMBER_TO_FAIL");
91 if (env != NULL && env[0] != 0) {
92 char *endptr;
93 malloc_number_to_fail = strtoull(env, &endptr, 10);
94 if (*endptr == 0) {
95 failure_enabled = 1;
96 atexit(exit_handler);
97 std::set_new_handler(cpp_new_handler);
98 }
99 }
Kenny Rootb8494592015-09-25 02:29:14 +0000100 break_on_fail = (NULL != getenv("MALLOC_BREAK_ON_FAIL"));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800101 init = 1;
102 }
103
104 in_call = 0;
105
106 if (!failure_enabled) {
107 return 0;
108 }
109
110 should_fail = (current_malloc_count == malloc_number_to_fail);
111 current_malloc_count++;
112
Kenny Rootb8494592015-09-25 02:29:14 +0000113 if (should_fail && break_on_fail) {
114 raise(SIGTRAP);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800115 }
116 return should_fail;
117}
118
119extern "C" {
120
121void *malloc(size_t size) {
122 if (should_fail_allocation()) {
Kenny Rootb8494592015-09-25 02:29:14 +0000123 errno = ENOMEM;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800124 return NULL;
125 }
126
127 return __libc_malloc(size);
128}
129
130void *calloc(size_t num_elems, size_t size) {
131 if (should_fail_allocation()) {
Kenny Rootb8494592015-09-25 02:29:14 +0000132 errno = ENOMEM;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800133 return NULL;
134 }
135
136 return __libc_calloc(num_elems, size);
137}
138
139void *realloc(void *ptr, size_t size) {
140 if (should_fail_allocation()) {
Kenny Rootb8494592015-09-25 02:29:14 +0000141 errno = ENOMEM;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800142 return NULL;
143 }
144
145 return __libc_realloc(ptr, size);
146}
147
148} // extern "C"
149
Adam Langleyf4e42722015-06-04 17:45:09 -0700150#endif /* defined(linux) && GLIBC && !ARM && !AARCH64 && !ASAN */