#include "config.h"
#ifndef HAVE_SOCKLEN_T
#define socklen_t int
#endif
#if !defined(__FreeBSD__) && !defined(__APPLE__)
#define _POSIX_SOURCE
#endif
#define _GNU_SOURCE
#define _XOPEN_SOURCE
#define _BSD_TYPES
#define __EXTENSIONS__
#define _ALL_SOURCE
#define _LARGE_FILE_API
#define _XOPEN_SOURCE_EXTENDED 1
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <time.h>
#include "mf-runtime.h"
#include "mf-impl.h"
#ifdef _MUDFLAP
#error "Do not compile this file with -fmudflap!"
#endif
#if PIC
void *
__mf_0fn_malloc (size_t c)
{
return NULL;
}
#endif
#undef malloc
WRAPPER(void *, malloc, size_t c)
{
size_t size_with_crumple_zones;
DECLARE(void *, malloc, size_t c);
void *result;
BEGIN_PROTECT (malloc, c);
size_with_crumple_zones =
CLAMPADD(c,CLAMPADD(__mf_opts.crumple_zone,
__mf_opts.crumple_zone));
result = (char *) CALL_REAL (malloc, size_with_crumple_zones);
if (LIKELY(result))
{
result += __mf_opts.crumple_zone;
__mf_register (result, c, __MF_TYPE_HEAP, "malloc region");
}
return result;
}
#ifdef PIC
void *
__mf_0fn_calloc (size_t c, size_t n)
{
enum foo { BS = 4096, NB=10 };
static char bufs[NB][BS];
static unsigned bufs_used[NB];
unsigned i;
for (i=0; i<NB; i++)
{
if (! bufs_used[i] && (c*n) < BS)
{
bufs_used[i] = 1;
return & bufs[i][0];
}
}
return NULL;
}
#endif
#undef calloc
WRAPPER(void *, calloc, size_t c, size_t n)
{
size_t size_with_crumple_zones;
DECLARE(void *, calloc, size_t, size_t);
DECLARE(void *, malloc, size_t);
DECLARE(void *, memset, void *, int, size_t);
char *result;
BEGIN_PROTECT (calloc, c, n);
size_with_crumple_zones =
CLAMPADD((c * n),
CLAMPADD(__mf_opts.crumple_zone,
__mf_opts.crumple_zone));
result = (char *) CALL_REAL (malloc, size_with_crumple_zones);
if (LIKELY(result))
memset (result, 0, size_with_crumple_zones);
if (LIKELY(result))
{
result += __mf_opts.crumple_zone;
__mf_register (result, c*n , __MF_TYPE_HEAP_I, "calloc region");
}
return result;
}
#if PIC
void *
__mf_0fn_realloc (void *buf, size_t c)
{
return NULL;
}
#endif
#undef realloc
WRAPPER(void *, realloc, void *buf, size_t c)
{
DECLARE(void * , realloc, void *, size_t);
size_t size_with_crumple_zones;
char *base = buf;
unsigned saved_wipe_heap;
char *result;
BEGIN_PROTECT (realloc, buf, c);
if (LIKELY(buf))
base -= __mf_opts.crumple_zone;
size_with_crumple_zones =
CLAMPADD(c, CLAMPADD(__mf_opts.crumple_zone,
__mf_opts.crumple_zone));
result = (char *) CALL_REAL (realloc, base, size_with_crumple_zones);
LOCKTH ();
__mf_state = reentrant;
saved_wipe_heap = __mf_opts.wipe_heap;
__mf_opts.wipe_heap = 0;
if (LIKELY(buf))
__mfu_unregister (buf, 0, __MF_TYPE_HEAP_I);
if (LIKELY(result))
{
result += __mf_opts.crumple_zone;
__mfu_register (result, c, __MF_TYPE_HEAP_I, "realloc region");
}
__mf_opts.wipe_heap = saved_wipe_heap;
__mf_state = active;
UNLOCKTH ();
return result;
}
#if PIC
void
__mf_0fn_free (void *buf)
{
return;
}
#endif
#undef free
WRAPPER(void, free, void *buf)
{
static void *free_queue [__MF_FREEQ_MAX];
static unsigned free_ptr = 0;
static int freeq_initialized = 0;
DECLARE(void, free, void *);
BEGIN_PROTECT (free, buf);
if (UNLIKELY(buf == NULL))
return;
LOCKTH ();
if (UNLIKELY(!freeq_initialized))
{
memset (free_queue, 0,
__MF_FREEQ_MAX * sizeof (void *));
freeq_initialized = 1;
}
UNLOCKTH ();
__mf_unregister (buf, 0, __MF_TYPE_HEAP_I);
if (UNLIKELY(__mf_opts.free_queue_length > 0))
{
char *freeme = NULL;
LOCKTH ();
if (free_queue [free_ptr] != NULL)
{
freeme = free_queue [free_ptr];
freeme -= __mf_opts.crumple_zone;
}
free_queue [free_ptr] = buf;
free_ptr = (free_ptr == (__mf_opts.free_queue_length-1) ? 0 : free_ptr + 1);
UNLOCKTH ();
if (freeme)
{
if (__mf_opts.trace_mf_calls)
{
VERBOSE_TRACE ("freeing deferred pointer %p (crumple %u)\n",
(void *) freeme,
__mf_opts.crumple_zone);
}
CALL_REAL (free, freeme);
}
}
else
{
char *base = (char *)buf;
base -= __mf_opts.crumple_zone;
if (__mf_opts.trace_mf_calls)
{
VERBOSE_TRACE ("freeing pointer %p = %p - %u\n",
(void *) base,
(void *) buf,
__mf_opts.crumple_zone);
}
CALL_REAL (free, base);
}
}
#if PIC
void *
__mf_0fn_mmap (void *start, size_t l, int prot, int f, int fd, off_t off)
{
return (void *) -1;
}
#endif
#undef mmap
WRAPPER(void *, mmap,
void *start, size_t length, int prot,
int flags, int fd, off_t offset)
{
DECLARE(void *, mmap, void *, size_t, int,
int, int, off_t);
void *result;
BEGIN_PROTECT (mmap, start, length, prot, flags, fd, offset);
result = CALL_REAL (mmap, start, length, prot,
flags, fd, offset);
if (result != (void *)-1)
{
size_t ps = getpagesize ();
uintptr_t base = (uintptr_t) result;
uintptr_t offset;
for (offset=0; offset<length; offset+=ps)
{
__mf_register ((void *) CLAMPADD (base, offset), ps,
__MF_TYPE_HEAP_I, "mmap page");
}
}
return result;
}
#if PIC
int
__mf_0fn_munmap (void *start, size_t length)
{
return -1;
}
#endif
#undef munmap
WRAPPER(int , munmap, void *start, size_t length)
{
DECLARE(int, munmap, void *, size_t);
int result;
BEGIN_PROTECT (munmap, start, length);
result = CALL_REAL (munmap, start, length);
if (result == 0)
{
size_t ps = getpagesize ();
uintptr_t base = (uintptr_t) start & (~ (ps - 1));
uintptr_t offset;
for (offset=0; offset<length; offset+=ps)
__mf_unregister ((void *) CLAMPADD (base, offset), ps, __MF_TYPE_HEAP_I);
}
return result;
}
void *
__mf_wrap_alloca_indirect (size_t c)
{
DECLARE (void *, malloc, size_t);
DECLARE (void, free, void *);
struct alloca_tracking { void *ptr; void *stack; struct alloca_tracking* next; };
static struct alloca_tracking *alloca_history = NULL;
void *stack = __builtin_frame_address (0);
void *result;
struct alloca_tracking *track;
TRACE ("%s\n", __PRETTY_FUNCTION__);
VERBOSE_TRACE ("alloca stack level %p\n", (void *) stack);
#define DEEPER_THAN <
while (alloca_history &&
((uintptr_t) alloca_history->stack DEEPER_THAN (uintptr_t) stack))
{
struct alloca_tracking *next = alloca_history->next;
__mf_unregister (alloca_history->ptr, 0, __MF_TYPE_HEAP);
CALL_REAL (free, alloca_history->ptr);
CALL_REAL (free, alloca_history);
alloca_history = next;
}
result = NULL;
if (LIKELY (c > 0))
{
track = (struct alloca_tracking *) CALL_REAL (malloc,
sizeof (struct alloca_tracking));
if (LIKELY (track != NULL))
{
result = CALL_REAL (malloc, c);
if (UNLIKELY (result == NULL))
{
CALL_REAL (free, track);
}
else
{
__mf_register (result, c, __MF_TYPE_HEAP, "alloca region");
track->ptr = result;
track->stack = stack;
track->next = alloca_history;
alloca_history = track;
}
}
}
return result;
}
#undef alloca
WRAPPER(void *, alloca, size_t c)
{
return __mf_wrap_alloca_indirect (c);
}