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

Last change on this file since 3696 was 3696, checked in by Sam Hocevar, 11 years ago

Undefine _XOPEN_SOURCE before redefining it.

  • Property svn:keywords set to Id
File size: 10.8 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 3696 2009-10-27 00:01:25Z 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/* Need this for MAP_ANON and valloc() on FreeBSD (together with cdefs.h) */
24#define _BSD_SOURCE
25#if defined HAVE_SYS_CDEFS_H
26#   include <sys/cdefs.h>
27#endif
28/* Use this to get mmap64() on glibc systems */
29#undef _LARGEFILE64_SOURCE
30#define _LARGEFILE64_SOURCE
31/* Use this to get ENOMEM on HP-UX */
32#define _INCLUDE_POSIX_SOURCE
33/* Need this to get standard mmap() on OpenSolaris */
34#undef _POSIX_C_SOURCE
35#define _POSIX_C_SOURCE 3
36/* Need this to get valloc() on OpenSolaris */
37#define __EXTENSIONS__
38/* Need this to include <libc.h> on OS X */
39#define _DARWIN_C_SOURCE
40/* Use this to get posix_memalign */
41#if defined HAVE_POSIX_MEMALIGN
42#   undef _XOPEN_SOURCE
43#   define _XOPEN_SOURCE 600
44#endif
45
46#if defined HAVE_STDINT_H
47#   include <stdint.h>
48#elif defined HAVE_INTTYPES_H
49#   include <inttypes.h>
50#endif
51#include <stdlib.h>
52#include <string.h>
53#include <errno.h>
54#include <signal.h>
55
56#if defined HAVE_MALLOC_H
57#   include <malloc.h>
58#endif
59#if defined HAVE_UNISTD_H
60#   include <unistd.h>
61#endif
62#if defined HAVE_SYS_MMAN_H
63#   include <sys/mman.h>
64#endif
65#if defined HAVE_LIBC_H
66#   include <libc.h>
67#endif
68
69#include "libzzuf.h"
70#include "lib-load.h"
71#include "debug.h"
72#include "fuzz.h"
73#include "fd.h"
74
75#if !defined SIGKILL
76#   define SIGKILL 9
77#endif
78
79#if !defined MAP_ANONYMOUS
80#   define MAP_ANONYMOUS MAP_ANON
81#endif
82
83/* TODO: mremap, maybe brk/sbrk (haha) */
84
85/* Library functions that we divert */
86static void *  (*ORIG(calloc))   (size_t nmemb, size_t size);
87static void *  (*ORIG(malloc))   (size_t size);
88static void    (*ORIG(free))     (void *ptr);
89#if defined HAVE_VALLOC
90static void *  (*ORIG(valloc))   (size_t size);
91#endif
92#if defined HAVE_MEMALIGN
93static void *  (*ORIG(memalign)) (size_t boundary, size_t size);
94#endif
95#if defined HAVE_POSIX_MEMALIGN
96static int     (*ORIG(posix_memalign)) (void **memptr, size_t alignment,
97                                        size_t size);
98#endif
99static void *  (*ORIG(realloc))  (void *ptr, size_t size);
100
101#if defined HAVE_MMAP
102static void *  (*ORIG(mmap))     (void *start, size_t length, int prot,
103                                  int flags, int fd, off_t offset);
104#endif
105#if defined HAVE_MMAP64
106static void *  (*ORIG(mmap64))   (void *start, size_t length, int prot,
107                                  int flags, int fd, off64_t offset);
108#endif
109#if defined HAVE_MUNMAP
110static int     (*ORIG(munmap))   (void *start, size_t length);
111#endif
112#if defined HAVE_MAP_FD
113static kern_return_t (*ORIG(map_fd)) (int fd, vm_offset_t offset,
114                                      vm_offset_t *addr, boolean_t find_space,
115                                      vm_size_t numbytes);
116#endif
117
118/* We need a static memory buffer because some functions call memory
119 * allocation routines before our library is loaded. Hell, even dlsym()
120 * calls calloc(), so we need to do something about it */
121#define DUMMY_BYTES 655360 /* 640 kB ought to be enough for anybody */
122static uint64_t dummy_buffer[DUMMY_BYTES / 8];
123static int64_t dummy_offset = 0;
124#define DUMMY_START ((uintptr_t)dummy_buffer)
125#define DUMMY_STOP ((uintptr_t)dummy_buffer + DUMMY_BYTES)
126
127void _zz_mem_init(void)
128{
129    LOADSYM(free);
130    LOADSYM(calloc);
131    LOADSYM(malloc);
132    LOADSYM(realloc);
133}
134
135void *NEW(calloc)(size_t nmemb, size_t size)
136{
137    void *ret;
138    if(!ORIG(calloc))
139    {
140        ret = dummy_buffer + dummy_offset;
141        memset(ret, 0, (nmemb * size + 7) / 8);
142        dummy_offset += (nmemb * size + 7) / 8;
143        debug("%s(%li, %li) = %p", __func__,
144              (long int)nmemb, (long int)size, ret);
145        return ret;
146    }
147    ret = ORIG(calloc)(nmemb, size);
148    if(ret == NULL && _zz_memory && errno == ENOMEM)
149        raise(SIGKILL);
150    return ret;
151}
152
153void *NEW(malloc)(size_t size)
154{
155    void *ret;
156    if(!ORIG(malloc))
157    {
158        ret = dummy_buffer + dummy_offset;
159        dummy_offset += (size + 7) / 8;
160        debug("%s(%li) = %p", __func__, (long int)size, ret);
161        return ret;
162    }
163    ret = ORIG(malloc)(size);
164    if(ret == NULL && _zz_memory && errno == ENOMEM)
165        raise(SIGKILL);
166    return ret;
167}
168
169void NEW(free)(void *ptr)
170{
171    if((uintptr_t)ptr >= DUMMY_START && (uintptr_t)ptr < DUMMY_STOP)
172    {
173        debug("%s(%p)", __func__, ptr);
174        return;
175    }
176    if(!ORIG(free))
177    {
178        /* FIXME: memory leak */
179        debug("%s(%p) IGNORED", __func__, ptr);
180        return;
181    }
182    ORIG(free)(ptr);
183}
184
185void *NEW(realloc)(void *ptr, size_t size)
186{
187    void *ret;
188    if(!ORIG(realloc)
189        || ((uintptr_t)ptr >= DUMMY_START && (uintptr_t)ptr < DUMMY_STOP))
190    {
191        ret = dummy_buffer + dummy_offset;
192        /* XXX: If ptr is NULL, we don't copy anything. If it is non-NULL, we
193         * copy everything even if it is too big, we don't have anything to
194         * overflow really. */
195        if(ptr)
196            memcpy(ret, ptr, size);
197        dummy_offset += (size + 7) * 8;
198        debug("%s(%p, %li) = %p", __func__, ptr, (long int)size, ret);
199        return ret;
200    }
201    LOADSYM(realloc);
202    ret = ORIG(realloc)(ptr, size);
203    if(ret == NULL && _zz_memory && errno == ENOMEM)
204        raise(SIGKILL);
205    return ret;
206}
207
208#if defined HAVE_VALLOC
209void *NEW(valloc)(size_t size)
210{
211    void *ret;
212    LOADSYM(valloc);
213    ret = ORIG(valloc)(size);
214    if(ret == NULL && _zz_memory && errno == ENOMEM)
215        raise(SIGKILL);
216    return ret;
217}
218#endif
219
220#if defined HAVE_MEMALIGN
221void *NEW(memalign)(size_t boundary, size_t size)
222{
223    void *ret;
224    LOADSYM(memalign);
225    ret = ORIG(memalign)(boundary, size);
226    if(ret == NULL && _zz_memory && errno == ENOMEM)
227        raise(SIGKILL);
228    return ret;
229}
230#endif
231
232#if defined HAVE_POSIX_MEMALIGN
233int NEW(posix_memalign)(void **memptr, size_t alignment, size_t size)
234{
235    int ret;
236    LOADSYM(posix_memalign);
237    ret = ORIG(posix_memalign)(memptr, alignment, size);
238    if(ret == ENOMEM && _zz_memory)
239        raise(SIGKILL);
240    return ret;
241}
242#endif
243
244/* Table used for mmap() and munmap() */
245void **maps = NULL;
246int nbmaps = 0;
247
248#define MMAP(fn, off_t) \
249    do { \
250        char *b = MAP_FAILED; \
251        LOADSYM(fn); \
252        if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd) \
253             || !_zz_isactive(fd)) \
254            return ORIG(fn)(start, length, prot, flags, fd, offset); \
255        ret = ORIG(fn)(NULL, length, prot, flags, fd, offset); \
256        if(ret != MAP_FAILED && length) \
257        { \
258            b = ORIG(fn)(start, length, PROT_READ | PROT_WRITE, \
259                         MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
260            if(b == MAP_FAILED) \
261            { \
262                munmap(ret, length); \
263                ret = MAP_FAILED; \
264            } \
265        } \
266        if(b != MAP_FAILED) \
267        { \
268            int i, oldpos; \
269            for(i = 0; i < nbmaps; i += 2) \
270                if(maps[i] == NULL) \
271                    break; \
272            if(i == nbmaps) \
273            { \
274                nbmaps += 2; \
275                maps = realloc(maps, nbmaps * sizeof(void *)); \
276            } \
277            maps[i] = b; \
278            maps[i + 1] = ret; \
279            oldpos = _zz_getpos(fd); \
280            _zz_setpos(fd, offset); /* mmap() maps the fd at offset 0 */ \
281            memcpy(b, ret, length); /* FIXME: get rid of this */ \
282            _zz_fuzz(fd, (uint8_t *)b, length); \
283            _zz_setpos(fd, oldpos); \
284            ret = b; \
285            if(length >= 4) \
286                debug("%s(%p, %li, %i, %i, %i, %lli) = %p \"%c%c%c%c...", \
287                      __func__, start, (long int)length, prot, flags, fd, \
288                      (long long int)offset, ret, b[0], b[1], b[2], b[3]); \
289            else \
290                debug("%s(%p, %li, %i, %i, %i, %lli) = %p \"%c...", \
291                      __func__, start, (long int)length, prot, flags, fd, \
292                      (long long int)offset, ret, b[0]); \
293        } \
294        else \
295            debug("%s(%p, %li, %i, %i, %i, %lli) = %p", \
296                  __func__, start, (long int)length, prot, flags, fd, \
297                  (long long int)offset, ret); \
298    } while(0)
299
300#if defined HAVE_MMAP
301void *NEW(mmap)(void *start, size_t length, int prot, int flags,
302                int fd, off_t offset)
303{
304    void *ret; MMAP(mmap, off_t); return ret;
305}
306#endif
307
308#if defined HAVE_MMAP64
309void *NEW(mmap64)(void *start, size_t length, int prot, int flags,
310                  int fd, off64_t offset)
311{
312    void *ret; MMAP(mmap64, off64_t); return ret;
313}
314#endif
315
316#if defined HAVE_MUNMAP
317int NEW(munmap)(void *start, size_t length)
318{
319    int ret, i;
320
321    LOADSYM(munmap);
322    for(i = 0; i < nbmaps; i++)
323    {
324        if(maps[i] != start)
325            continue;
326
327        ORIG(munmap)(start, length);
328        ret = ORIG(munmap)(maps[i + 1], length);
329        maps[i] = NULL;
330        maps[i + 1] = NULL;
331        debug("%s(%p, %li) = %i", __func__, start, (long int)length, ret);
332        return ret;
333    }
334
335    return ORIG(munmap)(start, length);
336}
337#endif
338
339#if defined HAVE_MAP_FD
340kern_return_t NEW(map_fd)(int fd, vm_offset_t offset, vm_offset_t *addr,
341                          boolean_t find_space, vm_size_t numbytes)
342{
343    kern_return_t ret;
344
345    LOADSYM(map_fd);
346    ret = ORIG(map_fd)(fd, offset, addr, find_space, numbytes);
347    if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd)
348         || !_zz_isactive(fd))
349        return ret;
350
351    if(ret == 0 && numbytes)
352    {
353        /* FIXME: do we also have to rewind the filedescriptor like in mmap? */
354        char *b = malloc(numbytes);
355        memcpy(b, (void *)*addr, numbytes);
356        _zz_fuzz(fd, (void *)b, numbytes);
357        *addr = (vm_offset_t)b;
358        /* FIXME: the map is never freed; there is no such thing as unmap_fd,
359         * but I suppose that kind of map should go when the filedescriptor is
360         * closed (unlike mmap, which returns a persistent buffer). */
361
362        if(numbytes >= 4)
363           debug("%s(%i, %lli, &%p, %i, %lli) = %i \"%c%c%c%c", __func__,
364                 fd, (long long int)offset, (void *)*addr, (int)find_space,
365                 (long long int)numbytes, ret, b[0], b[1], b[2], b[3]);
366        else
367           debug("%s(%i, %lli, &%p, %i, %lli) = %i \"%c", __func__, fd,
368                 (long long int)offset, (void *)*addr, (int)find_space,
369                 (long long int)numbytes, ret, b[0]);
370    }
371    else
372        debug("%s(%i, %lli, &%p, %i, %lli) = %i", __func__, fd,
373              (long long int)offset, (void *)*addr, (int)find_space,
374              (long long int)numbytes, ret);
375
376    return ret;
377}
378#endif
379
Note: See TracBrowser for help on using the repository browser.