guard area suggestion

Ian Wienand <[email protected]>
Newsgroups gmane.comp.lib.phil
Message-ID <[email protected]>
Hello,

Attached for consideration is a patch that creates a pointer to the
guard area, rather than making the tacit assumption that it will
always be at the bottom of the stack.  I think it makes it slightly
clearer what is happening when you have multiple stacks.

Also the stack top gets set to (pd)-1 near the end which I think is
incorrect.  when the memory is allocated for the stack previously pd
is set correctly so this just wastes space.

-i
[email protected]
http://www.gelato.unsw.edu.au
guard.diff (text/plain, 3.3 KB)
diff -ru ../libc-cvs-orig/nptl/allocatestack.c ./nptl/allocatestack.c
--- ../libc-cvs-orig/nptl/allocatestack.c	2003-03-12 05:02:17.000000000 +1100
+++ ./nptl/allocatestack.c	2003-03-13 12:09:32.000000000 +1100
@@ -350,6 +350,7 @@
       size_t guardsize;
       size_t reqsize;
       void *mem;
+      void *guardptr=NULL; /* initalise to stop gcc warning */
 
 #if COLORING_INCREMENT != 0
       /* Add one more page for stack coloring.  Don't to it for stacks
@@ -423,6 +424,15 @@
 	  pd->stackblock = mem;
 	  pd->stackblock_size = size;
 
+#ifdef NEED_SEPARATE_REGISTER_STACK
+	  /* the guard area is more or less half way between the top
+	     of the stack and the bottom */
+	  guardptr =  mem + (((size - __static_tls_size - guardsize) / 2) & ~pagesize_m1);
+#else
+	  /* the guard area is just at the bottom (mem) */
+	  guardptr = mem;
+#endif
+
 	  /* We allocated the first block thread-specific data array.
 	     This address will not change for the lifetime of this
 	     descriptor.  */
@@ -478,12 +488,8 @@
       /* Create or resize the guard area if necessary.  */
       if (__builtin_expect (guardsize > pd->guardsize, 0))
 	{
-#ifdef NEED_SEPARATE_REGISTER_STACK
-	  char *guard = mem + (((size - guardsize) / 2) & ~pagesize_m1);
-#else
-	  char *guard = mem;
-#endif
-	  if (mprotect (guard, guardsize, PROT_NONE) != 0)
+
+	  if (mprotect (guardptr, guardsize, PROT_NONE) != 0)
 	    {
 	      int err;
 	    mprot_error:
@@ -510,32 +516,19 @@
 	    }
 
 	  pd->guardsize = guardsize;
+	  pd->guardptr = guardptr;
 	}
       else if (__builtin_expect (pd->guardsize - guardsize > size - reqsize,
 				 0))
 	{
-	  /* The old guard area is too large.  */
-
-#ifdef NEED_SEPARATE_REGISTER_STACK
-	  char *guard = mem + (((size - guardsize) / 2) & ~pagesize_m1);
-	  char *oldguard = mem + (((size - pd->guardsize) / 2) & ~pagesize_m1);
-
-	  if (oldguard < guard
-	      && mprotect (oldguard, guard - oldguard,
-			   PROT_READ | PROT_WRITE | PROT_EXEC) != 0)
-	    goto mprot_error;
-
-	  if (mprotect (guard + guardsize,
-			oldguard + pd->guardsize - guard - guardsize,
+	  /* The old guard area is too large, give permissions back to
+	     the extra area.  */
+	  if (mprotect ((char *) pd->guardptr + guardsize, pd->guardsize - guardsize,
 			PROT_READ | PROT_WRITE | PROT_EXEC) != 0)
 	    goto mprot_error;
-#else
-	  if (mprotect ((char *) mem + guardsize, pd->guardsize - guardsize,
-			PROT_READ | PROT_WRITE | PROT_EXEC) != 0)
-	    goto mprot_error;
-#endif
 
 	  pd->guardsize = guardsize;
+	  pd->guardptr = guardptr;
 	}
     }
 
@@ -546,7 +539,8 @@
   /* The stack begins before the TCB and the static TLS block.  */
   stacktop = ((char *) (pd + 1) - __static_tls_size);
 #elif TLS_DTV_AT_TP
-  stacktop = (char *) (pd - 1);
+  /* The stack grows from below pd */
+  stacktop = (char *) (pd);
 #endif
 
 #ifdef NEED_SEPARATE_REGISTER_STACK
diff -ru ../libc-cvs-orig/nptl/descr.h ./nptl/descr.h
--- ../libc-cvs-orig/nptl/descr.h	2003-03-11 04:33:29.000000000 +1100
+++ ./nptl/descr.h	2003-03-13 12:38:14.000000000 +1100
@@ -179,6 +179,9 @@
   size_t stackblock_size;
   /* Size of the included guard area.  */
   size_t guardsize;
+  /* This points to the guard page.  Useful for archs with multiple stacks where
+     the guard page might be in the middle of the stack */
+  void *guardptr;
 } __attribute ((aligned (TCB_ALIGNMENT)));
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.