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