mirror of
https://sourceware.org/git/glibc.git
synced 2025-08-08 17:42:12 +03:00
* malloc/arena.c (heap_info): Add mprotect_size field, adjust pad.
(new_heap): Initialize mprotect_size. (grow_heap): When growing, only mprotect from mprotect_size till new_size if mprotect_size is smaller. When shrinking, use PROT_NONE MMAP for __libc_enable_secure only, otherwise use MADV_DONTNEED. 2007-05-07 Ulrich Drepper <drepper@redhat.com> Jakub Jelinek <jakub@redhat.com> * malloc/arena.c (heap_info): Add mprotect_size field, adjust pad. (new_heap): Initialize mprotect_size. (grow_heap): When growing, only mprotect from mprotect_size till new_size if mprotect_size is smaller. When shrinking, use PROT_NONE MMAP for __libc_enable_secure only, otherwise use MADV_DONTNEED.
This commit is contained in:
@@ -1,3 +1,12 @@
|
|||||||
|
2007-05-07 Ulrich Drepper <drepper@redhat.com>
|
||||||
|
Jakub Jelinek <jakub@redhat.com>
|
||||||
|
|
||||||
|
* malloc/arena.c (heap_info): Add mprotect_size field, adjust pad.
|
||||||
|
(new_heap): Initialize mprotect_size.
|
||||||
|
(grow_heap): When growing, only mprotect from mprotect_size till
|
||||||
|
new_size if mprotect_size is smaller. When shrinking, use PROT_NONE
|
||||||
|
MMAP for __libc_enable_secure only, otherwise use MADV_DONTNEED.
|
||||||
|
|
||||||
2007-04-30 Steven Munroe <sjmunroe@us.ibm.com>
|
2007-04-30 Steven Munroe <sjmunroe@us.ibm.com>
|
||||||
Peter Bergner <bergner@us.ibm.com>
|
Peter Bergner <bergner@us.ibm.com>
|
||||||
|
|
||||||
|
@@ -104,6 +104,7 @@ $(objpfx)memusagestat: $(memusagestat-modules:%=$(objpfx)%.o)
|
|||||||
include ../Rules
|
include ../Rules
|
||||||
|
|
||||||
CFLAGS-mcheck-init.c = $(PIC-ccflag)
|
CFLAGS-mcheck-init.c = $(PIC-ccflag)
|
||||||
|
CFLAGS-malloc.c += -DMALLOC_DEBUG
|
||||||
|
|
||||||
$(objpfx)libmcheck.a: $(objpfx)mcheck-init.o
|
$(objpfx)libmcheck.a: $(objpfx)mcheck-init.o
|
||||||
-rm -f $@
|
-rm -f $@
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
/* Malloc implementation for multiple threads without lock contention.
|
/* Malloc implementation for multiple threads without lock contention.
|
||||||
Copyright (C) 2001,2002,2003,2004,2005,2006 Free Software Foundation, Inc.
|
Copyright (C) 2001,2002,2003,2004,2005,2006,2007
|
||||||
|
Free Software Foundation, Inc.
|
||||||
This file is part of the GNU C Library.
|
This file is part of the GNU C Library.
|
||||||
Contributed by Wolfram Gloger <wg@malloc.de>, 2001.
|
Contributed by Wolfram Gloger <wg@malloc.de>, 2001.
|
||||||
|
|
||||||
@@ -59,10 +60,12 @@ typedef struct _heap_info {
|
|||||||
mstate ar_ptr; /* Arena for this heap. */
|
mstate ar_ptr; /* Arena for this heap. */
|
||||||
struct _heap_info *prev; /* Previous heap. */
|
struct _heap_info *prev; /* Previous heap. */
|
||||||
size_t size; /* Current size in bytes. */
|
size_t size; /* Current size in bytes. */
|
||||||
|
size_t mprotect_size; /* Size in bytes that has been mprotected
|
||||||
|
PROT_READ|PROT_WRITE. */
|
||||||
/* Make sure the following data is properly aligned, particularly
|
/* Make sure the following data is properly aligned, particularly
|
||||||
that sizeof (heap_info) + 2 * SIZE_SZ is a multiple of
|
that sizeof (heap_info) + 2 * SIZE_SZ is a multiple of
|
||||||
MALLOG_ALIGNMENT. */
|
MALLOC_ALIGNMENT. */
|
||||||
char pad[-5 * SIZE_SZ & MALLOC_ALIGN_MASK];
|
char pad[-6 * SIZE_SZ & MALLOC_ALIGN_MASK];
|
||||||
} heap_info;
|
} heap_info;
|
||||||
|
|
||||||
/* Get a compile-time error if the heap_info padding is not correct
|
/* Get a compile-time error if the heap_info padding is not correct
|
||||||
@@ -692,6 +695,7 @@ new_heap(size, top_pad) size_t size, top_pad;
|
|||||||
}
|
}
|
||||||
h = (heap_info *)p2;
|
h = (heap_info *)p2;
|
||||||
h->size = size;
|
h->size = size;
|
||||||
|
h->mprotect_size = size;
|
||||||
THREAD_STAT(stat_n_heaps++);
|
THREAD_STAT(stat_n_heaps++);
|
||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
@@ -714,17 +718,34 @@ grow_heap(h, diff) heap_info *h; long diff;
|
|||||||
new_size = (long)h->size + diff;
|
new_size = (long)h->size + diff;
|
||||||
if((unsigned long) new_size > (unsigned long) HEAP_MAX_SIZE)
|
if((unsigned long) new_size > (unsigned long) HEAP_MAX_SIZE)
|
||||||
return -1;
|
return -1;
|
||||||
if(mprotect((char *)h + h->size, diff, PROT_READ|PROT_WRITE) != 0)
|
if((unsigned long) new_size > h->mprotect_size) {
|
||||||
return -2;
|
if (mprotect((char *)h + h->mprotect_size,
|
||||||
|
(unsigned long) new_size - h->mprotect_size,
|
||||||
|
PROT_READ|PROT_WRITE) != 0)
|
||||||
|
return -2;
|
||||||
|
h->mprotect_size = new_size;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
new_size = (long)h->size + diff;
|
new_size = (long)h->size + diff;
|
||||||
if(new_size < (long)sizeof(*h))
|
if(new_size < (long)sizeof(*h))
|
||||||
return -1;
|
return -1;
|
||||||
/* Try to re-map the extra heap space freshly to save memory, and
|
/* Try to re-map the extra heap space freshly to save memory, and
|
||||||
make it inaccessible. */
|
make it inaccessible. */
|
||||||
if((char *)MMAP((char *)h + new_size, -diff, PROT_NONE,
|
#ifdef _LIBC
|
||||||
MAP_PRIVATE|MAP_FIXED) == (char *) MAP_FAILED)
|
if (__builtin_expect (__libc_enable_secure, 0))
|
||||||
return -2;
|
#else
|
||||||
|
if (1)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
if((char *)MMAP((char *)h + new_size, -diff, PROT_NONE,
|
||||||
|
MAP_PRIVATE|MAP_FIXED) == (char *) MAP_FAILED)
|
||||||
|
return -2;
|
||||||
|
h->mprotect_size = new_size;
|
||||||
|
}
|
||||||
|
#ifdef _LIBC
|
||||||
|
else
|
||||||
|
madvise ((char *)h + new_size, -diff, MADV_DONTNEED);
|
||||||
|
#endif
|
||||||
/*fprintf(stderr, "shrink %p %08lx\n", h, new_size);*/
|
/*fprintf(stderr, "shrink %p %08lx\n", h, new_size);*/
|
||||||
}
|
}
|
||||||
h->size = new_size;
|
h->size = new_size;
|
||||||
|
Reference in New Issue
Block a user