1/* instrumented_alloc.c -- Memory allocation instrumented to fail when 2 requested, for testing purposes. 3 Copyright (C) 2018-2021 Free Software Foundation, Inc. 4 5Redistribution and use in source and binary forms, with or without 6modification, are permitted provided that the following conditions are 7met: 8 9 (1) Redistributions of source code must retain the above copyright 10 notice, this list of conditions and the following disclaimer. 11 12 (2) Redistributions in binary form must reproduce the above copyright 13 notice, this list of conditions and the following disclaimer in 14 the documentation and/or other materials provided with the 15 distribution. 16 17 (3) The name of the author may not be used to 18 endorse or promote products derived from this software without 19 specific prior written permission. 20 21THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 25INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 26(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 27SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 29STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 30IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31POSSIBILITY OF SUCH DAMAGE. */ 32 33/* Include all the header files of alloc here, to make sure they're not 34 processed when including alloc.c below, such that the redefinitions of malloc 35 and realloc are only effective in alloc.c itself. This does not work for 36 config.h, because it's not wrapped in "#ifndef CONFIG_H\n#define CONFIG_H" 37 and "#endif" but that does not seem to be harmful. */ 38 39#include "config.h" 40 41#include <errno.h> 42#include <stdlib.h> 43#include <sys/types.h> 44#include <inttypes.h> 45 46#include "backtrace.h" 47#include "internal.h" 48 49extern void *instrumented_malloc (size_t size); 50extern void *instrumented_realloc (void *ptr, size_t size); 51 52#define malloc instrumented_malloc 53#define realloc instrumented_realloc 54#include "alloc.c" 55#undef malloc 56#undef realloc 57 58static uint64_t nr_allocs = 0; 59static uint64_t fail_at_alloc = 0; 60 61extern int at_fail_alloc_p (void); 62extern uint64_t get_nr_allocs (void); 63extern void set_fail_at_alloc (uint64_t); 64 65void * 66instrumented_malloc (size_t size) 67{ 68 void *res; 69 70 if (at_fail_alloc_p ()) 71 return NULL; 72 73 res = malloc (size); 74 if (res != NULL) 75 nr_allocs++; 76 77 return res; 78} 79 80void * 81instrumented_realloc (void *ptr, size_t size) 82{ 83 void *res; 84 85 if (size != 0) 86 { 87 if (at_fail_alloc_p ()) 88 return NULL; 89 } 90 91 res = realloc (ptr, size); 92 if (res != NULL) 93 nr_allocs++; 94 95 return res; 96} 97 98int 99at_fail_alloc_p (void) 100{ 101 return fail_at_alloc == nr_allocs + 1; 102} 103 104uint64_t 105get_nr_allocs (void) 106{ 107 return nr_allocs; 108} 109 110void 111set_fail_at_alloc (uint64_t nr) 112{ 113 fail_at_alloc = nr; 114} 115