Changeset 4005 for zzuf


Ignore:
Timestamp:
Nov 22, 2009, 7:54:37 PM (10 years ago)
Author:
Sam Hocevar
Message:

Refactor dummy memory manager and fix a bug in realloc().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • zzuf/trunk/src/lib-mem.c

    r3696 r4005  
    11/*
    22 *  zzuf - general purpose fuzzer
    3  *  Copyright (c) 2006,2007 Sam Hocevar <sam@zoy.org>
     3 *  Copyright (c) 2006-2009 Sam Hocevar <sam@hocevar.net>
    44 *                All Rights Reserved
    55 *
     
    118118/* We need a static memory buffer because some functions call memory
    119119 * 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 */
    122 static uint64_t dummy_buffer[DUMMY_BYTES / 8];
     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];
    123126static int64_t dummy_offset = 0;
    124127#define DUMMY_START ((uintptr_t)dummy_buffer)
     
    139142    {
    140143        ret = dummy_buffer + dummy_offset;
    141         memset(ret, 0, (nmemb * size + 7) / 8);
    142         dummy_offset += (nmemb * size + 7) / 8;
     144        memset(ret, 0, nmemb * size);
     145        dummy_offset += (nmemb * size + DUMMY_ALIGNMENT - 1) / DUMMY_ALIGNMENT;
    143146        debug("%s(%li, %li) = %p", __func__,
    144147              (long int)nmemb, (long int)size, ret);
     
    157160    {
    158161        ret = dummy_buffer + dummy_offset;
    159         dummy_offset += (size + 7) / 8;
     162        dummy_offset += (size + DUMMY_ALIGNMENT - 1) / DUMMY_ALIGNMENT;
    160163        debug("%s(%li) = %p", __func__, (long int)size, ret);
    161164        return ret;
     
    176179    if(!ORIG(free))
    177180    {
    178         /* FIXME: memory leak */
     181        /* FIXME: if free() doesn't exist yet, we have a memory leak */
    179182        debug("%s(%p) IGNORED", __func__, ptr);
    180183        return;
     
    195198        if(ptr)
    196199            memcpy(ret, ptr, size);
    197         dummy_offset += (size + 7) * 8;
     200        dummy_offset += (size + DUMMY_ALIGNMENT - 1) / DUMMY_ALIGNMENT;
    198201        debug("%s(%p, %li) = %p", __func__, ptr, (long int)size, ret);
    199202        return ret;
Note: See TracChangeset for help on using the changeset viewer.