2021-07-22 13:07:59 +00:00
|
|
|
/* Compatibility code for malloc debugging and state management.
|
2021-01-02 19:32:25 +00:00
|
|
|
Copyright (C) 2001-2021 Free Software Foundation, Inc.
|
2002-01-29 07:54:51 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
Contributed by Wolfram Gloger <wg@malloc.de>, 2001.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2002-08-26 22:40:48 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public License as
|
|
|
|
published by the Free Software Foundation; either version 2.1 of the
|
2002-01-29 07:54:51 +00:00
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library 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
|
2002-08-26 22:40:48 +00:00
|
|
|
Lesser General Public License for more details.
|
2002-01-29 07:54:51 +00:00
|
|
|
|
2002-08-26 22:40:48 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-09 23:18:22 +00:00
|
|
|
License along with the GNU C Library; see the file COPYING.LIB. If
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
|
|
|
not, see <https://www.gnu.org/licenses/>. */
|
2002-01-29 07:54:51 +00:00
|
|
|
|
2021-07-22 13:07:59 +00:00
|
|
|
#ifndef weak_variable
|
|
|
|
# define weak_variable weak_function
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_24)
|
|
|
|
void (*__malloc_initialize_hook) (void);
|
|
|
|
compat_symbol (libc, __malloc_initialize_hook,
|
|
|
|
__malloc_initialize_hook, GLIBC_2_0);
|
|
|
|
#endif
|
|
|
|
|
2021-07-22 13:07:54 +00:00
|
|
|
#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_34)
|
|
|
|
void weak_variable (*__after_morecore_hook) (void) = NULL;
|
|
|
|
compat_symbol (libc, __after_morecore_hook, __after_morecore_hook, GLIBC_2_0);
|
2021-07-22 13:07:57 +00:00
|
|
|
void *(*__morecore)(ptrdiff_t);
|
|
|
|
compat_symbol (libc, __morecore, __morecore, GLIBC_2_0);
|
2021-07-22 13:07:54 +00:00
|
|
|
#endif
|
|
|
|
|
2021-07-22 13:07:59 +00:00
|
|
|
static void *malloc_hook_ini (size_t, const void *) __THROW;
|
|
|
|
static void *realloc_hook_ini (void *, size_t, const void *) __THROW;
|
|
|
|
static void *memalign_hook_ini (size_t, size_t, const void *) __THROW;
|
|
|
|
|
|
|
|
void weak_variable (*__free_hook) (void *, const void *) = NULL;
|
|
|
|
void *weak_variable (*__malloc_hook)
|
|
|
|
(size_t, const void *) = malloc_hook_ini;
|
|
|
|
void *weak_variable (*__realloc_hook)
|
|
|
|
(void *, size_t, const void *) = realloc_hook_ini;
|
|
|
|
void *weak_variable (*__memalign_hook)
|
|
|
|
(size_t, size_t, const void *) = memalign_hook_ini;
|
|
|
|
|
2002-01-29 07:54:51 +00:00
|
|
|
/* Hooks for debugging versions. The initial hooks just call the
|
|
|
|
initialization routine, then do the normal work. */
|
|
|
|
|
2021-07-22 13:07:59 +00:00
|
|
|
/* These hooks will get executed only through the interposed allocator
|
|
|
|
functions in libc_malloc_debug.so. This means that the calls to malloc,
|
|
|
|
realloc, etc. will lead back into the interposed functions, which is what we
|
|
|
|
want.
|
|
|
|
|
|
|
|
These initial hooks are assumed to be called in a single-threaded context,
|
|
|
|
so it is safe to reset all hooks at once upon initialization. */
|
|
|
|
|
|
|
|
static void
|
|
|
|
generic_hook_ini (void)
|
2002-01-29 07:54:51 +00:00
|
|
|
{
|
|
|
|
__malloc_hook = NULL;
|
2021-07-22 13:07:59 +00:00
|
|
|
__realloc_hook = NULL;
|
|
|
|
__memalign_hook = NULL;
|
2014-01-02 08:38:18 +00:00
|
|
|
ptmalloc_init ();
|
2021-07-22 13:07:59 +00:00
|
|
|
|
|
|
|
#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_24)
|
|
|
|
void (*hook) (void) = atomic_forced_read (__malloc_initialize_hook);
|
|
|
|
if (hook != NULL)
|
|
|
|
(*hook)();
|
|
|
|
#endif
|
|
|
|
__malloc_initialized = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
malloc_hook_ini (size_t sz, const void *caller)
|
|
|
|
{
|
|
|
|
generic_hook_ini ();
|
|
|
|
return malloc (sz);
|
2002-01-29 07:54:51 +00:00
|
|
|
}
|
|
|
|
|
2014-01-02 08:38:18 +00:00
|
|
|
static void *
|
|
|
|
realloc_hook_ini (void *ptr, size_t sz, const void *caller)
|
2002-01-29 07:54:51 +00:00
|
|
|
{
|
2021-07-22 13:07:59 +00:00
|
|
|
generic_hook_ini ();
|
|
|
|
return realloc (ptr, sz);
|
2002-01-29 07:54:51 +00:00
|
|
|
}
|
|
|
|
|
2014-01-02 08:38:18 +00:00
|
|
|
static void *
|
|
|
|
memalign_hook_ini (size_t alignment, size_t sz, const void *caller)
|
2002-01-29 07:54:51 +00:00
|
|
|
{
|
2021-07-22 13:07:59 +00:00
|
|
|
generic_hook_ini ();
|
|
|
|
return memalign (alignment, sz);
|
2002-01-29 07:54:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 19:18:12 +00:00
|
|
|
#include "malloc-check.c"
|
2002-01-29 07:54:51 +00:00
|
|
|
|
2016-10-26 11:28:28 +00:00
|
|
|
#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_25)
|
2002-01-29 07:54:51 +00:00
|
|
|
|
2018-06-29 12:54:59 +00:00
|
|
|
/* Support for restoring dumped heaps contained in historic Emacs
|
|
|
|
executables. The heap saving feature (malloc_get_state) is no
|
|
|
|
longer implemented in this version of glibc, but we have a heap
|
|
|
|
rewriter in malloc_set_state which transforms the heap into a
|
|
|
|
version compatible with current malloc. */
|
2002-01-29 07:54:51 +00:00
|
|
|
|
|
|
|
#define MALLOC_STATE_MAGIC 0x444c4541l
|
2016-05-24 06:05:15 +00:00
|
|
|
#define MALLOC_STATE_VERSION (0 * 0x100l + 5l) /* major*0x100 + minor */
|
2014-01-02 08:38:18 +00:00
|
|
|
|
|
|
|
struct malloc_save_state
|
|
|
|
{
|
|
|
|
long magic;
|
|
|
|
long version;
|
|
|
|
mbinptr av[NBINS * 2 + 2];
|
|
|
|
char *sbrk_base;
|
|
|
|
int sbrked_mem_bytes;
|
2002-01-29 07:54:51 +00:00
|
|
|
unsigned long trim_threshold;
|
|
|
|
unsigned long top_pad;
|
2014-01-02 08:38:18 +00:00
|
|
|
unsigned int n_mmaps_max;
|
2002-01-29 07:54:51 +00:00
|
|
|
unsigned long mmap_threshold;
|
2014-01-02 08:38:18 +00:00
|
|
|
int check_action;
|
2002-01-29 07:54:51 +00:00
|
|
|
unsigned long max_sbrked_mem;
|
2016-02-19 16:07:04 +00:00
|
|
|
unsigned long max_total_mem; /* Always 0, for backwards compatibility. */
|
2014-01-02 08:38:18 +00:00
|
|
|
unsigned int n_mmaps;
|
|
|
|
unsigned int max_n_mmaps;
|
2002-01-29 07:54:51 +00:00
|
|
|
unsigned long mmapped_mem;
|
|
|
|
unsigned long max_mmapped_mem;
|
2014-01-02 08:38:18 +00:00
|
|
|
int using_malloc_checking;
|
2009-04-08 18:00:34 +00:00
|
|
|
unsigned long max_fast;
|
|
|
|
unsigned long arena_test;
|
|
|
|
unsigned long arena_max;
|
|
|
|
unsigned long narenas;
|
2002-01-29 07:54:51 +00:00
|
|
|
};
|
|
|
|
|
2016-10-26 11:28:28 +00:00
|
|
|
/* Dummy implementation which always fails. We need to provide this
|
|
|
|
symbol so that existing Emacs binaries continue to work with
|
|
|
|
BIND_NOW. */
|
2014-01-02 08:38:18 +00:00
|
|
|
void *
|
2016-10-26 11:28:28 +00:00
|
|
|
attribute_compat_text_section
|
|
|
|
malloc_get_state (void)
|
2002-01-29 07:54:51 +00:00
|
|
|
{
|
2016-10-26 11:28:28 +00:00
|
|
|
__set_errno (ENOSYS);
|
|
|
|
return NULL;
|
2002-01-29 07:54:51 +00:00
|
|
|
}
|
2016-10-26 11:28:28 +00:00
|
|
|
compat_symbol (libc, malloc_get_state, malloc_get_state, GLIBC_2_0);
|
2002-01-29 07:54:51 +00:00
|
|
|
|
|
|
|
int
|
2016-10-26 11:28:28 +00:00
|
|
|
attribute_compat_text_section
|
|
|
|
malloc_set_state (void *msptr)
|
2002-01-29 07:54:51 +00:00
|
|
|
{
|
2014-01-02 08:38:18 +00:00
|
|
|
struct malloc_save_state *ms = (struct malloc_save_state *) msptr;
|
2002-01-29 07:54:51 +00:00
|
|
|
|
2014-01-02 08:38:18 +00:00
|
|
|
if (ms->magic != MALLOC_STATE_MAGIC)
|
|
|
|
return -1;
|
|
|
|
|
2002-01-29 07:54:51 +00:00
|
|
|
/* Must fail if the major version is too high. */
|
2014-01-02 08:38:18 +00:00
|
|
|
if ((ms->version & ~0xffl) > (MALLOC_STATE_VERSION & ~0xffl))
|
|
|
|
return -2;
|
|
|
|
|
2018-06-29 12:54:59 +00:00
|
|
|
/* We do not need to perform locking here because malloc_set_state
|
2016-05-13 12:16:39 +00:00
|
|
|
must be called before the first call into the malloc subsytem
|
|
|
|
(usually via __malloc_initialize_hook). pthread_create always
|
|
|
|
calls calloc and thus must be called only afterwards, so there
|
|
|
|
cannot be more than one thread when we reach this point. */
|
|
|
|
|
|
|
|
/* Disable the malloc hooks (and malloc checking). */
|
|
|
|
__malloc_hook = NULL;
|
|
|
|
__realloc_hook = NULL;
|
|
|
|
__free_hook = NULL;
|
|
|
|
__memalign_hook = NULL;
|
|
|
|
using_malloc_checking = 0;
|
|
|
|
|
|
|
|
/* Patch the dumped heap. We no longer try to integrate into the
|
|
|
|
existing heap. Instead, we mark the existing chunks as mmapped.
|
|
|
|
Together with the update to dumped_main_arena_start and
|
|
|
|
dumped_main_arena_end, realloc and free will recognize these
|
|
|
|
chunks as dumped fake mmapped chunks and never free them. */
|
|
|
|
|
|
|
|
/* Find the chunk with the lowest address with the heap. */
|
|
|
|
mchunkptr chunk = NULL;
|
|
|
|
{
|
|
|
|
size_t *candidate = (size_t *) ms->sbrk_base;
|
|
|
|
size_t *end = (size_t *) (ms->sbrk_base + ms->sbrked_mem_bytes);
|
|
|
|
while (candidate < end)
|
|
|
|
if (*candidate != 0)
|
|
|
|
{
|
|
|
|
chunk = mem2chunk ((void *) (candidate + 1));
|
|
|
|
break;
|
|
|
|
}
|
2014-01-02 08:38:18 +00:00
|
|
|
else
|
2016-05-13 12:16:39 +00:00
|
|
|
++candidate;
|
|
|
|
}
|
|
|
|
if (chunk == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Iterate over the dumped heap and patch the chunks so that they
|
|
|
|
are treated as fake mmapped chunks. */
|
|
|
|
mchunkptr top = ms->av[2];
|
|
|
|
while (chunk < top)
|
2014-01-02 08:38:18 +00:00
|
|
|
{
|
2016-05-13 12:16:39 +00:00
|
|
|
if (inuse (chunk))
|
|
|
|
{
|
|
|
|
/* Mark chunk as mmapped, to trigger the fallback path. */
|
|
|
|
size_t size = chunksize (chunk);
|
|
|
|
set_head (chunk, size | IS_MMAPPED);
|
|
|
|
}
|
|
|
|
chunk = next_chunk (chunk);
|
2014-01-02 08:38:18 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 12:16:39 +00:00
|
|
|
/* The dumped fake mmapped chunks all lie in this address range. */
|
|
|
|
dumped_main_arena_start = (mchunkptr) ms->sbrk_base;
|
|
|
|
dumped_main_arena_end = top;
|
|
|
|
|
2002-01-29 07:54:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-10-26 11:28:28 +00:00
|
|
|
compat_symbol (libc, malloc_set_state, malloc_set_state, GLIBC_2_0);
|
|
|
|
|
|
|
|
#endif /* SHLIB_COMPAT */
|
2002-01-29 07:54:51 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 2
|
|
|
|
* End:
|
|
|
|
*/
|