Rizin
unix-like reverse engineering framework and cli tools
malloc.c
Go to the documentation of this file.
1 /*
2  malloc.c -- override *alloc() to allow testing special cases
3  Copyright (C) 2015-2021 Dieter Baron and Thomas Klausner
4 
5  This file is part of ckmame, a program to check rom sets for MAME.
6  The authors can be contacted at <ckmame@nih.at>
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions
10  are met:
11  1. Redistributions of source code must retain the above copyright
12  notice, this list of conditions and the following disclaimer.
13  2. Redistributions in binary form must reproduce the above copyright
14  notice, this list of conditions and the following disclaimer in
15  the documentation and/or other materials provided with the
16  distribution.
17  3. The name of the author may not be used to endorse or promote
18  products derived from this software without specific prior
19  written permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
22  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 /* #include <string.h> */
37 #include <errno.h>
38 #define __USE_GNU
39 #include <dlfcn.h>
40 #undef __USE_GNU
41 
42 #include "config.h"
43 
44 #if !defined(RTLD_NEXT)
45 #define RTLD_NEXT RTLD_DEFAULT
46 #endif
47 
48 #if defined(HAVE___PROGNAME)
49 extern char *__progname;
50 #endif
51 
52 #if defined(HAVE_GETPROGNAME)
53 /* all fine */
54 #else
55 const char *
56 getprogname(void) {
57 #if defined(HAVE___PROGNAME)
58  return __progname;
59 #else
60  return NULL;
61 #endif
62 }
63 #endif
64 
65 static int inited = 0;
66 static size_t count = 0;
67 static size_t max_count = 0;
68 static size_t min_size = 0;
69 static void *(*real_malloc)(size_t size) = NULL;
70 static void *(*real_calloc)(size_t number, size_t size) = NULL;
71 static void *(*real_realloc)(void *ptr, size_t size) = NULL;
72 
73 static const char *myname = NULL;
74 
75 /* TODO: add sentinel -- check if particular size is malloced before doing other stuff */
76 /* TODO: catch free for sentinel too */
77 /* TODO: optionally, catch malloc of particular size */
78 
79 static void
80 init(void) {
81  char *foo;
82  myname = getprogname();
83  if (!myname)
84  myname = "(unknown)";
85  if ((foo = getenv("MALLOC_MAX_COUNT")) != NULL)
86  max_count = strtoul(foo, NULL, 0);
87  if ((foo = getenv("MALLOC_MIN_SIZE")) != NULL)
88  min_size = strtoul(foo, NULL, 0);
89  real_calloc = dlsym(RTLD_NEXT, "calloc");
90  if (!real_calloc)
91  abort();
92  real_malloc = dlsym(RTLD_NEXT, "malloc");
93  if (!real_malloc)
94  abort();
95  real_realloc = dlsym(RTLD_NEXT, "realloc");
96  if (!real_realloc)
97  abort();
98  inited = 1;
99 }
100 
101 void *
102 calloc(size_t number, size_t size) {
103  void *ret;
104 
105  if (!inited) {
106  init();
107  }
108 
109  if (number >= min_size / size && count >= max_count) {
110  errno = ENOMEM;
111  return NULL;
112  }
113 
114  ret = real_calloc(number, size);
115  if (size >= min_size) {
116  count++;
117  }
118 
119  return ret;
120 }
121 
122 void *
123 malloc(size_t size) {
124  void *ret;
125 
126  if (!inited) {
127  init();
128  }
129 
130  if (size >= min_size && count >= max_count) {
131  errno = ENOMEM;
132  return NULL;
133  }
134 
135  ret = real_malloc(size);
136  if (size >= min_size) {
137  count++;
138  }
139 
140  return ret;
141 }
142 
143 void *
144 realloc(void *ptr, size_t size) {
145  void *ret;
146 
147  if (!inited) {
148  init();
149  }
150 
151  if (size >= min_size && count >= max_count) {
152  errno = ENOMEM;
153  return NULL;
154  }
155 
156  ret = real_realloc(ptr, size);
157  if (size >= min_size) {
158  count++;
159  }
160 
161  return ret;
162 }
#define NULL
Definition: cris-opc.c:27
voidpf void uLong size
Definition: ioapi.h:138
static RzMain foo[]
Definition: main.c:11
static void *(* real_calloc)(size_t number, size_t size)
Definition: malloc.c:70
static void *(* real_malloc)(size_t size)
Definition: malloc.c:69
static void init(void)
Definition: malloc.c:80
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
const char * getprogname(void)
Definition: malloc.c:56
static const char * myname
Definition: malloc.c:73
#define RTLD_NEXT
Definition: malloc.c:45
static size_t count
Definition: malloc.c:66
void * malloc(size_t size)
Definition: malloc.c:123
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
static void *(* real_realloc)(void *ptr, size_t size)
Definition: malloc.c:71
static int inited
Definition: malloc.c:65
static size_t max_count
Definition: malloc.c:67
static size_t min_size
Definition: malloc.c:68
#define ENOMEM
Definition: sftypes.h:122
char * getenv()