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