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

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

Reorganise source code to better separate zzuf and libzzuf. Note: the Win32
build is now broken.

  • Property svn:keywords set to Id
File size: 11.2 KB
Line 
1/*
2 *  zzuf - general purpose fuzzer
3 *  Copyright (c) 2006-2009 Sam Hocevar <sam@hocevar.net>
4 *                All Rights Reserved
5 *
6 *  $Id: lib-mem.c 4112 2009-12-12 22:19:47Z 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. The dummy buffer
121 * is defined as an uint64_t array to ensure at least 8-byte alignment. */
122#define DUMMY_BYTES 640*1024 /* 640 kB ought to be enough for anybody */
123#define DUMMY_TYPE uint64_t
124#define DUMMY_ALIGNMENT (sizeof(DUMMY_TYPE))
125static DUMMY_TYPE dummy_buffer[DUMMY_BYTES / DUMMY_ALIGNMENT];
126static int64_t dummy_offset = 0;
127#define DUMMY_START ((uintptr_t)dummy_buffer)
128#define DUMMY_STOP ((uintptr_t)dummy_buffer + DUMMY_BYTES)
129
130void _zz_mem_init(void)
131{
132    LOADSYM(free);
133    LOADSYM(calloc);
134    LOADSYM(malloc);
135    LOADSYM(realloc);
136}
137
138void *NEW(calloc)(size_t nmemb, size_t size)
139{
140    void *ret;
141    if(!ORIG(calloc))
142    {
143        ret = dummy_buffer + dummy_offset;
144        memset(ret, 0, nmemb * size);
145        dummy_offset += (nmemb * size + DUMMY_ALIGNMENT - 1) / DUMMY_ALIGNMENT;
146        debug("%s(%li, %li) = %p", __func__,
147              (long int)nmemb, (long int)size, ret);
148        return ret;
149    }
150    ret = ORIG(calloc)(nmemb, size);
151    if(ret == NULL && _zz_memory && errno == ENOMEM)
152        raise(SIGKILL);
153    return ret;
154}
155
156void *NEW(malloc)(size_t size)
157{
158    void *ret;
159    if(!ORIG(malloc))
160    {
161        ret = dummy_buffer + dummy_offset;
162        dummy_offset += (size + DUMMY_ALIGNMENT - 1) / DUMMY_ALIGNMENT;
163        debug("%s(%li) = %p", __func__, (long int)size, ret);
164        return ret;
165    }
166    ret = ORIG(malloc)(size);
167    if(ret == NULL && _zz_memory && errno == ENOMEM)
168        raise(SIGKILL);
169    return ret;
170}
171
172void NEW(free)(void *ptr)
173{
174    if((uintptr_t)ptr >= DUMMY_START && (uintptr_t)ptr < DUMMY_STOP)
175    {
176        debug("%s(%p)", __func__, ptr);
177        return;
178    }
179    if(!ORIG(free))
180    {
181        /* FIXME: if free() doesn't exist yet, we have a memory leak */
182        debug("%s(%p) IGNORED", __func__, ptr);
183        return;
184    }
185    ORIG(free)(ptr);
186}
187
188void *NEW(realloc)(void *ptr, size_t size)
189{
190    void *ret;
191    if(!ORIG(realloc)
192        || ((uintptr_t)ptr >= DUMMY_START && (uintptr_t)ptr < DUMMY_STOP))
193    {
194        ret = dummy_buffer + dummy_offset;
195        /* XXX: If ptr is NULL, we don't copy anything. If it is non-NULL, we
196         * copy everything even if it is too big, we don't have anything to
197         * overflow really. */
198        if(ptr)
199            memcpy(ret, ptr, size);
200        dummy_offset += (size + DUMMY_ALIGNMENT - 1) / DUMMY_ALIGNMENT;
201        debug("%s(%p, %li) = %p", __func__, ptr, (long int)size, ret);
202        return ret;
203    }
204    LOADSYM(realloc);
205    ret = ORIG(realloc)(ptr, size);
206    if(ret == NULL && _zz_memory && errno == ENOMEM)
207        raise(SIGKILL);
208    return ret;
209}
210
211#if defined HAVE_VALLOC
212void *NEW(valloc)(size_t size)
213{
214    void *ret;
215    LOADSYM(valloc);
216    ret = ORIG(valloc)(size);
217    if(ret == NULL && _zz_memory && errno == ENOMEM)
218        raise(SIGKILL);
219    return ret;
220}
221#endif
222
223#if defined HAVE_MEMALIGN
224void *NEW(memalign)(size_t boundary, size_t size)
225{
226    void *ret;
227    LOADSYM(memalign);
228    ret = ORIG(memalign)(boundary, size);
229    if(ret == NULL && _zz_memory && errno == ENOMEM)
230        raise(SIGKILL);
231    return ret;
232}
233#endif
234
235#if defined HAVE_POSIX_MEMALIGN
236int NEW(posix_memalign)(void **memptr, size_t alignment, size_t size)
237{
238    int ret;
239    LOADSYM(posix_memalign);
240    ret = ORIG(posix_memalign)(memptr, alignment, size);
241    if(ret == ENOMEM && _zz_memory)
242        raise(SIGKILL);
243    return ret;
244}
245#endif
246
247/* Table used for mmap() and munmap() */
248void **maps = NULL;
249int nbmaps = 0;
250
251#define MMAP(mymmap, off_t) \
252    do { \
253        char *b = MAP_FAILED; \
254        LOADSYM(mymmap); \
255        if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd) \
256             || !_zz_isactive(fd)) \
257            return ORIG(mymmap)(start, length, prot, flags, fd, offset); \
258        ret = ORIG(mymmap)(NULL, length, prot, flags, fd, offset); \
259        if(ret != MAP_FAILED && length) \
260        { \
261            b = ORIG(mymmap)(start, length, PROT_READ | PROT_WRITE, \
262                             MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
263            if(b == MAP_FAILED) \
264            { \
265                munmap(ret, length); \
266                ret = MAP_FAILED; \
267            } \
268        } \
269        if(b != MAP_FAILED) \
270        { \
271            int i, oldpos; \
272            for(i = 0; i < nbmaps; i += 2) \
273                if(maps[i] == NULL) \
274                    break; \
275            if(i == nbmaps) \
276            { \
277                nbmaps += 2; \
278                maps = realloc(maps, nbmaps * sizeof(void *)); \
279            } \
280            maps[i] = b; \
281            maps[i + 1] = ret; \
282            oldpos = _zz_getpos(fd); \
283            _zz_setpos(fd, offset); /* mmap() maps the fd at offset 0 */ \
284            memcpy(b, ret, length); /* FIXME: get rid of this */ \
285            _zz_fuzz(fd, (uint8_t *)b, length); \
286            _zz_setpos(fd, oldpos); \
287            ret = b; \
288            if(length >= 4) \
289                debug("%s(%p, %li, %i, %i, %i, %lli) = %p \"%c%c%c%c...", \
290                      __func__, start, (long int)length, prot, flags, fd, \
291                      (long long int)offset, ret, b[0], b[1], b[2], b[3]); \
292            else \
293                debug("%s(%p, %li, %i, %i, %i, %lli) = %p \"%c...", \
294                      __func__, start, (long int)length, prot, flags, fd, \
295                      (long long int)offset, ret, b[0]); \
296        } \
297        else \
298            debug("%s(%p, %li, %i, %i, %i, %lli) = %p", \
299                  __func__, start, (long int)length, prot, flags, fd, \
300                  (long long int)offset, ret); \
301    } while(0)
302
303#if defined HAVE_MMAP
304void *NEW(mmap)(void *start, size_t length, int prot, int flags,
305                int fd, off_t offset)
306{
307    void *ret; MMAP(mmap, off_t); return ret;
308}
309#endif
310
311#if defined HAVE_MMAP64
312void *NEW(mmap64)(void *start, size_t length, int prot, int flags,
313                  int fd, off64_t offset)
314{
315    void *ret; MMAP(mmap64, off64_t); return ret;
316}
317#endif
318
319#if defined HAVE_MUNMAP
320int NEW(munmap)(void *start, size_t length)
321{
322    int ret, i;
323
324    LOADSYM(munmap);
325    for(i = 0; i < nbmaps; i++)
326    {
327        if(maps[i] != start)
328            continue;
329
330        ORIG(munmap)(start, length);
331        ret = ORIG(munmap)(maps[i + 1], length);
332        maps[i] = NULL;
333        maps[i + 1] = NULL;
334        debug("%s(%p, %li) = %i", __func__, start, (long int)length, ret);
335        return ret;
336    }
337
338    return ORIG(munmap)(start, length);
339}
340#endif
341
342#if defined HAVE_MAP_FD
343kern_return_t NEW(map_fd)(int fd, vm_offset_t offset, vm_offset_t *addr,
344                          boolean_t find_space, vm_size_t numbytes)
345{
346    kern_return_t ret;
347
348    LOADSYM(map_fd);
349    ret = ORIG(map_fd)(fd, offset, addr, find_space, numbytes);
350    if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd)
351         || !_zz_isactive(fd))
352        return ret;
353
354    if(ret == 0 && numbytes)
355    {
356        /* FIXME: do we also have to rewind the filedescriptor like in mmap? */
357        char *b = malloc(numbytes);
358        memcpy(b, (void *)*addr, numbytes);
359        _zz_fuzz(fd, (void *)b, numbytes);
360        *addr = (vm_offset_t)b;
361        /* FIXME: the map is never freed; there is no such thing as unmap_fd,
362         * but I suppose that kind of map should go when the filedescriptor is
363         * closed (unlike mmap, which returns a persistent buffer). */
364
365        if(numbytes >= 4)
366           debug("%s(%i, %lli, &%p, %i, %lli) = %i \"%c%c%c%c", __func__,
367                 fd, (long long int)offset, (void *)*addr, (int)find_space,
368                 (long long int)numbytes, ret, b[0], b[1], b[2], b[3]);
369        else
370           debug("%s(%i, %lli, &%p, %i, %lli) = %i \"%c", __func__, fd,
371                 (long long int)offset, (void *)*addr, (int)find_space,
372                 (long long int)numbytes, ret, b[0]);
373    }
374    else
375        debug("%s(%i, %lli, &%p, %i, %lli) = %i", __func__, fd,
376              (long long int)offset, (void *)*addr, (int)find_space,
377              (long long int)numbytes, ret);
378
379    return ret;
380}
381#endif
382
Note: See TracBrowser for help on using the repository browser.