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

Last change on this file since 4258 was 4258, checked in by Sam Hocevar, 10 years ago

Add a workaround for OS X ignoring RLIMIT_RSS / RLIMIT_AS and getrusage()
not filling in RSS information. After each memory allocation, we ask the
Mach for the RSS value.

  • Property svn:keywords set to Id
File size: 12.5 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
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         && tbi.resident_size > _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
156void *NEW(calloc)(size_t nmemb, size_t size)
157{
158    void *ret;
159    if(!ORIG(calloc))
160    {
161        /* Store the chunk length just before the buffer we'll return */
162        size_t lsize = size;
163        memcpy(dummy_buffer + dummy_offset, &lsize, sizeof(size_t));
164        dummy_offset++;
165
166        ret = dummy_buffer + dummy_offset;
167        memset(ret, 0, nmemb * size);
168        dummy_offset += (nmemb * size + DUMMY_ALIGNMENT - 1) / DUMMY_ALIGNMENT;
169        debug("%s(%li, %li) = %p", __func__,
170              (long int)nmemb, (long int)size, ret);
171        return ret;
172    }
173    ret = ORIG(calloc)(nmemb, size);
174    if(ret == NULL && _zz_memory && errno == ENOMEM)
175        raise(SIGKILL);
176    return ret;
177}
178
179void *NEW(malloc)(size_t size)
180{
181    void *ret;
182    if(!ORIG(malloc))
183    {
184        /* Store the chunk length just before the buffer we'll return */
185        memcpy(dummy_buffer + dummy_offset, &size, sizeof(size_t));
186        dummy_offset++;
187
188        ret = dummy_buffer + dummy_offset;
189        dummy_offset += (size + DUMMY_ALIGNMENT - 1) / DUMMY_ALIGNMENT;
190        debug("%s(%li) = %p", __func__, (long int)size, ret);
191        return ret;
192    }
193    ret = ORIG(malloc)(size);
194    if (_zz_memory && ((!ret && errno == ENOMEM)
195                        || (ret && memory_exceeded())))
196        raise(SIGKILL);
197    return ret;
198}
199
200void NEW(free)(void *ptr)
201{
202    if((uintptr_t)ptr >= DUMMY_START && (uintptr_t)ptr < DUMMY_STOP)
203    {
204        debug("%s(%p)", __func__, ptr);
205        return;
206    }
207    if(!ORIG(free))
208    {
209        /* FIXME: if free() doesn't exist yet, we have a memory leak */
210        debug("%s(%p) IGNORED", __func__, ptr);
211        return;
212    }
213    ORIG(free)(ptr);
214}
215
216void *NEW(realloc)(void *ptr, size_t size)
217{
218    void *ret;
219    if(!ORIG(realloc)
220        || ((uintptr_t)ptr >= DUMMY_START && (uintptr_t)ptr < DUMMY_STOP))
221    {
222        size_t oldsize;
223
224        /* Store the chunk length just before the buffer we'll return */
225        memcpy(dummy_buffer + dummy_offset, &size, sizeof(size_t));
226        dummy_offset++;
227
228        ret = dummy_buffer + dummy_offset;
229        if ((uintptr_t)ptr >= DUMMY_START && (uintptr_t)ptr < DUMMY_STOP)
230            memcpy(&oldsize, (DUMMY_TYPE *)ptr - 1, sizeof(size_t));
231        else
232            oldsize = 0;
233        memcpy(ret, ptr, size < oldsize ? size : oldsize);
234        dummy_offset += (size + DUMMY_ALIGNMENT - 1) / DUMMY_ALIGNMENT;
235        debug("%s(%p, %li) = %p", __func__, ptr, (long int)size, ret);
236        return ret;
237    }
238    LOADSYM(realloc);
239    ret = ORIG(realloc)(ptr, size);
240    if (_zz_memory && ((!ret && errno == ENOMEM)
241                        || (ret && memory_exceeded())))
242        raise(SIGKILL);
243    return ret;
244}
245
246#if defined HAVE_VALLOC
247void *NEW(valloc)(size_t size)
248{
249    void *ret;
250    LOADSYM(valloc);
251    ret = ORIG(valloc)(size);
252    if (_zz_memory && ((!ret && errno == ENOMEM)
253                        || (ret && memory_exceeded())))
254        raise(SIGKILL);
255    return ret;
256}
257#endif
258
259#if defined HAVE_MEMALIGN
260void *NEW(memalign)(size_t boundary, size_t size)
261{
262    void *ret;
263    LOADSYM(memalign);
264    ret = ORIG(memalign)(boundary, size);
265    if (_zz_memory && ((!ret && errno == ENOMEM)
266                        || (ret && memory_exceeded())))
267        raise(SIGKILL);
268    return ret;
269}
270#endif
271
272#if defined HAVE_POSIX_MEMALIGN
273int NEW(posix_memalign)(void **memptr, size_t alignment, size_t size)
274{
275    int ret;
276    LOADSYM(posix_memalign);
277    ret = ORIG(posix_memalign)(memptr, alignment, size);
278    if (_zz_memory && ((!ret && errno == ENOMEM)
279                        || (ret && memory_exceeded())))
280        raise(SIGKILL);
281    return ret;
282}
283#endif
284
285/* Table used for mmap() and munmap() */
286void **maps = NULL;
287int nbmaps = 0;
288
289#define ZZ_MMAP(mymmap, off_t) \
290    do { \
291        char *b = MAP_FAILED; \
292        LOADSYM(mymmap); \
293        if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd) \
294             || !_zz_isactive(fd)) \
295            return ORIG(mymmap)(start, length, prot, flags, fd, offset); \
296        ret = ORIG(mymmap)(NULL, length, prot, flags, fd, offset); \
297        if(ret != MAP_FAILED && length) \
298        { \
299            b = ORIG(mymmap)(start, length, PROT_READ | PROT_WRITE, \
300                             MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
301            if(b == MAP_FAILED) \
302            { \
303                munmap(ret, length); \
304                ret = MAP_FAILED; \
305            } \
306        } \
307        if(b != MAP_FAILED) \
308        { \
309            int i, oldpos; \
310            for(i = 0; i < nbmaps; i += 2) \
311                if(maps[i] == NULL) \
312                    break; \
313            if(i == nbmaps) \
314            { \
315                nbmaps += 2; \
316                maps = realloc(maps, nbmaps * sizeof(void *)); \
317            } \
318            maps[i] = b; \
319            maps[i + 1] = ret; \
320            oldpos = _zz_getpos(fd); \
321            _zz_setpos(fd, offset); /* mmap() maps the fd at offset 0 */ \
322            memcpy(b, ret, length); /* FIXME: get rid of this */ \
323            _zz_fuzz(fd, (uint8_t *)b, length); \
324            _zz_setpos(fd, oldpos); \
325            ret = b; \
326            if(length >= 4) \
327                debug("%s(%p, %li, %i, %i, %i, %lli) = %p \"%c%c%c%c...", \
328                      __func__, start, (long int)length, prot, flags, fd, \
329                      (long long int)offset, ret, b[0], b[1], b[2], b[3]); \
330            else \
331                debug("%s(%p, %li, %i, %i, %i, %lli) = %p \"%c...", \
332                      __func__, start, (long int)length, prot, flags, fd, \
333                      (long long int)offset, ret, b[0]); \
334        } \
335        else \
336            debug("%s(%p, %li, %i, %i, %i, %lli) = %p", \
337                  __func__, start, (long int)length, prot, flags, fd, \
338                  (long long int)offset, ret); \
339    } while(0)
340
341#if defined HAVE_MMAP
342void *NEW(mmap)(void *start, size_t length, int prot, int flags,
343                int fd, off_t offset)
344{
345    void *ret; ZZ_MMAP(mmap, off_t); return ret;
346}
347#endif
348
349#if defined HAVE_MMAP64
350void *NEW(mmap64)(void *start, size_t length, int prot, int flags,
351                  int fd, off64_t offset)
352{
353    void *ret; ZZ_MMAP(mmap64, off64_t); return ret;
354}
355#endif
356
357#if defined HAVE_MUNMAP
358int NEW(munmap)(void *start, size_t length)
359{
360    int ret, i;
361
362    LOADSYM(munmap);
363    for(i = 0; i < nbmaps; i++)
364    {
365        if(maps[i] != start)
366            continue;
367
368        ORIG(munmap)(start, length);
369        ret = ORIG(munmap)(maps[i + 1], length);
370        maps[i] = NULL;
371        maps[i + 1] = NULL;
372        debug("%s(%p, %li) = %i", __func__, start, (long int)length, ret);
373        return ret;
374    }
375
376    return ORIG(munmap)(start, length);
377}
378#endif
379
380#if defined HAVE_MAP_FD
381kern_return_t NEW(map_fd)(int fd, vm_offset_t offset, vm_offset_t *addr,
382                          boolean_t find_space, vm_size_t numbytes)
383{
384    kern_return_t ret;
385
386    LOADSYM(map_fd);
387    ret = ORIG(map_fd)(fd, offset, addr, find_space, numbytes);
388    if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd)
389         || !_zz_isactive(fd))
390        return ret;
391
392    if(ret == 0 && numbytes)
393    {
394        /* FIXME: do we also have to rewind the filedescriptor like in mmap? */
395        char *b = malloc(numbytes);
396        memcpy(b, (void *)*addr, numbytes);
397        _zz_fuzz(fd, (void *)b, numbytes);
398        *addr = (vm_offset_t)b;
399        /* FIXME: the map is never freed; there is no such thing as unmap_fd,
400         * but I suppose that kind of map should go when the filedescriptor is
401         * closed (unlike mmap, which returns a persistent buffer). */
402
403        if(numbytes >= 4)
404           debug("%s(%i, %lli, &%p, %i, %lli) = %i \"%c%c%c%c", __func__,
405                 fd, (long long int)offset, (void *)*addr, (int)find_space,
406                 (long long int)numbytes, ret, b[0], b[1], b[2], b[3]);
407        else
408           debug("%s(%i, %lli, &%p, %i, %lli) = %i \"%c", __func__, fd,
409                 (long long int)offset, (void *)*addr, (int)find_space,
410                 (long long int)numbytes, ret, b[0]);
411    }
412    else
413        debug("%s(%i, %lli, &%p, %i, %lli) = %i", __func__, fd,
414              (long long int)offset, (void *)*addr, (int)find_space,
415              (long long int)numbytes, ret);
416
417    return ret;
418}
419#endif
420
Note: See TracBrowser for help on using the repository browser.