Commit 38532ce6 authored by Paul Eggert's avatar Paul Eggert
Browse files

* alloc.c: Do not assume sizeof (size_t) is a multiple of 8

when debugging.
(alignof, XMALLOC_BASE_ALIGNMENT, XMALLOC_HEADER_ALIGNMENT)
(XMALLOC_OVERRUN_SIZE_SIZE): New macros.
(XMALLOC_OVERRUN_CHECK_OVERHEAD, xmalloc_put_size, xmalloc_get_size)
(overrun_check_malloc, overrun_check_realloc, overrun_check_free):
Replace uses of sizeof (size_t) with XMALLOC_OVERRUN_SIZE_SIZE.
parent b13f71e2
2011-09-29 Paul Eggert <eggert@cs.ucla.edu>
* alloc.c: Do not assume sizeof (size_t) is a multiple of 8
when debugging.
(alignof, XMALLOC_BASE_ALIGNMENT, XMALLOC_HEADER_ALIGNMENT)
(XMALLOC_OVERRUN_SIZE_SIZE): New macros.
(XMALLOC_OVERRUN_CHECK_OVERHEAD, xmalloc_put_size, xmalloc_get_size)
(overrun_check_malloc, overrun_check_realloc, overrun_check_free):
Replace uses of sizeof (size_t) with XMALLOC_OVERRUN_SIZE_SIZE.
2011-09-28 Paul Eggert <eggert@cs.ucla.edu>
* alloc.c (pure_bytes_used_lisp, pure_bytes_used_non_lisp):
......
......@@ -490,9 +490,9 @@ buffer_memory_full (ptrdiff_t nbytes)
/* Check for overrun in malloc'ed buffers by wrapping a header and trailer
around each block.
The header consists of 16 fixed bytes followed by sizeof (size_t) bytes
containing the original block size in little-endian order,
while the trailer consists of 16 fixed bytes.
The header consists of 16 fixed bytes followed by
XMALLOC_OVERRUN_SIZE_SIZE bytes containing the original block size
in little-endian order. The trailer consists of 16 fixed bytes.
The header is used to detect whether this block has been allocated
through these functions -- as it seems that some low-level libc
......@@ -502,7 +502,24 @@ buffer_memory_full (ptrdiff_t nbytes)
#define XMALLOC_OVERRUN_CHECK_SIZE 16
#define XMALLOC_OVERRUN_CHECK_OVERHEAD \
(2 * XMALLOC_OVERRUN_CHECK_SIZE + sizeof (size_t))
(2 * XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE)
/* Define XMALLOC_OVERRUN_SIZE_SIZE so that (1) it's large enough to
hold a size_t value and (2) so that the header size is a multiple
of the alignment that Emacs needs. */
#define alignof(type) offsetof (struct { char c; type x; }, x)
#define XMALLOC_BASE_ALIGNMENT \
max (max (alignof (double), alignof (long double)), alignof (intmax_t))
#ifdef USE_LSB_TAG
# define XMALLOC_HEADER_ALIGNMENT max (1 << GCTYPEBITS, XMALLOC_BASE_ALIGNMENT)
#else
# define XMALLOC_HEADER_ALIGNMENT XMALLOC_BASE_ALIGNMENT
#endif
#define XMALLOC_OVERRUN_SIZE_SIZE \
(((XMALLOC_OVERRUN_CHECK_SIZE + sizeof (size_t) \
+ XMALLOC_HEADER_ALIGNMENT - 1) \
/ XMALLOC_HEADER_ALIGNMENT * XMALLOC_HEADER_ALIGNMENT) \
- XMALLOC_OVERRUN_CHECK_SIZE)
static char const xmalloc_overrun_check_header[XMALLOC_OVERRUN_CHECK_SIZE] =
{ '\x9a', '\x9b', '\xae', '\xaf',
......@@ -522,9 +539,9 @@ static void
xmalloc_put_size (unsigned char *ptr, size_t size)
{
int i;
for (i = 0; i < sizeof (size_t); i++)
for (i = 0; i < XMALLOC_OVERRUN_SIZE_SIZE; i++)
{
*--ptr = size & (1 << CHAR_BIT) - 1;
*--ptr = size & ((1 << CHAR_BIT) - 1);
size >>= CHAR_BIT;
}
}
......@@ -534,8 +551,8 @@ xmalloc_get_size (unsigned char *ptr)
{
size_t size = 0;
int i;
ptr -= sizeof (size_t);
for (i = 0; i < sizeof (size_t); i++)
ptr -= XMALLOC_OVERRUN_SIZE_SIZE;
for (i = 0; i < XMALLOC_OVERRUN_SIZE_SIZE; i++)
{
size <<= CHAR_BIT;
size += *ptr++;
......@@ -579,7 +596,7 @@ overrun_check_malloc (size_t size)
if (val && check_depth == 1)
{
memcpy (val, xmalloc_overrun_check_header, XMALLOC_OVERRUN_CHECK_SIZE);
val += XMALLOC_OVERRUN_CHECK_SIZE + sizeof (size_t);
val += XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE;
xmalloc_put_size (val, size);
memcpy (val + size, xmalloc_overrun_check_trailer,
XMALLOC_OVERRUN_CHECK_SIZE);
......@@ -603,7 +620,7 @@ overrun_check_realloc (POINTER_TYPE *block, size_t size)
if (val
&& check_depth == 1
&& memcmp (xmalloc_overrun_check_header,
val - XMALLOC_OVERRUN_CHECK_SIZE - sizeof (size_t),
val - XMALLOC_OVERRUN_CHECK_SIZE - XMALLOC_OVERRUN_SIZE_SIZE,
XMALLOC_OVERRUN_CHECK_SIZE) == 0)
{
size_t osize = xmalloc_get_size (val);
......@@ -611,8 +628,8 @@ overrun_check_realloc (POINTER_TYPE *block, size_t size)
XMALLOC_OVERRUN_CHECK_SIZE))
abort ();
memset (val + osize, 0, XMALLOC_OVERRUN_CHECK_SIZE);
val -= XMALLOC_OVERRUN_CHECK_SIZE + sizeof (size_t);
memset (val, 0, XMALLOC_OVERRUN_CHECK_SIZE + sizeof (size_t));
val -= XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE;
memset (val, 0, XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE);
}
val = (unsigned char *) realloc ((POINTER_TYPE *)val, size + overhead);
......@@ -620,7 +637,7 @@ overrun_check_realloc (POINTER_TYPE *block, size_t size)
if (val && check_depth == 1)
{
memcpy (val, xmalloc_overrun_check_header, XMALLOC_OVERRUN_CHECK_SIZE);
val += XMALLOC_OVERRUN_CHECK_SIZE + sizeof (size_t);
val += XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE;
xmalloc_put_size (val, size);
memcpy (val + size, xmalloc_overrun_check_trailer,
XMALLOC_OVERRUN_CHECK_SIZE);
......@@ -640,7 +657,7 @@ overrun_check_free (POINTER_TYPE *block)
if (val
&& check_depth == 1
&& memcmp (xmalloc_overrun_check_header,
val - XMALLOC_OVERRUN_CHECK_SIZE - sizeof (size_t),
val - XMALLOC_OVERRUN_CHECK_SIZE - XMALLOC_OVERRUN_SIZE_SIZE,
XMALLOC_OVERRUN_CHECK_SIZE) == 0)
{
size_t osize = xmalloc_get_size (val);
......@@ -648,12 +665,12 @@ overrun_check_free (POINTER_TYPE *block)
XMALLOC_OVERRUN_CHECK_SIZE))
abort ();
#ifdef XMALLOC_CLEAR_FREE_MEMORY
val -= XMALLOC_OVERRUN_CHECK_SIZE + sizeof (size_t);
val -= XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE;
memset (val, 0xff, osize + XMALLOC_OVERRUN_CHECK_OVERHEAD);
#else
memset (val + osize, 0, XMALLOC_OVERRUN_CHECK_SIZE);
val -= XMALLOC_OVERRUN_CHECK_SIZE + sizeof (size_t);
memset (val, 0, XMALLOC_OVERRUN_CHECK_SIZE + sizeof (size_t));
val -= XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE;
memset (val, 0, XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE);
#endif
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment