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

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

Revert any potential overriding macro before declaring a new function.

  • Property svn:keywords set to Id
File size: 12.7 KB
Line 
1/*
2 *  zzuf - general purpose fuzzer
3 *  Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net>
4 *                All Rights Reserved
5 *
6 *  This program is free software. It comes without any warranty, to
7 *  the extent permitted by applicable law. You can redistribute it
8 *  and/or modify it under the terms of the Do What The Fuck You Want
9 *  To Public License, Version 2, as published by Sam Hocevar. See
10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
11 */
12
13/*
14 *  load-mem.c: loaded memory handling functions
15 */
16
17#include "config.h"
18
19/* Need this for RTLD_NEXT */
20#define _GNU_SOURCE
21/* Need this for MAP_ANON and valloc() on FreeBSD (together with cdefs.h) */
22#define _BSD_SOURCE
23#if defined HAVE_SYS_CDEFS_H
24#   include <sys/cdefs.h>
25#endif
26/* Use this to get mmap64() on glibc systems */
27#undef _LARGEFILE64_SOURCE
28#define _LARGEFILE64_SOURCE
29/* Use this to get ENOMEM on HP-UX */
30#define _INCLUDE_POSIX_SOURCE
31/* Need this to get standard mmap() on OpenSolaris */
32#undef _POSIX_C_SOURCE
33#define _POSIX_C_SOURCE 3
34/* Need this to get valloc() on OpenSolaris */
35#define __EXTENSIONS__
36/* Need this to include <libc.h> on OS X */
37#define _DARWIN_C_SOURCE
38/* Use this to get posix_memalign */
39#if defined HAVE_POSIX_MEMALIGN && !defined __sun
40#   undef _XOPEN_SOURCE
41#   define _XOPEN_SOURCE 600
42#endif
43
44#if defined HAVE_STDINT_H
45#   include <stdint.h>
46#elif defined HAVE_INTTYPES_H
47#   include <inttypes.h>
48#endif
49#include <stdlib.h>
50#include <string.h>
51#include <errno.h>
52#include <signal.h>
53
54#if defined HAVE_MALLOC_H
55#   include <malloc.h>
56#endif
57#if defined HAVE_UNISTD_H
58#   include <unistd.h>
59#endif
60#if defined HAVE_SYS_MMAN_H
61#   include <sys/mman.h>
62#endif
63#if defined HAVE_LIBC_H
64#   include <libc.h>
65#endif
66#if defined HAVE_MACH_TASK_H
67#   include <mach/mach.h>
68#   include <mach/task.h>
69#endif
70
71#include "libzzuf.h"
72#include "lib-load.h"
73#include "debug.h"
74#include "fuzz.h"
75#include "fd.h"
76
77#if !defined SIGKILL
78#   define SIGKILL 9
79#endif
80
81#if !defined MAP_ANONYMOUS
82#   define MAP_ANONYMOUS MAP_ANON
83#endif
84
85/* TODO: mremap, maybe brk/sbrk (haha) */
86
87/* Library functions that we divert */
88static void *  (*ORIG(calloc))   (size_t nmemb, size_t size);
89static void *  (*ORIG(malloc))   (size_t size);
90static void    (*ORIG(free))     (void *ptr);
91#if defined HAVE_VALLOC
92static void *  (*ORIG(valloc))   (size_t size);
93#endif
94#if defined HAVE_MEMALIGN
95static void *  (*ORIG(memalign)) (size_t boundary, size_t size);
96#endif
97#if defined HAVE_POSIX_MEMALIGN
98static int     (*ORIG(posix_memalign)) (void **memptr, size_t alignment,
99                                        size_t size);
100#endif
101static void *  (*ORIG(realloc))  (void *ptr, size_t size);
102
103#if defined HAVE_MMAP
104static void *  (*ORIG(mmap))     (void *start, size_t length, int prot,
105                                  int flags, int fd, off_t offset);
106#endif
107#if defined HAVE_MMAP64
108static void *  (*ORIG(mmap64))   (void *start, size_t length, int prot,
109                                  int flags, int fd, off64_t offset);
110#endif
111#if defined HAVE_MUNMAP
112static int     (*ORIG(munmap))   (void *start, size_t length);
113#endif
114#if defined HAVE_MAP_FD
115static kern_return_t (*ORIG(map_fd)) (int fd, vm_offset_t offset,
116                                      vm_offset_t *addr, boolean_t find_space,
117                                      vm_size_t numbytes);
118#endif
119
120/* We need a static memory buffer because some functions call memory
121 * allocation routines before our library is loaded. Hell, even dlsym()
122 * calls calloc(), so we need to do something about it. The dummy buffer
123 * is defined as an uint64_t array to ensure at least 8-byte alignment. */
124#define DUMMY_BYTES 640*1024 /* 640 kB ought to be enough for anybody */
125#define DUMMY_TYPE uint64_t
126#define DUMMY_ALIGNMENT (sizeof(DUMMY_TYPE))
127static DUMMY_TYPE dummy_buffer[DUMMY_BYTES / DUMMY_ALIGNMENT];
128static int64_t dummy_offset = 0;
129#define DUMMY_START ((uintptr_t)dummy_buffer)
130#define DUMMY_STOP ((uintptr_t)dummy_buffer + DUMMY_BYTES)
131
132/* setrlimit(RLIMIT_AS) is ignored on OS X, we need to check memory usage
133 * from inside the process. Oh, and getrusage() doesn't work either. */
134static int memory_exceeded(void)
135{
136#if defined HAVE_MACH_TASK_H
137    struct task_basic_info tbi;
138    mach_msg_type_number_t mmtn = TASK_BASIC_INFO_COUNT;
139
140    if (task_info(mach_task_self(), TASK_BASIC_INFO,
141                  (task_info_t)&tbi, &mmtn) == KERN_SUCCESS
142         && (int64_t)tbi.resident_size / 1048576 > (int64_t)_zz_memory)
143        return 1;
144#endif
145    return 0;
146}
147
148void _zz_mem_init(void)
149{
150    LOADSYM(free);
151    LOADSYM(calloc);
152    LOADSYM(malloc);
153    LOADSYM(realloc);
154}
155
156#undef calloc
157void *NEW(calloc)(size_t nmemb, size_t size)
158{
159    void *ret;
160    if(!ORIG(calloc))
161    {
162        /* Store the chunk length just before the buffer we'll return */
163        size_t lsize = size;
164        memcpy(dummy_buffer + dummy_offset, &lsize, sizeof(size_t));
165        dummy_offset++;
166
167        ret = dummy_buffer + dummy_offset;
168        memset(ret, 0, nmemb * size);
169        dummy_offset += (nmemb * size + DUMMY_ALIGNMENT - 1) / DUMMY_ALIGNMENT;
170        debug("%s(%li, %li) = %p", __func__,
171              (long int)nmemb, (long int)size, ret);
172        return ret;
173    }
174    ret = ORIG(calloc)(nmemb, size);
175    if(ret == NULL && _zz_memory && errno == ENOMEM)
176        raise(SIGKILL);
177    return ret;
178}
179
180#undef malloc
181void *NEW(malloc)(size_t size)
182{
183    void *ret;
184    if(!ORIG(malloc))
185    {
186        /* Store the chunk length just before the buffer we'll return */
187        memcpy(dummy_buffer + dummy_offset, &size, sizeof(size_t));
188        dummy_offset++;
189
190        ret = dummy_buffer + dummy_offset;
191        dummy_offset += (size + DUMMY_ALIGNMENT - 1) / DUMMY_ALIGNMENT;
192        debug("%s(%li) = %p", __func__, (long int)size, ret);
193        return ret;
194    }
195    ret = ORIG(malloc)(size);
196    if (_zz_memory && ((!ret && errno == ENOMEM)
197                        || (ret && memory_exceeded())))
198        raise(SIGKILL);
199    return ret;
200}
201
202#undef free
203void NEW(free)(void *ptr)
204{
205    if((uintptr_t)ptr >= DUMMY_START && (uintptr_t)ptr < DUMMY_STOP)
206    {
207        debug("%s(%p)", __func__, ptr);
208        return;
209    }
210    if(!ORIG(free))
211    {
212        /* FIXME: if free() doesn't exist yet, we have a memory leak */
213        debug("%s(%p) IGNORED", __func__, ptr);
214        return;
215    }
216    ORIG(free)(ptr);
217}
218
219#undef realloc
220void *NEW(realloc)(void *ptr, size_t size)
221{
222    void *ret;
223    if(!ORIG(realloc)
224        || ((uintptr_t)ptr >= DUMMY_START && (uintptr_t)ptr < DUMMY_STOP))
225    {
226        size_t oldsize;
227
228        /* Store the chunk length just before the buffer we'll return */
229        memcpy(dummy_buffer + dummy_offset, &size, sizeof(size_t));
230        dummy_offset++;
231
232        ret = dummy_buffer + dummy_offset;
233        if ((uintptr_t)ptr >= DUMMY_START && (uintptr_t)ptr < DUMMY_STOP)
234            memcpy(&oldsize, (DUMMY_TYPE *)ptr - 1, sizeof(size_t));
235        else
236            oldsize = 0;
237        memcpy(ret, ptr, size < oldsize ? size : oldsize);
238        dummy_offset += (size + DUMMY_ALIGNMENT - 1) / DUMMY_ALIGNMENT;
239        debug("%s(%p, %li) = %p", __func__, ptr, (long int)size, ret);
240        return ret;
241    }
242    LOADSYM(realloc);
243    ret = ORIG(realloc)(ptr, size);
244    if (_zz_memory && ((!ret && errno == ENOMEM)
245                        || (ret && memory_exceeded())))
246        raise(SIGKILL);
247    return ret;
248}
249
250#if defined HAVE_VALLOC
251#undef valloc
252void *NEW(valloc)(size_t size)
253{
254    void *ret;
255    LOADSYM(valloc);
256    ret = ORIG(valloc)(size);
257    if (_zz_memory && ((!ret && errno == ENOMEM)
258                        || (ret && memory_exceeded())))
259        raise(SIGKILL);
260    return ret;
261}
262#endif
263
264#if defined HAVE_MEMALIGN
265#undef memalign
266void *NEW(memalign)(size_t boundary, size_t size)
267{
268    void *ret;
269    LOADSYM(memalign);
270    ret = ORIG(memalign)(boundary, size);
271    if (_zz_memory && ((!ret && errno == ENOMEM)
272                        || (ret && memory_exceeded())))
273        raise(SIGKILL);
274    return ret;
275}
276#endif
277
278#if defined HAVE_POSIX_MEMALIGN
279#undef posix_memalign
280int NEW(posix_memalign)(void **memptr, size_t alignment, size_t size)
281{
282    int ret;
283    LOADSYM(posix_memalign);
284    ret = ORIG(posix_memalign)(memptr, alignment, size);
285    if (_zz_memory && ((!ret && errno == ENOMEM)
286                        || (ret && memory_exceeded())))
287        raise(SIGKILL);
288    return ret;
289}
290#endif
291
292/* Table used for mmap() and munmap() */
293void **maps = NULL;
294int nbmaps = 0;
295
296#define ZZ_MMAP(mymmap, off_t) \
297    do { \
298        char *b = MAP_FAILED; \
299        LOADSYM(mymmap); \
300        if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd) \
301             || !_zz_isactive(fd)) \
302            return ORIG(mymmap)(start, length, prot, flags, fd, offset); \
303        ret = ORIG(mymmap)(NULL, length, prot, flags, fd, offset); \
304        if(ret != MAP_FAILED && length) \
305        { \
306            b = ORIG(mymmap)(start, length, PROT_READ | PROT_WRITE, \
307                             MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
308            if(b == MAP_FAILED) \
309            { \
310                munmap(ret, length); \
311                ret = MAP_FAILED; \
312            } \
313        } \
314        if(b != MAP_FAILED) \
315        { \
316            int i, oldpos; \
317            for(i = 0; i < nbmaps; i += 2) \
318                if(maps[i] == NULL) \
319                    break; \
320            if(i == nbmaps) \
321            { \
322                nbmaps += 2; \
323                maps = realloc(maps, nbmaps * sizeof(void *)); \
324            } \
325            maps[i] = b; \
326            maps[i + 1] = ret; \
327            oldpos = _zz_getpos(fd); \
328            _zz_setpos(fd, offset); /* mmap() maps the fd at offset 0 */ \
329            memcpy(b, ret, length); /* FIXME: get rid of this */ \
330            _zz_fuzz(fd, (uint8_t *)b, length); \
331            _zz_setpos(fd, oldpos); \
332            ret = b; \
333            if(length >= 4) \
334                debug("%s(%p, %li, %i, %i, %i, %lli) = %p \"%c%c%c%c...", \
335                      __func__, start, (long int)length, prot, flags, fd, \
336                      (long long int)offset, ret, b[0], b[1], b[2], b[3]); \
337            else \
338                debug("%s(%p, %li, %i, %i, %i, %lli) = %p \"%c...", \
339                      __func__, start, (long int)length, prot, flags, fd, \
340                      (long long int)offset, ret, b[0]); \
341        } \
342        else \
343            debug("%s(%p, %li, %i, %i, %i, %lli) = %p", \
344                  __func__, start, (long int)length, prot, flags, fd, \
345                  (long long int)offset, ret); \
346    } while(0)
347
348#if defined HAVE_MMAP
349#undef mmap
350void *NEW(mmap)(void *start, size_t length, int prot, int flags,
351                int fd, off_t offset)
352{
353    void *ret; ZZ_MMAP(mmap, off_t); return ret;
354}
355#endif
356
357#if defined HAVE_MMAP64
358#undef mmap64
359void *NEW(mmap64)(void *start, size_t length, int prot, int flags,
360                  int fd, off64_t offset)
361{
362    void *ret; ZZ_MMAP(mmap64, off64_t); return ret;
363}
364#endif
365
366#if defined HAVE_MUNMAP
367#undef munmap
368int NEW(munmap)(void *start, size_t length)
369{
370    int ret, i;
371
372    LOADSYM(munmap);
373    for(i = 0; i < nbmaps; i++)
374    {
375        if(maps[i] != start)
376            continue;
377
378        ORIG(munmap)(start, length);
379        ret = ORIG(munmap)(maps[i + 1], length);
380        maps[i] = NULL;
381        maps[i + 1] = NULL;
382        debug("%s(%p, %li) = %i", __func__, start, (long int)length, ret);
383        return ret;
384    }
385
386    return ORIG(munmap)(start, length);
387}
388#endif
389
390#if defined HAVE_MAP_FD
391#undef map_fd
392kern_return_t NEW(map_fd)(int fd, vm_offset_t offset, vm_offset_t *addr,
393                          boolean_t find_space, vm_size_t numbytes)
394{
395    kern_return_t ret;
396
397    LOADSYM(map_fd);
398    ret = ORIG(map_fd)(fd, offset, addr, find_space, numbytes);
399    if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd)
400         || !_zz_isactive(fd))
401        return ret;
402
403    if(ret == 0 && numbytes)
404    {
405        /* FIXME: do we also have to rewind the filedescriptor like in mmap? */
406        char *b = malloc(numbytes);
407        memcpy(b, (void *)*addr, numbytes);
408        _zz_fuzz(fd, (void *)b, numbytes);
409        *addr = (vm_offset_t)b;
410        /* FIXME: the map is never freed; there is no such thing as unmap_fd,
411         * but I suppose that kind of map should go when the filedescriptor is
412         * closed (unlike mmap, which returns a persistent buffer). */
413
414        if(numbytes >= 4)
415           debug("%s(%i, %lli, &%p, %i, %lli) = %i \"%c%c%c%c", __func__,
416                 fd, (long long int)offset, (void *)*addr, (int)find_space,
417                 (long long int)numbytes, ret, b[0], b[1], b[2], b[3]);
418        else
419           debug("%s(%i, %lli, &%p, %i, %lli) = %i \"%c", __func__, fd,
420                 (long long int)offset, (void *)*addr, (int)find_space,
421                 (long long int)numbytes, ret, b[0]);
422    }
423    else
424        debug("%s(%i, %lli, &%p, %i, %lli) = %i", __func__, fd,
425              (long long int)offset, (void *)*addr, (int)find_space,
426              (long long int)numbytes, ret);
427
428    return ret;
429}
430#endif
431
Note: See TracBrowser for help on using the repository browser.