2004-05-13 08:41:07 +02:00
|
|
|
/* Mudflap: narrow-pointer bounds-checking by tree rewriting.
|
2009-04-09 17:00:19 +02:00
|
|
|
Copyright (C) 2002, 2003, 2004, 2009 Free Software Foundation, Inc.
|
2004-05-13 08:41:07 +02:00
|
|
|
Contributed by Frank Ch. Eigler <fche@redhat.com>
|
|
|
|
and Graydon Hoare <graydon@redhat.com>
|
|
|
|
|
|
|
|
This file is part of GCC.
|
|
|
|
|
|
|
|
GCC is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free
|
2009-04-09 17:00:19 +02:00
|
|
|
Software Foundation; either version 3, or (at your option) any later
|
2004-05-13 08:41:07 +02:00
|
|
|
version.
|
|
|
|
|
|
|
|
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
2009-04-09 17:00:19 +02:00
|
|
|
Under Section 7 of GPL version 3, you are granted additional
|
|
|
|
permissions described in the GCC Runtime Library Exception, version
|
|
|
|
3.1, as published by the Free Software Foundation.
|
2004-05-13 08:41:07 +02:00
|
|
|
|
2009-04-09 17:00:19 +02:00
|
|
|
You should have received a copy of the GNU General Public License and
|
|
|
|
a copy of the GCC Runtime Library Exception along with this program;
|
|
|
|
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
2004-05-13 08:41:07 +02:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#ifndef HAVE_SOCKLEN_T
|
|
|
|
#define socklen_t int
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* These attempt to coax various unix flavours to declare all our
|
|
|
|
needed tidbits in the system headers. */
|
|
|
|
#if !defined(__FreeBSD__) && !defined(__APPLE__)
|
|
|
|
#define _POSIX_SOURCE
|
|
|
|
#endif /* Some BSDs break <sys/socket.h> if this is defined. */
|
2005-03-22 03:14:33 +01:00
|
|
|
#define _GNU_SOURCE
|
2004-05-13 08:41:07 +02:00
|
|
|
#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
|
|
|
|
|
|
|
|
|
|
|
|
/* Memory allocation related hook functions. Some of these are
|
|
|
|
intercepted via linker wrapping or symbol interposition. Others
|
|
|
|
use plain macros in mf-runtime.h. */
|
|
|
|
|
|
|
|
|
|
|
|
#if PIC
|
2006-11-10 19:42:28 +01:00
|
|
|
|
|
|
|
enum { BS = 4096, NB=10 };
|
|
|
|
static char __mf_0fn_bufs[NB][BS];
|
|
|
|
static unsigned __mf_0fn_bufs_used[NB];
|
|
|
|
|
|
|
|
|
2004-05-13 08:41:07 +02:00
|
|
|
/* A special bootstrap variant. */
|
|
|
|
void *
|
|
|
|
__mf_0fn_malloc (size_t c)
|
|
|
|
{
|
gcc.c (MFWRAP_SPEC): Don't wrap pthread_join or pthread_exit.
* gcc.c (MFWRAP_SPEC): Don't wrap pthread_join or pthread_exit.
* acinclude.m4: New file.
* configure.ac: Invoke LIBMUDFLAP_CHECK_TLS.
* configure, config.h.in, Makefile.in, testsuite/Makefile.in: Rebuild.
* mf-hooks1.c (__mf_0fn_malloc): Move body from ...
(__mf_0fn_calloc): ... here.
* mf-hooks3.c (struct pthread_info): Remove.
(__mf_pthread_info, __mf_pthread_info_idx): Remove.
(LIBMUDFLAPTH_THREADS_MAX): Set to 1021.
(struct mf_thread_data): New.
(mf_thread_data, mf_thread_data_lock): New.
(__mf_allocate_blank_threadinfo): Remove.
(__mf_find_threadinfo): Rewrite and simplify. Only use if TLS is
not available.
(__mf_state_perthread): Remove.
(__mf_get_state, __mf_set_state): New.
(__mf_pthread_cleanup): Use &errno, rather than saved pointer.
Update mf_thread_data killing procedure.
(__mf_pthread_spawner): Similarly.
(__mf_0fn_pthread_create): Only use wrapper if necessary. Remove
code to allocate thread stack space.
(__mf_0fn_pthread_join, pthread_join): Remove.
(__mf_0fn_pthread_exit, pthread_exit): Remove.
* mf-impl.h (dyn_pthread_join, dyn_pthread_exit): Remove.
(__mf_state_1): Rename from __mf_state; use TLS when available.
(__mf_get_state, __mf_set_state): New. Update all users.
* mf-runtime.c (begin_recursion_protect1): New.
(BEGIN_RECURSION_PROTECT): Use it.
(__mf_state_1): Rename from __mf_state; use TLS when available.
(threads_active_p): Remove.
(__mf_usage): Compute it directly.
From-SVN: r102108
2005-07-17 04:29:00 +02:00
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i=0; i<NB; i++)
|
|
|
|
{
|
2006-11-10 19:42:28 +01:00
|
|
|
if (! __mf_0fn_bufs_used[i] && c < BS)
|
gcc.c (MFWRAP_SPEC): Don't wrap pthread_join or pthread_exit.
* gcc.c (MFWRAP_SPEC): Don't wrap pthread_join or pthread_exit.
* acinclude.m4: New file.
* configure.ac: Invoke LIBMUDFLAP_CHECK_TLS.
* configure, config.h.in, Makefile.in, testsuite/Makefile.in: Rebuild.
* mf-hooks1.c (__mf_0fn_malloc): Move body from ...
(__mf_0fn_calloc): ... here.
* mf-hooks3.c (struct pthread_info): Remove.
(__mf_pthread_info, __mf_pthread_info_idx): Remove.
(LIBMUDFLAPTH_THREADS_MAX): Set to 1021.
(struct mf_thread_data): New.
(mf_thread_data, mf_thread_data_lock): New.
(__mf_allocate_blank_threadinfo): Remove.
(__mf_find_threadinfo): Rewrite and simplify. Only use if TLS is
not available.
(__mf_state_perthread): Remove.
(__mf_get_state, __mf_set_state): New.
(__mf_pthread_cleanup): Use &errno, rather than saved pointer.
Update mf_thread_data killing procedure.
(__mf_pthread_spawner): Similarly.
(__mf_0fn_pthread_create): Only use wrapper if necessary. Remove
code to allocate thread stack space.
(__mf_0fn_pthread_join, pthread_join): Remove.
(__mf_0fn_pthread_exit, pthread_exit): Remove.
* mf-impl.h (dyn_pthread_join, dyn_pthread_exit): Remove.
(__mf_state_1): Rename from __mf_state; use TLS when available.
(__mf_get_state, __mf_set_state): New. Update all users.
* mf-runtime.c (begin_recursion_protect1): New.
(BEGIN_RECURSION_PROTECT): Use it.
(__mf_state_1): Rename from __mf_state; use TLS when available.
(threads_active_p): Remove.
(__mf_usage): Compute it directly.
From-SVN: r102108
2005-07-17 04:29:00 +02:00
|
|
|
{
|
2006-11-10 19:42:28 +01:00
|
|
|
__mf_0fn_bufs_used[i] = 1;
|
|
|
|
return & __mf_0fn_bufs[i][0];
|
gcc.c (MFWRAP_SPEC): Don't wrap pthread_join or pthread_exit.
* gcc.c (MFWRAP_SPEC): Don't wrap pthread_join or pthread_exit.
* acinclude.m4: New file.
* configure.ac: Invoke LIBMUDFLAP_CHECK_TLS.
* configure, config.h.in, Makefile.in, testsuite/Makefile.in: Rebuild.
* mf-hooks1.c (__mf_0fn_malloc): Move body from ...
(__mf_0fn_calloc): ... here.
* mf-hooks3.c (struct pthread_info): Remove.
(__mf_pthread_info, __mf_pthread_info_idx): Remove.
(LIBMUDFLAPTH_THREADS_MAX): Set to 1021.
(struct mf_thread_data): New.
(mf_thread_data, mf_thread_data_lock): New.
(__mf_allocate_blank_threadinfo): Remove.
(__mf_find_threadinfo): Rewrite and simplify. Only use if TLS is
not available.
(__mf_state_perthread): Remove.
(__mf_get_state, __mf_set_state): New.
(__mf_pthread_cleanup): Use &errno, rather than saved pointer.
Update mf_thread_data killing procedure.
(__mf_pthread_spawner): Similarly.
(__mf_0fn_pthread_create): Only use wrapper if necessary. Remove
code to allocate thread stack space.
(__mf_0fn_pthread_join, pthread_join): Remove.
(__mf_0fn_pthread_exit, pthread_exit): Remove.
* mf-impl.h (dyn_pthread_join, dyn_pthread_exit): Remove.
(__mf_state_1): Rename from __mf_state; use TLS when available.
(__mf_get_state, __mf_set_state): New. Update all users.
* mf-runtime.c (begin_recursion_protect1): New.
(BEGIN_RECURSION_PROTECT): Use it.
(__mf_state_1): Rename from __mf_state; use TLS when available.
(threads_active_p): Remove.
(__mf_usage): Compute it directly.
From-SVN: r102108
2005-07-17 04:29:00 +02:00
|
|
|
}
|
|
|
|
}
|
2004-05-13 08:41:07 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-06-04 22:12:01 +02:00
|
|
|
|
2004-05-13 08:41:07 +02:00
|
|
|
#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);
|
|
|
|
|
2005-03-22 03:14:33 +01:00
|
|
|
size_with_crumple_zones =
|
2004-05-13 08:41:07 +02:00
|
|
|
CLAMPADD(c,CLAMPADD(__mf_opts.crumple_zone,
|
|
|
|
__mf_opts.crumple_zone));
|
2005-08-18 22:01:54 +02:00
|
|
|
BEGIN_MALLOC_PROTECT ();
|
2004-05-13 08:41:07 +02:00
|
|
|
result = (char *) CALL_REAL (malloc, size_with_crumple_zones);
|
2005-08-18 22:01:54 +02:00
|
|
|
END_MALLOC_PROTECT ();
|
2005-03-22 03:14:33 +01:00
|
|
|
|
2004-05-13 08:41:07 +02:00
|
|
|
if (LIKELY(result))
|
|
|
|
{
|
|
|
|
result += __mf_opts.crumple_zone;
|
|
|
|
__mf_register (result, c, __MF_TYPE_HEAP, "malloc region");
|
|
|
|
/* XXX: register __MF_TYPE_NOACCESS for crumple zones. */
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef PIC
|
|
|
|
/* A special bootstrap variant. */
|
|
|
|
void *
|
|
|
|
__mf_0fn_calloc (size_t c, size_t n)
|
|
|
|
{
|
gcc.c (MFWRAP_SPEC): Don't wrap pthread_join or pthread_exit.
* gcc.c (MFWRAP_SPEC): Don't wrap pthread_join or pthread_exit.
* acinclude.m4: New file.
* configure.ac: Invoke LIBMUDFLAP_CHECK_TLS.
* configure, config.h.in, Makefile.in, testsuite/Makefile.in: Rebuild.
* mf-hooks1.c (__mf_0fn_malloc): Move body from ...
(__mf_0fn_calloc): ... here.
* mf-hooks3.c (struct pthread_info): Remove.
(__mf_pthread_info, __mf_pthread_info_idx): Remove.
(LIBMUDFLAPTH_THREADS_MAX): Set to 1021.
(struct mf_thread_data): New.
(mf_thread_data, mf_thread_data_lock): New.
(__mf_allocate_blank_threadinfo): Remove.
(__mf_find_threadinfo): Rewrite and simplify. Only use if TLS is
not available.
(__mf_state_perthread): Remove.
(__mf_get_state, __mf_set_state): New.
(__mf_pthread_cleanup): Use &errno, rather than saved pointer.
Update mf_thread_data killing procedure.
(__mf_pthread_spawner): Similarly.
(__mf_0fn_pthread_create): Only use wrapper if necessary. Remove
code to allocate thread stack space.
(__mf_0fn_pthread_join, pthread_join): Remove.
(__mf_0fn_pthread_exit, pthread_exit): Remove.
* mf-impl.h (dyn_pthread_join, dyn_pthread_exit): Remove.
(__mf_state_1): Rename from __mf_state; use TLS when available.
(__mf_get_state, __mf_set_state): New. Update all users.
* mf-runtime.c (begin_recursion_protect1): New.
(BEGIN_RECURSION_PROTECT): Use it.
(__mf_state_1): Rename from __mf_state; use TLS when available.
(threads_active_p): Remove.
(__mf_usage): Compute it directly.
From-SVN: r102108
2005-07-17 04:29:00 +02:00
|
|
|
return __mf_0fn_malloc (c * n);
|
2004-05-13 08:41:07 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-06-04 22:12:01 +02:00
|
|
|
|
2004-05-13 08:41:07 +02:00
|
|
|
#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);
|
2005-03-22 03:14:33 +01:00
|
|
|
|
|
|
|
size_with_crumple_zones =
|
2004-05-13 08:41:07 +02:00
|
|
|
CLAMPADD((c * n), /* XXX: CLAMPMUL */
|
|
|
|
CLAMPADD(__mf_opts.crumple_zone,
|
2005-03-22 03:14:33 +01:00
|
|
|
__mf_opts.crumple_zone));
|
2005-08-18 22:01:54 +02:00
|
|
|
BEGIN_MALLOC_PROTECT ();
|
2004-05-13 08:41:07 +02:00
|
|
|
result = (char *) CALL_REAL (malloc, size_with_crumple_zones);
|
2005-08-18 22:01:54 +02:00
|
|
|
END_MALLOC_PROTECT ();
|
2005-03-22 03:14:33 +01:00
|
|
|
|
2004-05-13 08:41:07 +02:00
|
|
|
if (LIKELY(result))
|
|
|
|
memset (result, 0, size_with_crumple_zones);
|
2005-03-22 03:14:33 +01:00
|
|
|
|
2004-05-13 08:41:07 +02:00
|
|
|
if (LIKELY(result))
|
|
|
|
{
|
|
|
|
result += __mf_opts.crumple_zone;
|
|
|
|
__mf_register (result, c*n /* XXX: clamp */, __MF_TYPE_HEAP_I, "calloc region");
|
|
|
|
/* XXX: register __MF_TYPE_NOACCESS for crumple zones. */
|
|
|
|
}
|
2005-03-22 03:14:33 +01:00
|
|
|
|
2004-05-13 08:41:07 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if PIC
|
|
|
|
/* A special bootstrap variant. */
|
|
|
|
void *
|
|
|
|
__mf_0fn_realloc (void *buf, size_t c)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-06-04 22:12:01 +02:00
|
|
|
|
2004-05-13 08:41:07 +02:00
|
|
|
#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;
|
|
|
|
|
2005-03-22 03:14:33 +01:00
|
|
|
size_with_crumple_zones =
|
2004-05-13 08:41:07 +02:00
|
|
|
CLAMPADD(c, CLAMPADD(__mf_opts.crumple_zone,
|
|
|
|
__mf_opts.crumple_zone));
|
2005-08-18 22:01:54 +02:00
|
|
|
BEGIN_MALLOC_PROTECT ();
|
2004-05-13 08:41:07 +02:00
|
|
|
result = (char *) CALL_REAL (realloc, base, size_with_crumple_zones);
|
2005-08-18 22:01:54 +02:00
|
|
|
END_MALLOC_PROTECT ();
|
2004-05-13 08:41:07 +02:00
|
|
|
|
|
|
|
/* Ensure heap wiping doesn't occur during this peculiar
|
|
|
|
unregister/reregister pair. */
|
|
|
|
LOCKTH ();
|
gcc.c (MFWRAP_SPEC): Don't wrap pthread_join or pthread_exit.
* gcc.c (MFWRAP_SPEC): Don't wrap pthread_join or pthread_exit.
* acinclude.m4: New file.
* configure.ac: Invoke LIBMUDFLAP_CHECK_TLS.
* configure, config.h.in, Makefile.in, testsuite/Makefile.in: Rebuild.
* mf-hooks1.c (__mf_0fn_malloc): Move body from ...
(__mf_0fn_calloc): ... here.
* mf-hooks3.c (struct pthread_info): Remove.
(__mf_pthread_info, __mf_pthread_info_idx): Remove.
(LIBMUDFLAPTH_THREADS_MAX): Set to 1021.
(struct mf_thread_data): New.
(mf_thread_data, mf_thread_data_lock): New.
(__mf_allocate_blank_threadinfo): Remove.
(__mf_find_threadinfo): Rewrite and simplify. Only use if TLS is
not available.
(__mf_state_perthread): Remove.
(__mf_get_state, __mf_set_state): New.
(__mf_pthread_cleanup): Use &errno, rather than saved pointer.
Update mf_thread_data killing procedure.
(__mf_pthread_spawner): Similarly.
(__mf_0fn_pthread_create): Only use wrapper if necessary. Remove
code to allocate thread stack space.
(__mf_0fn_pthread_join, pthread_join): Remove.
(__mf_0fn_pthread_exit, pthread_exit): Remove.
* mf-impl.h (dyn_pthread_join, dyn_pthread_exit): Remove.
(__mf_state_1): Rename from __mf_state; use TLS when available.
(__mf_get_state, __mf_set_state): New. Update all users.
* mf-runtime.c (begin_recursion_protect1): New.
(BEGIN_RECURSION_PROTECT): Use it.
(__mf_state_1): Rename from __mf_state; use TLS when available.
(threads_active_p): Remove.
(__mf_usage): Compute it directly.
From-SVN: r102108
2005-07-17 04:29:00 +02:00
|
|
|
__mf_set_state (reentrant);
|
2004-05-13 08:41:07 +02:00
|
|
|
saved_wipe_heap = __mf_opts.wipe_heap;
|
|
|
|
__mf_opts.wipe_heap = 0;
|
|
|
|
|
|
|
|
if (LIKELY(buf))
|
2005-03-22 03:14:33 +01:00
|
|
|
__mfu_unregister (buf, 0, __MF_TYPE_HEAP_I);
|
2004-06-24 23:12:18 +02:00
|
|
|
/* NB: underlying region may have been __MF_TYPE_HEAP. */
|
2005-03-22 03:14:33 +01:00
|
|
|
|
2004-05-13 08:41:07 +02:00
|
|
|
if (LIKELY(result))
|
|
|
|
{
|
|
|
|
result += __mf_opts.crumple_zone;
|
|
|
|
__mfu_register (result, c, __MF_TYPE_HEAP_I, "realloc region");
|
|
|
|
/* XXX: register __MF_TYPE_NOACCESS for crumple zones. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore previous setting. */
|
|
|
|
__mf_opts.wipe_heap = saved_wipe_heap;
|
|
|
|
|
gcc.c (MFWRAP_SPEC): Don't wrap pthread_join or pthread_exit.
* gcc.c (MFWRAP_SPEC): Don't wrap pthread_join or pthread_exit.
* acinclude.m4: New file.
* configure.ac: Invoke LIBMUDFLAP_CHECK_TLS.
* configure, config.h.in, Makefile.in, testsuite/Makefile.in: Rebuild.
* mf-hooks1.c (__mf_0fn_malloc): Move body from ...
(__mf_0fn_calloc): ... here.
* mf-hooks3.c (struct pthread_info): Remove.
(__mf_pthread_info, __mf_pthread_info_idx): Remove.
(LIBMUDFLAPTH_THREADS_MAX): Set to 1021.
(struct mf_thread_data): New.
(mf_thread_data, mf_thread_data_lock): New.
(__mf_allocate_blank_threadinfo): Remove.
(__mf_find_threadinfo): Rewrite and simplify. Only use if TLS is
not available.
(__mf_state_perthread): Remove.
(__mf_get_state, __mf_set_state): New.
(__mf_pthread_cleanup): Use &errno, rather than saved pointer.
Update mf_thread_data killing procedure.
(__mf_pthread_spawner): Similarly.
(__mf_0fn_pthread_create): Only use wrapper if necessary. Remove
code to allocate thread stack space.
(__mf_0fn_pthread_join, pthread_join): Remove.
(__mf_0fn_pthread_exit, pthread_exit): Remove.
* mf-impl.h (dyn_pthread_join, dyn_pthread_exit): Remove.
(__mf_state_1): Rename from __mf_state; use TLS when available.
(__mf_get_state, __mf_set_state): New. Update all users.
* mf-runtime.c (begin_recursion_protect1): New.
(BEGIN_RECURSION_PROTECT): Use it.
(__mf_state_1): Rename from __mf_state; use TLS when available.
(threads_active_p): Remove.
(__mf_usage): Compute it directly.
From-SVN: r102108
2005-07-17 04:29:00 +02:00
|
|
|
__mf_set_state (active);
|
2004-05-13 08:41:07 +02:00
|
|
|
UNLOCKTH ();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if PIC
|
|
|
|
/* A special bootstrap variant. */
|
|
|
|
void
|
|
|
|
__mf_0fn_free (void *buf)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef free
|
|
|
|
WRAPPER(void, free, void *buf)
|
|
|
|
{
|
|
|
|
/* Use a circular queue to delay some number (__mf_opts.free_queue_length) of free()s. */
|
|
|
|
static void *free_queue [__MF_FREEQ_MAX];
|
|
|
|
static unsigned free_ptr = 0;
|
|
|
|
static int freeq_initialized = 0;
|
2005-03-22 03:14:33 +01:00
|
|
|
DECLARE(void, free, void *);
|
|
|
|
|
2004-05-13 08:41:07 +02:00
|
|
|
BEGIN_PROTECT (free, buf);
|
|
|
|
|
|
|
|
if (UNLIKELY(buf == NULL))
|
|
|
|
return;
|
|
|
|
|
2006-11-10 19:42:28 +01:00
|
|
|
#if PIC
|
|
|
|
/* Check whether the given buffer might have come from a
|
|
|
|
__mf_0fn_malloc/calloc call that for whatever reason was not
|
|
|
|
redirected back to __mf_0fn_free. If so, we just ignore the
|
|
|
|
call. */
|
|
|
|
if (UNLIKELY((uintptr_t) buf >= (uintptr_t) __mf_0fn_bufs &&
|
|
|
|
(uintptr_t) buf < ((uintptr_t) __mf_0fn_bufs + sizeof(__mf_0fn_bufs))))
|
|
|
|
{
|
|
|
|
VERBOSE_TRACE ("skipping free of boot (0fn) alloc buffer %p\n", buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-05-13 08:41:07 +02:00
|
|
|
LOCKTH ();
|
|
|
|
if (UNLIKELY(!freeq_initialized))
|
|
|
|
{
|
2005-03-22 03:14:33 +01:00
|
|
|
memset (free_queue, 0,
|
2004-05-13 08:41:07 +02:00
|
|
|
__MF_FREEQ_MAX * sizeof (void *));
|
|
|
|
freeq_initialized = 1;
|
|
|
|
}
|
|
|
|
UNLOCKTH ();
|
|
|
|
|
2004-06-24 23:12:18 +02:00
|
|
|
__mf_unregister (buf, 0, __MF_TYPE_HEAP_I);
|
|
|
|
/* NB: underlying region may have been __MF_TYPE_HEAP. */
|
2004-05-13 08:41:07 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2005-03-22 03:14:33 +01:00
|
|
|
VERBOSE_TRACE ("freeing deferred pointer %p (crumple %u)\n",
|
2004-05-13 08:41:07 +02:00
|
|
|
(void *) freeme,
|
|
|
|
__mf_opts.crumple_zone);
|
|
|
|
}
|
2005-08-18 22:01:54 +02:00
|
|
|
BEGIN_MALLOC_PROTECT ();
|
2004-05-13 08:41:07 +02:00
|
|
|
CALL_REAL (free, freeme);
|
2005-08-18 22:01:54 +02:00
|
|
|
END_MALLOC_PROTECT ();
|
2004-05-13 08:41:07 +02:00
|
|
|
}
|
2005-03-22 03:14:33 +01:00
|
|
|
}
|
|
|
|
else
|
2004-05-13 08:41:07 +02:00
|
|
|
{
|
|
|
|
/* back pointer up a bit to the beginning of crumple zone */
|
|
|
|
char *base = (char *)buf;
|
|
|
|
base -= __mf_opts.crumple_zone;
|
|
|
|
if (__mf_opts.trace_mf_calls)
|
|
|
|
{
|
|
|
|
VERBOSE_TRACE ("freeing pointer %p = %p - %u\n",
|
2005-03-22 03:14:33 +01:00
|
|
|
(void *) base,
|
|
|
|
(void *) buf,
|
2004-05-13 08:41:07 +02:00
|
|
|
__mf_opts.crumple_zone);
|
|
|
|
}
|
2005-08-18 22:01:54 +02:00
|
|
|
BEGIN_MALLOC_PROTECT ();
|
2004-05-13 08:41:07 +02:00
|
|
|
CALL_REAL (free, base);
|
2005-08-18 22:01:54 +02:00
|
|
|
END_MALLOC_PROTECT ();
|
2004-05-13 08:41:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-30 03:50:56 +02:00
|
|
|
/* We can only wrap mmap if the target supports it. Likewise for munmap.
|
|
|
|
We assume we have both if we have mmap. */
|
|
|
|
#ifdef HAVE_MMAP
|
|
|
|
|
2004-05-13 08:41:07 +02:00
|
|
|
#if PIC
|
|
|
|
/* A special bootstrap variant. */
|
|
|
|
void *
|
|
|
|
__mf_0fn_mmap (void *start, size_t l, int prot, int f, int fd, off_t off)
|
|
|
|
{
|
|
|
|
return (void *) -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#undef mmap
|
2005-03-22 03:14:33 +01:00
|
|
|
WRAPPER(void *, mmap,
|
|
|
|
void *start, size_t length, int prot,
|
2004-05-13 08:41:07 +02:00
|
|
|
int flags, int fd, off_t offset)
|
|
|
|
{
|
2005-03-22 03:14:33 +01:00
|
|
|
DECLARE(void *, mmap, void *, size_t, int,
|
2004-05-13 08:41:07 +02:00
|
|
|
int, int, off_t);
|
|
|
|
void *result;
|
|
|
|
BEGIN_PROTECT (mmap, start, length, prot, flags, fd, offset);
|
|
|
|
|
2005-03-22 03:14:33 +01:00
|
|
|
result = CALL_REAL (mmap, start, length, prot,
|
2004-05-13 08:41:07 +02:00
|
|
|
flags, fd, offset);
|
|
|
|
|
|
|
|
/*
|
2005-03-22 03:14:33 +01:00
|
|
|
VERBOSE_TRACE ("mmap (%08lx, %08lx, ...) => %08lx\n",
|
2004-05-13 08:41:07 +02:00
|
|
|
(uintptr_t) start, (uintptr_t) length,
|
|
|
|
(uintptr_t) result);
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (result != (void *)-1)
|
|
|
|
{
|
|
|
|
/* Register each page as a heap object. Why not register it all
|
|
|
|
as a single segment? That's so that a later munmap() call
|
|
|
|
can unmap individual pages. XXX: would __MF_TYPE_GUESS make
|
|
|
|
this more automatic? */
|
|
|
|
size_t ps = getpagesize ();
|
|
|
|
uintptr_t base = (uintptr_t) result;
|
|
|
|
uintptr_t offset;
|
|
|
|
|
|
|
|
for (offset=0; offset<length; offset+=ps)
|
|
|
|
{
|
|
|
|
/* XXX: We could map PROT_NONE to __MF_TYPE_NOACCESS. */
|
|
|
|
/* XXX: Unaccessed HEAP pages are reported as leaks. Is this
|
|
|
|
appropriate for unaccessed mmap pages? */
|
|
|
|
__mf_register ((void *) CLAMPADD (base, offset), ps,
|
|
|
|
__MF_TYPE_HEAP_I, "mmap page");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if PIC
|
|
|
|
/* A special bootstrap variant. */
|
|
|
|
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);
|
2005-03-22 03:14:33 +01:00
|
|
|
|
2004-05-13 08:41:07 +02:00
|
|
|
result = CALL_REAL (munmap, start, length);
|
|
|
|
|
|
|
|
/*
|
2005-03-22 03:14:33 +01:00
|
|
|
VERBOSE_TRACE ("munmap (%08lx, %08lx, ...) => %08lx\n",
|
2004-05-13 08:41:07 +02:00
|
|
|
(uintptr_t) start, (uintptr_t) length,
|
|
|
|
(uintptr_t) result);
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (result == 0)
|
|
|
|
{
|
|
|
|
/* Unregister each page as a heap object. */
|
|
|
|
size_t ps = getpagesize ();
|
|
|
|
uintptr_t base = (uintptr_t) start & (~ (ps - 1)); /* page align */
|
|
|
|
uintptr_t offset;
|
|
|
|
|
|
|
|
for (offset=0; offset<length; offset+=ps)
|
2004-06-24 23:12:18 +02:00
|
|
|
__mf_unregister ((void *) CLAMPADD (base, offset), ps, __MF_TYPE_HEAP_I);
|
2004-05-13 08:41:07 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2005-09-30 03:50:56 +02:00
|
|
|
#endif /* HAVE_MMAP */
|
2004-05-13 08:41:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* This wrapper is a little different, as it's called indirectly from
|
2005-03-22 03:14:33 +01:00
|
|
|
__mf_fini also to clean up pending allocations. */
|
2004-05-13 08:41:07 +02:00
|
|
|
void *
|
|
|
|
__mf_wrap_alloca_indirect (size_t c)
|
|
|
|
{
|
|
|
|
DECLARE (void *, malloc, size_t);
|
|
|
|
DECLARE (void, free, void *);
|
|
|
|
|
|
|
|
/* This struct, a linked list, tracks alloca'd objects. The newest
|
|
|
|
object is at the head of the list. If we detect that we've
|
|
|
|
popped a few levels of stack, then the listed objects are freed
|
|
|
|
as needed. NB: The tracking struct is allocated with
|
|
|
|
real_malloc; the user data with wrap_malloc.
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
|
|
|
|
/* XXX: thread locking! */
|
|
|
|
|
|
|
|
/* Free any previously alloca'd blocks that belong to deeper-nested functions,
|
|
|
|
which must therefore have exited by now. */
|
2004-06-04 22:12:01 +02:00
|
|
|
|
|
|
|
#define DEEPER_THAN < /* XXX: for x86; steal find_stack_direction() from libiberty/alloca.c */
|
|
|
|
|
2004-05-13 08:41:07 +02:00
|
|
|
while (alloca_history &&
|
|
|
|
((uintptr_t) alloca_history->stack DEEPER_THAN (uintptr_t) stack))
|
|
|
|
{
|
|
|
|
struct alloca_tracking *next = alloca_history->next;
|
2004-06-24 23:12:18 +02:00
|
|
|
__mf_unregister (alloca_history->ptr, 0, __MF_TYPE_HEAP);
|
2005-08-18 22:01:54 +02:00
|
|
|
BEGIN_MALLOC_PROTECT ();
|
2004-05-13 08:41:07 +02:00
|
|
|
CALL_REAL (free, alloca_history->ptr);
|
|
|
|
CALL_REAL (free, alloca_history);
|
2005-08-18 22:01:54 +02:00
|
|
|
END_MALLOC_PROTECT ();
|
2004-05-13 08:41:07 +02:00
|
|
|
alloca_history = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate new block. */
|
|
|
|
result = NULL;
|
|
|
|
if (LIKELY (c > 0)) /* alloca(0) causes no allocation. */
|
|
|
|
{
|
2005-08-18 22:01:54 +02:00
|
|
|
BEGIN_MALLOC_PROTECT ();
|
2005-03-22 03:14:33 +01:00
|
|
|
track = (struct alloca_tracking *) CALL_REAL (malloc,
|
2004-05-13 08:41:07 +02:00
|
|
|
sizeof (struct alloca_tracking));
|
2005-08-18 22:01:54 +02:00
|
|
|
END_MALLOC_PROTECT ();
|
2004-05-13 08:41:07 +02:00
|
|
|
if (LIKELY (track != NULL))
|
|
|
|
{
|
2005-08-18 22:01:54 +02:00
|
|
|
BEGIN_MALLOC_PROTECT ();
|
2004-05-13 08:41:07 +02:00
|
|
|
result = CALL_REAL (malloc, c);
|
2005-08-18 22:01:54 +02:00
|
|
|
END_MALLOC_PROTECT ();
|
2004-05-13 08:41:07 +02:00
|
|
|
if (UNLIKELY (result == NULL))
|
|
|
|
{
|
2005-08-18 22:01:54 +02:00
|
|
|
BEGIN_MALLOC_PROTECT ();
|
2004-05-13 08:41:07 +02:00
|
|
|
CALL_REAL (free, track);
|
2005-08-18 22:01:54 +02:00
|
|
|
END_MALLOC_PROTECT ();
|
2004-05-13 08:41:07 +02:00
|
|
|
/* Too bad. XXX: What about errno? */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
__mf_register (result, c, __MF_TYPE_HEAP, "alloca region");
|
|
|
|
track->ptr = result;
|
|
|
|
track->stack = stack;
|
|
|
|
track->next = alloca_history;
|
|
|
|
alloca_history = track;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-03-22 03:14:33 +01:00
|
|
|
|
2004-05-13 08:41:07 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef alloca
|
|
|
|
WRAPPER(void *, alloca, size_t c)
|
|
|
|
{
|
|
|
|
return __mf_wrap_alloca_indirect (c);
|
|
|
|
}
|
|
|
|
|