about summary refs log tree commit diff
path: root/nptl/tst-pthread-getattr.c
blob: 692bce46294c6ce5822405eba73bbac678d9fd20 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* Make sure that the stackaddr returned by pthread_getattr_np is
   reachable.

   Copyright (C) 2012 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <stdio.h>
#include <string.h>
#include <sys/resource.h>
#include <pthread.h>
#include <alloca.h>
#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))

/* 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)
{
  void *mem = &mem;
  /* FIXME:  mem >= stackaddr for _STACK_GROWSUP.  */
  mem = alloca ((size_t) (mem - stackaddr));
  assert (mem <= stackaddr);

  /* We don't access mem here because the compiler may move the stack pointer
     beyond what we expect, thus making our alloca send the stack pointer
     beyond stackaddr.  Using only stackaddr without the assert may make the
     compiler think that this instruction is independent of the above alloca
     and hence reshuffle to do this dereference before the alloca.  */
  *(int *)stackaddr = 42;
  return stackaddr;
}

static int
get_self_pthread_attr (const char *id, void **stackaddr, size_t *stacksize)
{
  pthread_attr_t attr;
  int ret;
  pthread_t me = pthread_self ();

  if ((ret = pthread_getattr_np (me, &attr)))
    {
      printf ("%s: pthread_getattr_np failed: %s\n", id, strerror (ret));
      return 1;
    }

  if ((ret = pthread_attr_getstack (&attr, stackaddr, stacksize)))
    {
      printf ("%s: pthread_attr_getstack returned error: %s\n", id,
	      strerror (ret));
      return 1;
    }

  return 0;
}

/* Verify that the stack size returned by pthread_getattr_np is usable when
   the returned value is subject to rlimit.  */
static int
check_stack_top (void)
{
  struct rlimit stack_limit;
  void *stackaddr;
  size_t stacksize = 0;
  int ret;

  puts ("Verifying that stack top is accessible");

  ret = getrlimit (RLIMIT_STACK, &stack_limit);
  if (ret)
    {
      perror ("getrlimit failed");
      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
     (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)))
    {
      perror ("setrlimit failed");
      return 1;
    }

  if (get_self_pthread_attr ("check_stack_top2", &stackaddr, &stacksize))
    return 1;

  printf ("Adjusted rlimit: stacksize=%zu, stackaddr=%p\n", stacksize,
          stackaddr);

  /* So that the compiler does not optimize out this call.  */
  stackaddr = allocate_and_test (stackaddr);
  assert (*(int *)stackaddr == 42);

  puts ("Stack top tests done");

  return 0;
}

/* TODO: Similar check for thread stacks once the thread stack sizes are
   fixed.  */
static int
do_test (void)
{
  return check_stack_top ();
}


#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"