source: zzuf/trunk/src/lib-mem.c @ 1714

Last change on this file since 1714 was 1714, checked in by Sam Hocevar, 13 years ago
  • Crap! Fixed memory manager initialisation. And sticking that into the 0.6.1 tarball, no one will notice! (ha ha ha)
  • Property svn:keywords set to Id
File size: 9.2 KB
Line 
1/*
2 *  zzuf - general purpose fuzzer
3 *  Copyright (c) 2006,2007 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: lib-mem.c 1714 2007-01-25 19:55:20Z sam $
7 *
8 *  This program is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 *  load-mem.c: loaded memory handling functions
17 */
18
19#include "config.h"
20
21/* Need this for RTLD_NEXT */
22#define _GNU_SOURCE
23/* Use this to get mmap64() on glibc systems */
24#define _LARGEFILE64_SOURCE
25/* Use this to get posix_memalign */
26#if defined HAVE_POSIX_MEMALIGN
27#   define _XOPEN_SOURCE 600
28#endif
29
30#if defined HAVE_STDINT_H
31#   include <stdint.h>
32#elif defined HAVE_INTTYPES_H
33#   include <inttypes.h>
34#endif
35#include <stdlib.h>
36#include <string.h>
37#include <errno.h>
38#include <signal.h>
39
40#if defined HAVE_MALLOC_H
41#   include <malloc.h>
42#endif
43#include <unistd.h>
44#if defined HAVE_SYS_MMAN_H
45#   include <sys/mman.h>
46#endif
47#if defined HAVE_LIBC_H
48#   include <libc.h>
49#endif
50
51#include "libzzuf.h"
52#include "lib-load.h"
53#include "debug.h"
54#include "fuzz.h"
55#include "fd.h"
56
57#if !defined SIGKILL
58#   define SIGKILL 9
59#endif
60
61/* TODO: mremap, maybe brk/sbrk (haha) */
62
63/* Library functions that we divert */
64static void *  (*ORIG(calloc))   (size_t nmemb, size_t size);
65static void *  (*ORIG(malloc))   (size_t size);
66static void    (*ORIG(free))     (void *ptr);
67#if defined HAVE_VALLOC
68static void *  (*ORIG(valloc))   (size_t size);
69#endif
70#if defined HAVE_MEMALIGN
71static void *  (*ORIG(memalign)) (size_t boundary, size_t size);
72#endif
73#if defined HAVE_POSIX_MEMALIGN
74static int     (*ORIG(posix_memalign)) (void **memptr, size_t alignment,
75                                        size_t size);
76#endif
77static void *  (*ORIG(realloc))  (void *ptr, size_t size);
78
79#if defined HAVE_MMAP
80static void *  (*ORIG(mmap))     (void *start, size_t length, int prot,
81                                  int flags, int fd, off_t offset);
82#endif
83#if defined HAVE_MMAP64
84static void *  (*ORIG(mmap64))   (void *start, size_t length, int prot,
85                                  int flags, int fd, off64_t offset);
86#endif
87#if defined HAVE_MUNMAP
88static int     (*ORIG(munmap))   (void *start, size_t length);
89#endif
90#if defined HAVE_MAP_FD
91static kern_return_t (*ORIG(map_fd)) (int fd, vm_offset_t offset,
92                                      vm_offset_t *addr, boolean_t find_space,
93                                      vm_size_t numbytes);
94#endif
95
96/* We need a static memory buffer because some functions call memory
97 * allocation routines before our library is loaded. Hell, even dlsym()
98 * calls calloc(), so we need to do something about it */
99#define DUMMY_BYTES 655360 /* 640 kB ought to be enough for anybody */
100static uint64_t dummy_buffer[DUMMY_BYTES / 8];
101static int dummy_offset = 0;
102#define DUMMY_START ((uintptr_t)dummy_buffer)
103#define DUMMY_STOP ((uintptr_t)dummy_buffer + DUMMY_BYTES)
104
105void _zz_mem_init(void)
106{
107    LOADSYM(calloc);
108    LOADSYM(malloc);
109    LOADSYM(realloc);
110}
111
112void *NEW(calloc)(size_t nmemb, size_t size)
113{
114    void *ret;
115    if(!ORIG(calloc))
116    {
117        ret = dummy_buffer + dummy_offset;
118        memset(ret, 0, (nmemb * size + 7) / 8);
119        dummy_offset += (nmemb * size + 7) / 8;
120        return ret;
121    }
122    ret = ORIG(calloc)(nmemb, size);
123    if(ret == NULL && _zz_memory && errno == ENOMEM)
124        raise(SIGKILL);
125    return ret;
126}
127
128void *NEW(malloc)(size_t size)
129{
130    void *ret;
131    if(!ORIG(malloc))
132    {
133        ret = dummy_buffer + dummy_offset;
134        dummy_offset += (size + 7) / 8;
135        return ret;
136    }
137    ret = ORIG(malloc)(size);
138    if(ret == NULL && _zz_memory && errno == ENOMEM)
139        raise(SIGKILL);
140    return ret;
141}
142
143void NEW(free)(void *ptr)
144{
145    if((uintptr_t)ptr >= DUMMY_START && (uintptr_t)ptr < DUMMY_STOP)
146        return;
147    LOADSYM(free);
148    ORIG(free)(ptr);
149}
150
151void *NEW(realloc)(void *ptr, size_t size)
152{
153    void *ret;
154    if(!ORIG(realloc)
155        || ((uintptr_t)ptr >= DUMMY_START && (uintptr_t)ptr < DUMMY_STOP))
156    {
157        ret = dummy_buffer + dummy_offset;
158        memcpy(ret, ptr, size);
159        dummy_offset += (size + 7) * 8;
160        return ret;
161    }
162    LOADSYM(realloc);
163    ret = ORIG(realloc)(ptr, size);
164    if(ret == NULL && _zz_memory && errno == ENOMEM)
165        raise(SIGKILL);
166    return ret;
167}
168
169#if defined HAVE_VALLOC
170void *NEW(valloc)(size_t size)
171{
172    void *ret;
173    LOADSYM(valloc);
174    ret = ORIG(valloc)(size);
175    if(ret == NULL && _zz_memory && errno == ENOMEM)
176        raise(SIGKILL);
177    return ret;
178}
179#endif
180
181#if defined HAVE_MEMALIGN
182void *NEW(memalign)(size_t boundary, size_t size)
183{
184    void *ret;
185    LOADSYM(memalign);
186    ret = ORIG(memalign)(boundary, size);
187    if(ret == NULL && _zz_memory && errno == ENOMEM)
188        raise(SIGKILL);
189    return ret;
190}
191#endif
192
193#if defined HAVE_POSIX_MEMALIGN
194int NEW(posix_memalign)(void **memptr, size_t alignment, size_t size)
195{
196    int ret;
197    LOADSYM(posix_memalign);
198    ret = ORIG(posix_memalign)(memptr, alignment, size);
199    if(ret == ENOMEM && _zz_memory)
200        raise(SIGKILL);
201    return ret;
202}
203#endif
204
205/* Table used for mmap() and munmap() */
206void **maps = NULL;
207int nbmaps = 0;
208
209#define MMAP(fn, off_t) \
210    do { \
211        LOADSYM(fn); \
212        ret = ORIG(fn)(start, length, prot, flags, fd, offset); \
213        if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd)) \
214            return ret; \
215        if(ret && length) \
216        { \
217            char *b = malloc(length); \
218            int i, oldpos; \
219            for(i = 0; i < nbmaps; i += 2) \
220                if(maps[i] == NULL) \
221                    break; \
222            if(i == nbmaps) \
223            { \
224                nbmaps += 2; \
225                maps = realloc(maps, nbmaps * sizeof(void *)); \
226            } \
227            maps[i] = b; \
228            maps[i + 1] = ret; \
229            oldpos = _zz_getpos(fd); \
230            _zz_setpos(fd, offset); /* mmap() maps the fd at offset 0 */ \
231            memcpy(b, ret, length); /* FIXME: get rid of this */ \
232            _zz_fuzz(fd, (uint8_t *)b, length); \
233            _zz_setpos(fd, oldpos); \
234            ret = b; \
235            if(length >= 4) \
236                debug("%s(%p, %li, %i, %i, %i, %lli) = %p \"%c%c%c%c...", \
237                      __func__, start, (long int)length, prot, flags, fd, \
238                      (long long int)offset, ret, b[0], b[1], b[2], b[3]); \
239            else \
240                debug("%s(%p, %li, %i, %i, %i, %lli) = %p \"%c...", \
241                      __func__, start, (long int)length, prot, flags, fd, \
242                      (long long int)offset, ret, b[0]); \
243        } \
244        else \
245            debug("%s(%p, %li, %i, %i, %i, %lli) = %p", \
246                  __func__, start, (long int)length, prot, flags, fd, \
247                  (long long int)offset, ret); \
248    } while(0)
249
250#if defined HAVE_MMAP
251void *NEW(mmap)(void *start, size_t length, int prot, int flags,
252                int fd, off_t offset)
253{
254    void *ret; MMAP(mmap, off_t); return ret;
255}
256#endif
257
258#if defined HAVE_MMAP64
259void *NEW(mmap64)(void *start, size_t length, int prot, int flags,
260                  int fd, off64_t offset)
261{
262    void *ret; MMAP(mmap64, off64_t); return ret;
263}
264#endif
265
266#if defined HAVE_MUNMAP
267int NEW(munmap)(void *start, size_t length)
268{
269    int ret, i;
270
271    LOADSYM(munmap);
272    for(i = 0; i < nbmaps; i++)
273    {
274        if(maps[i] != start)
275            continue;
276
277        free(start);
278        ret = ORIG(munmap)(maps[i + 1], length);
279        maps[i] = NULL;
280        maps[i + 1] = NULL;
281        debug("%s(%p, %li) = %i", __func__, start, (long int)length, ret);
282        return ret;
283    }
284
285    return ORIG(munmap)(start, length);
286}
287#endif
288
289#if defined HAVE_MAP_FD
290kern_return_t NEW(map_fd)(int fd, vm_offset_t offset, vm_offset_t *addr,
291                          boolean_t find_space, vm_size_t numbytes)
292{
293    kern_return_t ret;
294
295    LOADSYM(map_fd);
296    ret = ORIG(map_fd)(fd, offset, addr, find_space, numbytes);
297    if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd))
298        return ret;
299
300    if(ret == 0 && numbytes)
301    {
302        /* FIXME: do we also have to rewind the filedescriptor like in mmap? */
303        char *b = malloc(numbytes);
304        memcpy(b, (void *)*addr, numbytes);
305        _zz_fuzz(fd, (void *)b, numbytes);
306        *addr = (vm_offset_t)b;
307        /* FIXME: the map is never freed; there is no such thing as unmap_fd,
308         * but I suppose that kind of map should go when the filedescriptor is
309         * closed (unlike mmap, which returns a persistent buffer). */
310
311        if(numbytes >= 4)
312           debug("%s(%i, %lli, &%p, %i, %lli) = %i \"%c%c%c%c", __func__,
313                 fd, (long long int)offset, (void *)*addr, (int)find_space,
314                 (long long int)numbytes, ret, b[0], b[1], b[2], b[3]);
315        else
316           debug("%s(%i, %lli, &%p, %i, %lli) = %i \"%c", __func__, fd,
317                 (long long int)offset, (void *)*addr, (int)find_space,
318                 (long long int)numbytes, ret, b[0]);
319    }
320    else
321        debug("%s(%i, %lli, &%p, %i, %lli) = %i", __func__, fd,
322              (long long int)offset, (void *)*addr, (int)find_space,
323              (long long int)numbytes, ret);
324
325    return ret;
326}
327#endif
328
Note: See TracBrowser for help on using the repository browser.