diff options
Diffstat (limited to 'src/alloc.c')
| -rw-r--r-- | src/alloc.c | 47 |
1 files changed, 45 insertions, 2 deletions
diff --git a/src/alloc.c b/src/alloc.c index 4c5094b8f48..ead5c8a8ca4 100644 --- a/src/alloc.c +++ b/src/alloc.c | |||
| @@ -482,10 +482,53 @@ buffer_memory_full (EMACS_INT nbytes) | |||
| 482 | xsignal (Qnil, Vmemory_signal_data); | 482 | xsignal (Qnil, Vmemory_signal_data); |
| 483 | } | 483 | } |
| 484 | 484 | ||
| 485 | #ifdef XMALLOC_OVERRUN_CHECK | 485 | |
| 486 | #ifndef XMALLOC_OVERRUN_CHECK | ||
| 487 | #define XMALLOC_OVERRUN_CHECK_OVERHEAD 0 | ||
| 488 | #else | ||
| 486 | 489 | ||
| 487 | /* Check for overrun in malloc'ed buffers by wrapping a header and trailer | 490 | /* Check for overrun in malloc'ed buffers by wrapping a header and trailer |
| 488 | around each block. */ | 491 | around each block. |
| 492 | |||
| 493 | The header consists of XMALLOC_OVERRUN_CHECK_SIZE fixed bytes | ||
| 494 | followed by XMALLOC_OVERRUN_SIZE_SIZE bytes containing the original | ||
| 495 | block size in little-endian order. The trailer consists of | ||
| 496 | XMALLOC_OVERRUN_CHECK_SIZE fixed bytes. | ||
| 497 | |||
| 498 | The header is used to detect whether this block has been allocated | ||
| 499 | through these functions, as some low-level libc functions may | ||
| 500 | bypass the malloc hooks. */ | ||
| 501 | |||
| 502 | #define XMALLOC_OVERRUN_CHECK_SIZE 16 | ||
| 503 | #define XMALLOC_OVERRUN_CHECK_OVERHEAD \ | ||
| 504 | (2 * XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE) | ||
| 505 | |||
| 506 | /* Define XMALLOC_OVERRUN_SIZE_SIZE so that (1) it's large enough to | ||
| 507 | hold a size_t value and (2) the header size is a multiple of the | ||
| 508 | alignment that Emacs needs for C types and for USE_LSB_TAG. */ | ||
| 509 | #define XMALLOC_BASE_ALIGNMENT \ | ||
| 510 | offsetof ( \ | ||
| 511 | struct { \ | ||
| 512 | union { long double d; intmax_t i; void *p; } u; \ | ||
| 513 | char c; \ | ||
| 514 | }, \ | ||
| 515 | c) | ||
| 516 | #ifdef USE_LSB_TAG | ||
| 517 | /* A common multiple of the positive integers A and B. Ideally this | ||
| 518 | would be the least common multiple, but there's no way to do that | ||
| 519 | as a constant expression in C, so do the best that we can easily do. */ | ||
| 520 | # define COMMON_MULTIPLE(a, b) \ | ||
| 521 | ((a) % (b) == 0 ? (a) : (b) % (a) == 0 ? (b) : (a) * (b)) | ||
| 522 | # define XMALLOC_HEADER_ALIGNMENT \ | ||
| 523 | COMMON_MULTIPLE (1 << GCTYPEBITS, XMALLOC_BASE_ALIGNMENT) | ||
| 524 | #else | ||
| 525 | # define XMALLOC_HEADER_ALIGNMENT XMALLOC_BASE_ALIGNMENT | ||
| 526 | #endif | ||
| 527 | #define XMALLOC_OVERRUN_SIZE_SIZE \ | ||
| 528 | (((XMALLOC_OVERRUN_CHECK_SIZE + sizeof (size_t) \ | ||
| 529 | + XMALLOC_HEADER_ALIGNMENT - 1) \ | ||
| 530 | / XMALLOC_HEADER_ALIGNMENT * XMALLOC_HEADER_ALIGNMENT) \ | ||
| 531 | - XMALLOC_OVERRUN_CHECK_SIZE) | ||
| 489 | 532 | ||
| 490 | static char const xmalloc_overrun_check_header[XMALLOC_OVERRUN_CHECK_SIZE] = | 533 | static char const xmalloc_overrun_check_header[XMALLOC_OVERRUN_CHECK_SIZE] = |
| 491 | { '\x9a', '\x9b', '\xae', '\xaf', | 534 | { '\x9a', '\x9b', '\xae', '\xaf', |