This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: tst-pthread-getattr changes break sparc


On Fri, 27 Jul 2012 11:00:15 -0400, Chris wrote:
> It might be simpler to just test that the page pointed to by
> "stackaddr" is accessible.  You could target your alloca() request to
> stackaddr plus half of getpagesize(), and not worry about the precise
> details of alloca's overhead.  I don't think there's any need to
> worry about whether the bottom of that bottom page is accessible or
> not, if we can show that the middle of the bottom page is
> accessible.  It feels like it would make the code a lot less finicky.

Thanks, I've used your idea and put in a fix to the test case. The fix
is now in the siddhesh/getattr branch. David, can you please verify
that this now works on sparc?

The effective patch to master is attached. I have verified that the
test case works correctly on x86_64.

Regards,
Siddhesh

nptl/ChangeLog:

2012-07-27  Siddhesh Poyarekar  <siddhesh@redhat.com>

	* tst-pthread-getattr.c (MAX_STACK_SIZE): New macro.
	(_MIN): New macro function.
	(allocate_and_test): Return MEM.  Rename parameter to TARGET.
	(check_stack_top): New local variable MEM.  Cap stack size
	to MAX_STACK_SIZE using _MIN.  Call allocate_and_test for
	halfway up the stack top page.  Verify that the top page was
	written into.
diff --git a/nptl/tst-pthread-getattr.c b/nptl/tst-pthread-getattr.c
index 6f2cfc6..8b6ca1b 100644
--- a/nptl/tst-pthread-getattr.c
+++ b/nptl/tst-pthread-getattr.c
@@ -23,16 +23,32 @@
 #include <sys/resource.h>
 #include <pthread.h>
 #include <alloca.h>
-
-/* Move the stack pointer so that stackaddr is accessible and then check if it
-   really is accessible.  This will segfault if it fails.  */
-static void
-allocate_and_test (void *stackaddr)
+#include <assert.h>
+
+/* There is an obscure bug in the kernel due to which RLIMIT_STACK is sometimes
+   returned as unlimited when it is not, which may cause this test to fail.
+   There is also the other case where RLIMIT_STACK is intentionally set as
+   unlimited or very high, which may result in a vma that is too large and again
+   results in a test case failure.  To avoid these problems, we cap the stack
+   size to one less than 8M.  See the following mailing list threads for more
+   information about this problem:
+   <http://sourceware.org/ml/libc-alpha/2012-06/msg00599.html>
+   <http://sourceware.org/ml/libc-alpha/2012-06/msg00713.html>.  */
+#define MAX_STACK_SIZE (8192 * 1024 - 1)
+
+#define _MIN(l,o) ((l) < (o) ? (l) : (o))
+
+/* Check if the page in which TARGET lies is accessible.  This will segfault
+   if it fails.  */
+static void *
+allocate_and_test (void *target)
 {
   void *mem = &mem;
-  /* FIXME:  The difference will be negative for _STACK_GROWSUP.  */
-  mem = alloca ((size_t) (mem - stackaddr));
-  *(int *)(mem) = 0;
+  /* FIXME:  mem >= target for _STACK_GROWSUP.  */
+  mem = alloca ((size_t) (mem - target));
+
+  *(int *)mem = 42;
+  return mem;
 }
 
 static int
@@ -64,7 +80,7 @@ static int
 check_stack_top (void)
 {
   struct rlimit stack_limit;
-  void *stackaddr;
+  void *stackaddr, *mem;
   size_t stacksize = 0;
   int ret;
 
@@ -77,17 +93,20 @@ check_stack_top (void)
       return 1;
     }
 
+  printf ("current rlimit_stack is %zu\n", stack_limit.rlim_cur);
+
   if (get_self_pthread_attr ("check_stack_top", &stackaddr, &stacksize))
     return 1;
 
-  /* Reduce the rlimit to a page less that what is currently being returned so
-     that we ensure that pthread_getattr_np uses rlimit.  The figure is
-     intentionally unaligned so to verify that pthread_getattr_np returns an
-     aligned stacksize that correctly fits into the rlimit.  We don't bother
-     about the case where the stack is limited by the vma below it and not by
-     the rlimit because the stacksize returned in that case is computed from
-     the end of that vma and is hence safe.  */
-  stack_limit.rlim_cur = stacksize - 4095;
+  /* Reduce the rlimit to a page less that what is currently being returned
+     (subject to a maximum of MAX_STACK_SIZE) so that we ensure that
+     pthread_getattr_np uses rlimit.  The figure is intentionally unaligned so
+     to verify that pthread_getattr_np returns an aligned stacksize that
+     correctly fits into the rlimit.  We don't bother about the case where the
+     stack is limited by the vma below it and not by the rlimit because the
+     stacksize returned in that case is computed from the end of that vma and is
+     hence safe.  */
+  stack_limit.rlim_cur = _MIN(stacksize - 4095, MAX_STACK_SIZE);
   printf ("Adjusting RLIMIT_STACK to %zu\n", stack_limit.rlim_cur);
   if ((ret = setrlimit (RLIMIT_STACK, &stack_limit)))
     {
@@ -100,7 +119,23 @@ check_stack_top (void)
 
   printf ("Adjusted rlimit: stacksize=%zu, stackaddr=%p\n", stacksize,
           stackaddr);
-  allocate_and_test (stackaddr);
+
+  /* A lot of targets tend to write stuff on top of the user stack during
+     context switches, so we cannot possibly safely go up to the very top of
+     stack and test access there.  It is however sufficient to simply check if
+     the top page is accessible, so we target our access halfway up the top
+     page.  Thanks Chris Metcalf for this idea.  */
+  mem = allocate_and_test (stackaddr + 2048);
+
+  /* Before we celebrate, make sure we actually did test the same page.  */
+  if (((uintptr_t) stackaddr & ~0xfff ) != ((uintptr_t) mem & ~0xfff))
+    {
+      printf ("We successfully wrote into the wrong page. ");
+      printf ("Expected %lx, but got %lx\n", (uintptr_t) stackaddr & ~0xfff,
+	      (uintptr_t) mem & ~0xfff);
+
+      return 1;
+    }
 
   puts ("Stack top tests done");
 

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]