about summary refs log tree commit diff
path: root/elf/tst-decorate-maps.c
blob: b0ea65ffd7b4aafe3133c802dc9ef472f5f18705 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* Check the VMA name decoration.
   Copyright (C) 2023 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
   <https://www.gnu.org/licenses/>.  */

#include <stdlib.h>
#include <string.h>
#include <support/check.h>
#include <support/support.h>
#include <support/test-driver.h>
#include <support/xstdio.h>
#include <support/xthread.h>
#include <support/xunistd.h>
#include <sys/mman.h>

#ifndef MAP_STACK
# define MAP_STACK 0
#endif

static pthread_barrier_t b;

static int expected_n_arenas;

static void *
tf (void *closure)
{
  void *p = xmalloc (1024);

  /* Wait the thread startup, so thread stack is allocated.  */
  xpthread_barrier_wait (&b);

  /* Wait the test to read the process mapping.  */
  xpthread_barrier_wait (&b);

  free (p);

  return NULL;
}

struct proc_maps_t
{
  int n_def_threads;
  int n_user_threads;
  int n_arenas;
  int n_malloc_mmap;
  int n_loader_malloc_mmap;
};

static struct proc_maps_t
read_proc_maps (void)
{
  if (test_verbose)
    printf ("=== print process %jd memory mapping ===\n",
	    (intmax_t) getpid ());
  struct proc_maps_t r = { 0 };

  FILE *f = xfopen ("/proc/self/maps", "r");
  char *line = NULL;
  size_t line_len = 0;
  while (xgetline (&line, &line_len, f))
    {
      if (test_verbose)
	printf ("%s", line);
      if (strstr (line, "[anon: glibc: pthread stack:") != NULL)
	r.n_def_threads++;
      else if (strstr (line, "[anon: glibc: pthread user stack:") != NULL)
	r.n_user_threads++;
      else if (strstr (line, "[anon: glibc: malloc arena]") != NULL)
	r.n_arenas++;
      else if (strstr (line, "[anon: glibc: malloc]") != NULL)
	r.n_malloc_mmap++;
      else if (strstr (line, "[anon: glibc: loader malloc]") != NULL)
	r.n_loader_malloc_mmap++;
    }
  free (line);
  xfclose (f);

  if (test_verbose)
    printf ("===\n");
  return r;
}

static void
do_test_threads (bool set_guard)
{
  enum
    {
      num_def_threads  = 8,
      num_user_threads = 2,
      num_threads = num_def_threads + num_user_threads,
    };

  xpthread_barrier_init (&b, NULL, num_threads + 1);

  /* Issue a large malloc to trigger a mmap call.  */
  void *p = xmalloc (256 * 1024);

  pthread_t thr[num_threads];
  {
    int i = 0;
    for (; i < num_threads - num_user_threads; i++)
      {
	pthread_attr_t attr;
	xpthread_attr_init (&attr);
	/* The guard page is not annotated.  */
	if (!set_guard)
	  xpthread_attr_setguardsize (&attr, 0);
	thr[i] = xpthread_create (&attr, tf, NULL);
	xpthread_attr_destroy (&attr);
      }
    for (; i < num_threads; i++)
      {
	pthread_attr_t attr;
	xpthread_attr_init (&attr);
	size_t stacksize = support_small_thread_stack_size ();
	void *stack = xmmap (0,
			     stacksize,
			     PROT_READ | PROT_WRITE,
			     MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK,
			     -1);
	xpthread_attr_setstack (&attr, stack, stacksize);
	if (!set_guard)
	  xpthread_attr_setguardsize (&attr, 0);
	thr[i] = xpthread_create (&attr, tf, NULL);
	xpthread_attr_destroy (&attr);
      }
  }

  /* Wait all threads to finshed statup and stack allocation.  */
  xpthread_barrier_wait (&b);

  {
    struct proc_maps_t r = read_proc_maps ();
    TEST_COMPARE (r.n_def_threads, num_def_threads);
    TEST_COMPARE (r.n_user_threads, num_user_threads);
    TEST_COMPARE (r.n_arenas, expected_n_arenas);
    TEST_COMPARE (r.n_malloc_mmap, 1);
    /* On some architectures the loader might use more than one page.  */
    TEST_VERIFY (r.n_loader_malloc_mmap >= 1);
  }

  /* Let the threads finish.  */
  xpthread_barrier_wait (&b);

  for (int i = 0; i < num_threads; i++)
    xpthread_join (thr[i]);

  {
    struct proc_maps_t r = read_proc_maps ();
    TEST_COMPARE (r.n_def_threads, 0);
    TEST_COMPARE (r.n_user_threads, 0);
    TEST_COMPARE (r.n_arenas, expected_n_arenas);
    TEST_COMPARE (r.n_malloc_mmap, 1);
    TEST_VERIFY (r.n_loader_malloc_mmap >= 1);
  }

  free (p);
}

static void
do_prepare (int argc, char *argv[])
{
  TEST_VERIFY_EXIT (argc == 2);
  expected_n_arenas = strtol (argv[1], NULL, 10);
  expected_n_arenas = expected_n_arenas - 1;
}
#define PREPARE do_prepare

static int
do_test (void)
{
  support_need_proc ("Reads /proc/self/maps to get stack names.");

  if (!support_set_vma_name_supported ())
    FAIL_UNSUPPORTED ("kernel does not support PR_SET_VMA_ANON_NAME");

  do_test_threads (false);
  do_test_threads (true);

  return 0;
}

#include <support/test-driver.c>