[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[commits] r4564 - in /fsf/trunk/libc: ChangeLog elf/tst-execstack.c malloc/Makefile malloc/malloc.c malloc/tst-trim1.c



Author: eglibc
Date: Mon Dec 17 00:04:43 2007
New Revision: 4564

Log:
Import glibc-mainline for 2007-12-17

Added:
    fsf/trunk/libc/malloc/tst-trim1.c
Modified:
    fsf/trunk/libc/ChangeLog
    fsf/trunk/libc/elf/tst-execstack.c
    fsf/trunk/libc/malloc/Makefile
    fsf/trunk/libc/malloc/malloc.c

Modified: fsf/trunk/libc/ChangeLog
==============================================================================
--- fsf/trunk/libc/ChangeLog (original)
+++ fsf/trunk/libc/ChangeLog Mon Dec 17 00:04:43 2007
@@ -1,3 +1,18 @@
+2007-12-16  Ulrich Drepper  <drepper@xxxxxxxxxx>
+
+	* elf/tst-execstack.c (do_test): Don't fail if SELinux forbids
+	executable stacks.
+
+	* malloc/malloc.c (public_mTRIm): Iterate over all arenas and call
+	mTRIm for all of them.
+	(mTRIm): Additionally iterate over all free blocks and use madvise
+	to free memory for all those blocks which contain at least one
+	memory page.
+	* malloc/tst-trim1.c: New file.
+	* malloc/Makefile (tests): Add tst-trim1.
+
+	* malloc/malloc.c (do_check_malloc_state): Minimal cleanups.
+
 2007-12-14  Ulrich Drepper  <drepper@xxxxxxxxxx>
 
 	* sysdeps/unix/sysv/linux/x86_64/sysdep.h (INTERNAL_SYSCALL_ERROR_P):

Modified: fsf/trunk/libc/elf/tst-execstack.c
==============================================================================
--- fsf/trunk/libc/elf/tst-execstack.c (original)
+++ fsf/trunk/libc/elf/tst-execstack.c Mon Dec 17 00:04:43 2007
@@ -2,6 +2,7 @@
    on load of a DSO that requires executable stacks.  */
 
 #include <dlfcn.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
@@ -45,9 +46,43 @@
 }
 #endif
 
+
+static bool allow_execstack = true;
+
+
 static int
 do_test (void)
 {
+  /* Check whether SELinux is enabled and disallows executable stacks.  */
+  FILE *fp = fopen ("/selinux/enforce", "r");
+  if (fp != NULL)
+    {
+      char *line = NULL;
+      size_t linelen = 0;
+
+      bool enabled = false;
+      ssize_t n = getline (&line, &linelen, fp);
+      if (n > 0 && line[0] != '0')
+	enabled = true;
+
+      fclose (fp);
+
+      if (enabled)
+	{
+	  fp = fopen ("/selinux/booleans/allow_execstack", "r");
+	  if (fp != NULL)
+	    {
+	      n = getline (&line, &linelen, fp);
+	      if (n > 0 && line[0] == '0')
+		allow_execstack = false;
+	    }
+
+	  fclose (fp);
+	}
+    }
+
+  printf ("executable stacks %sallowed\n", allow_execstack ? "" : "not ");
+
   static void *f;		/* Address of this is used in other threads. */
 
 #if USE_PTHREADS
@@ -77,7 +112,7 @@
   if (h == NULL)
     {
       printf ("cannot load: %s\n", dlerror ());
-      return 1;
+      return allow_execstack;
     }
 
   f = dlsym (h, "tryme");
@@ -113,10 +148,10 @@
      Let them run to test it.  */
   pthread_barrier_wait (&go_barrier);
 
-  pthread_exit (0);
+  pthread_exit (! allow_execstack);
 #endif
 
-  return 0;
+  return ! allow_execstack;
 }
 
 static void

Modified: fsf/trunk/libc/malloc/Makefile
==============================================================================
--- fsf/trunk/libc/malloc/Makefile (original)
+++ fsf/trunk/libc/malloc/Makefile Mon Dec 17 00:04:43 2007
@@ -1,4 +1,4 @@
-# Copyright (C) 1991-1999, 2000, 2001, 2002, 2003, 2005, 2006
+# Copyright (C) 1991-1999, 2000, 2001, 2002, 2003, 2005, 2006, 2007
 # Free Software Foundation, Inc.
 # This file is part of the GNU C Library.
 
@@ -27,7 +27,7 @@
 dist-headers := malloc.h
 headers := $(dist-headers) obstack.h mcheck.h
 tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \
-	 tst-mallocstate tst-mcheck tst-mallocfork
+	 tst-mallocstate tst-mcheck tst-mallocfork tst-trim1
 test-srcs = tst-mtrace
 
 distribute = thread-m.h mtrace.pl mcheck-init.c stackinfo.h memusage.h \

Modified: fsf/trunk/libc/malloc/malloc.c
==============================================================================
--- fsf/trunk/libc/malloc/malloc.c (original)
+++ fsf/trunk/libc/malloc/malloc.c Mon Dec 17 00:04:43 2007
@@ -1592,7 +1592,7 @@
 static Void_t** _int_icalloc(mstate, size_t, size_t, Void_t**);
 static Void_t** _int_icomalloc(mstate, size_t, size_t*, Void_t**);
 #endif
