From: Paul Eggert Date: Thu, 29 Sep 2011 07:44:58 +0000 (-0700) Subject: * alloc.c: Do not assume sizeof (size_t) is a multiple of 8 X-Git-Tag: emacs-24.2.90~471^2~6^2~172 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=38532ce6ecb33efee5f697886d9fd20a3c2cda01;p=emacs.git * 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. --- diff --git a/src/ChangeLog b/src/ChangeLog index 4f0a0b9ab74..37f2e39848f 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,13 @@ +2011-09-29 Paul Eggert + + * 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 * alloc.c (pure_bytes_used_lisp, pure_bytes_used_non_lisp): diff --git a/src/alloc.c b/src/alloc.c index 6bcb216bb5e..286421ebe47 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -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 }