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