-static int      mTRIm(size_t);
+static int      mTRIm(mstate, size_t);
 static size_t   mUSABLe(Void_t*);
 static void     mSTATs(void);
 static int      mALLOPt(int, int);
@@ -2739,8 +2739,6 @@
   mchunkptr p;
   mchunkptr q;
   mbinptr b;
-  unsigned int binbit;
-  int empty;
   unsigned int idx;
   INTERNAL_SIZE_T size;
   unsigned long total = 0;
@@ -2810,8 +2808,8 @@
 
     /* binmap is accurate (except for bin 1 == unsorted_chunks) */
     if (i >= 2) {
-      binbit = get_binmap(av,i);
-      empty = last(b) == b;
+      unsigned int binbit = get_binmap(av,i);
+      int empty = last(b) == b;
       if (!binbit)
         assert(empty);
       else if (!empty)
@@ -4013,13 +4011,22 @@
 int
 public_mTRIm(size_t s)
 {
-  int result;
+  int result = 0;
 
   if(__malloc_initialized < 0)
     ptmalloc_init ();
-  (void)mutex_lock(&main_arena.mutex);
-  result = mTRIm(s);
-  (void)mutex_unlock(&main_arena.mutex);
+
+  mstate ar_ptr = &main_arena;
+  do
+    {
+      (void) mutex_lock (&ar_ptr->mutex);
+      result |= mTRIm (ar_ptr, s);
+      (void) mutex_unlock (&ar_ptr->mutex);
+
+      ar_ptr = ar_ptr->next;
+    }
+  while (ar_ptr != &main_arena);
+
   return result;
 }
 
@@ -5489,20 +5496,60 @@
 */
 
 #if __STD_C
-int mTRIm(size_t pad)
-#else
-int mTRIm(pad) size_t pad;
+static int mTRIm(mstate av, size_t pad)
+#else
+static int mTRIm(av, pad) mstate av; size_t pad;
 #endif
 {
-  mstate av = &main_arena; /* already locked */
-
   /* Ensure initialization/consolidation */
-  malloc_consolidate(av);
+  malloc_consolidate (av);
+
+  const size_t ps = mp_.pagesize;
+  int psindex = bin_index (ps);
+  const size_t psm1 = ps - 1;
+
+  int result = 0;
+  for (int i = 1; i < NBINS; ++i)
+    if (i == 1 || i >= psindex)
+      {
+        mbinptr bin = bin_at (av, i);
+
+        for (mchunkptr p = last (bin); p != bin; p = p->bk)
+	  {
+	    INTERNAL_SIZE_T size = chunksize (p);
+
+	    if (size > psm1 + sizeof (struct malloc_chunk))
+	      {
+		/* See whether the chunk contains at least one unused page.  */
+		char *paligned_mem = (char *) (((uintptr_t) p
+						+ sizeof (struct malloc_chunk)
+						+ psm1) & ~psm1);
+
+		assert ((char *) chunk2mem (p) + 4 * SIZE_SZ <= paligned_mem);
+		assert ((char *) p + size > paligned_mem);
+
+		/* This is the size we could potentially free.  */
+		size -= paligned_mem - (char *) p;
+
+		if (size > psm1)
+		  {
+#ifdef MALLOC_DEBUG
+		    /* When debugging we simulate destroying the memory
+		       content.  */
+		    memset (paligned_mem, 0x89, size & ~psm1);
+#endif
+		    madvise (paligned_mem, size & ~psm1, MADV_DONTNEED);
+
+		    result = 1;
+		  }
+	      }
+	  }
+      }
 
 #ifndef MORECORE_CANNOT_TRIM
-  return sYSTRIm(pad, av);
-#else
-  return 0;
+  return result | (av == &main_arena ? sYSTRIm (pad, av) : 0);
+#else
+  return result;
 #endif
 }
 

Added: fsf/trunk/libc/malloc/tst-trim1.c
==============================================================================
--- fsf/trunk/libc/malloc/tst-trim1.c (added)
+++ fsf/trunk/libc/malloc/tst-trim1.c Mon Dec 17 00:04:43 2007
@@ -1,0 +1,56 @@
+#include <malloc.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define N 10000
+
+static void *arr[N];
+
+static int
+do_test (void)
+{
+  for (int i = 0; i < N; ++i)
+    {
+      size_t size = random () % 16384;
+
+      if ((arr[i] = malloc (size)) == NULL)
+	{
+	nomem:
+	  puts ("not enough memory");
+	  return 0;
+	}
+
+      memset (arr[i], size, size);
+    }
+
+  void *p = malloc (256);
+  if (p == NULL)
+    goto nomem;
+  memset (p, 1, 256);
+
+  puts ("==================================================================");
+
+  for (int i = 0; i < N; ++i)
+    if (i % 13 != 0)
+      free (arr[i]);
+
+  puts ("==================================================================");
+
+  malloc_trim (0);
+
+  puts ("==================================================================");
+
+  p = malloc (30000);
+  if (p == NULL)
+    goto nomem;
+
+  memset (p, 2, 30000);
+
+  malloc_trim (0);
+
+  return 0;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"