Imported Upstream version 1.1.3
[platform/upstream/libzip.git] / regress / malloc.c
1 /*
2   malloc.c -- override *alloc() to allow testing special cases
3   Copyright (C) 2015 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 {
58 #if defined(HAVE___PROGNAME)
59     return __progname;
60 #else
61     return NULL;
62 #endif
63 }
64 #endif
65
66 static int inited = 0;
67 static size_t count = 0;
68 static size_t max_count = 0;
69 static size_t min_size = 0;
70 static void *(*real_malloc)(size_t size) = NULL;
71 static void *(*real_calloc)(size_t number, size_t size) = NULL;
72 static void *(*real_realloc)(void *ptr, size_t size) = NULL;
73
74 static const char *myname = NULL;
75
76 /* TODO: add sentinel -- check if particular size is malloced before doing other stuff */
77 /* TODO: catch free for sentinel too */
78 /* TODO: optionally, catch malloc of particular size */
79
80 static void
81 init(void)
82 {
83     char *foo;
84     myname = getprogname();
85     if (!myname)
86         myname = "(unknown)";
87     if ((foo=getenv("MALLOC_MAX_COUNT")) != NULL)
88         max_count = strtoul(foo, NULL, 0);
89     if ((foo=getenv("MALLOC_MIN_SIZE")) != NULL)
90         min_size = strtoul(foo, NULL, 0);
91     real_calloc = dlsym(RTLD_NEXT, "calloc");
92     if (!real_calloc)
93         abort();
94     real_malloc = dlsym(RTLD_NEXT, "malloc");
95     if (!real_malloc)
96         abort();
97     real_realloc = dlsym(RTLD_NEXT, "realloc");
98     if (!real_realloc)
99         abort();
100     inited = 1;
101 }
102
103 void *
104 calloc(size_t number, size_t size)
105 {
106     void *ret;
107
108     if (!inited) {
109         init();
110     }
111
112     if (number >= min_size/size && count >= max_count) {
113         errno = ENOMEM;
114         return NULL;
115     }
116
117     ret = real_calloc(number, size);
118     if (size >= min_size) {
119         count++;
120     }
121
122     return ret;
123 }
124
125 void *
126 malloc(size_t size)
127 {
128     void *ret;
129
130     if (!inited) {
131         init();
132     }
133
134     if (size >= min_size && count >= max_count) {
135         errno = ENOMEM;
136         return NULL;
137     }
138
139     ret = real_malloc(size);
140     if (size >= min_size) {
141         count++;
142     }
143
144     return ret;
145 }
146
147 void *
148 realloc(void *ptr, size_t size)
149 {
150     void *ret;
151
152     if (!inited) {
153         init();
154     }
155
156     if (size >= min_size && count >= max_count) {
157         errno = ENOMEM;
158         return NULL;
159     }
160
161     ret = real_realloc(ptr, size);
162     if (size >= min_size) {
163         count++;
164     }
165
166     return ret;
167 }