File: coredump_many_segments.c

package info (click to toggle)
valgrind 1%3A3.16.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 158,568 kB
  • sloc: ansic: 746,130; exp: 26,134; xml: 22,708; asm: 13,570; cpp: 7,691; makefile: 6,177; perl: 5,965; sh: 5,665; javascript: 929
file content (234 lines) | stat: -rw-r--r-- 6,245 bytes parent folder | download | duplicates (4)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* Tests that Valgrind coredump support works correctly even when
   number of segments exceeds 0xffff.
   For this to work, large number of pages is mmap()'ed into the
   process (virtual) address space. These pages must not be adjacent
   to each other otherwise the memory manager will coalesce them
   into a single one. So they are one page apart.

   NOTE: Valgrind's internal limit VG_N_SEGMENTS must be at least
   140000 otherwise you get a fatal error similar to this one:
       "FATAL: VG_N_SEGMENTS is too low."

   Test case passes successfully if the number of segments is
   correctly displayed in elfdump output:

   $ elfdump -e vgcore.*
ELF Header
  ...
  e_phoff: 0x34  e_phentsize: 32  e_phnum: PN_XNUM (see shdr[0].sh_info)
                                  ^^^^^^^^^^^^^^^^
Section Header[0]:  (ELF Ehdr extensions)
  ...
    sh_link: 0 (e_shstrndx)  sh_info: 65554 (e_phnum)
                             ^^^^^^^^^^^^^^^^^^^^^^^^
*/ 

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/procfs.h>
#include <sys/stat.h>

#define SEGMENTS (0xffff + 2)

#if 0
#define DEBUG(format, ...) printf(format, ## __VA_ARGS__)
#else
#define DEBUG(format, ...)
#endif

#define PRINT(format, ...) printf(format, ## __VA_ARGS__)

/* Represents a free range of a virtual address space. */
typedef struct range {
   uintptr_t     start;
   uintptr_t     end;
   size_t        size;
   struct range *next;
} range_t;

/* Processes a single prmap_t entry and builds the free ranges. */
static int process_map(const prmap_t *map, range_t **ranges_head,
                       range_t **ranges_tail, size_t page_size)
{
   assert(map != NULL);
   assert(ranges_head != NULL);
   assert(ranges_tail != NULL);

   range_t *head = *ranges_head;
   range_t *tail = *ranges_tail;

   DEBUG("processing map with addr=%p and size=%zu\n",
         map->pr_vaddr, map->pr_size);

   if (head == NULL) {
      head = calloc(1, sizeof(range_t));
      if (head == NULL) {
         fprintf(stderr, "calloc failed\n");
         return -1;
      }
      head->start = (uintptr_t) page_size; // do not start at address '0'

      tail = head;
      *ranges_head = head;
      *ranges_tail = tail;
   }

   if ((map->pr_vaddr < tail->start) ||
       (map->pr_vaddr - tail->start < 3 * page_size)) {
      DEBUG("last range at %p is too small, skipping it\n",
            tail->start);
      tail->start = map->pr_vaddr + map->pr_size + page_size;
      return 0;
   }

   tail->end = map->pr_vaddr - page_size;
   tail->size = tail->end - tail->start;

   range_t *new_one = calloc(1, sizeof(range_t));
   if (new_one == NULL) {
      fprintf(stderr, "calloc failed\n");
      return -1;
   }

   new_one->start = map->pr_vaddr + map->pr_size + page_size;
   tail->next = new_one;
   *ranges_tail = new_one;
   return 0;
}

/* Reads /proc/self/map and builds free ranges. */
static range_t *read_proc_map(size_t page_size)
{
   int fd = open("/proc/self/map", O_RDONLY);
   if (fd == -1) {
      int error = errno;
      fprintf(stderr, "open failed: %s (%d)\n", strerror(error), error);
      return NULL;
   }

   prmap_t map;
   range_t *ranges_head = NULL;
   range_t *ranges_tail = NULL;

   ssize_t bytes = read(fd, &map, sizeof(map));
   while (bytes == sizeof(map)) {
      if (map.pr_size != 0) {
         if (process_map(&map, &ranges_head, &ranges_tail,
                         page_size) != 0) {
            return NULL;
         }
      }
      bytes = read(fd, &map, sizeof(map));
   }

   if (ranges_tail != NULL) {
      ranges_tail->end = (uintptr_t) ~0;
      ranges_tail->size = ranges_tail->end - ranges_tail->start;
   }

   close(fd);
   return ranges_head;
}

static void print_ranges(const range_t *head)
{
   while (head != NULL) {
      DEBUG("free range [%8p - %8p] of size %7zuK\n",
            head->start, head->end, head->size / 1024);
      head = head->next;
   }
}

static size_t sum_ranges(const range_t *head)
{
   size_t sum = 0;

   while (head != NULL) {
      sum += head->size;
      head = head->next;
   }

   return sum;
}

static void *map_segment(void *fixed_addr)
{
   int flags = MAP_NORESERVE | MAP_ANON | MAP_PRIVATE | MAP_FIXED;
   void *addr = mmap(fixed_addr, 1, PROT_READ | PROT_WRITE,
                     flags, -1, 0);
   if (addr == MAP_FAILED) {
      int error = errno;
      fprintf(stderr, "mmap failed: %s (%d)\n", strerror(error), error);
      return NULL;
   }
   assert(addr == fixed_addr);

   *((char *) addr) = 1; // make the mmap'ed page dirty
   // DEBUG("mmap(%8p) = %8p\n", fixed_addr, addr);
   return addr;
}

int main(int argc, const char *argv[])
{
   long page_size = sysconf(_SC_PAGESIZE);
   if (page_size == -1) {
      perror("sysconf");
      return 1;
   }

   PRINT("Page size determined as %ld bytes.\n", page_size);

   range_t *ranges = read_proc_map(page_size);
   print_ranges(ranges);

   size_t sum = sum_ranges(ranges);
   if (sum < SEGMENTS * page_size) {
      fprintf(stderr, "Free (virtual) address space cannot accomodate "
              "%u pages.\n", SEGMENTS);
      return 1;
   }

   PRINT("mmap()'ing %u segments:", SEGMENTS);
   fflush(stdout);

   unsigned int segment = 0;
   while ((ranges != NULL) && (segment < SEGMENTS)) {
      unsigned int page;
      for (page = 0; page < ranges->size / (2 * page_size); page++) {
         uintptr_t start = ranges->start + 2 * page * page_size;
         void *addr = map_segment((void *) start);
         if (addr == NULL) {
            fprintf(stderr, "Mapping failed for segment %u at address "
                    "%" PRIxPTR ".\n", segment, start);
            return 1;
         }

         segment += 1;
         if (segment >= SEGMENTS) {
            break;
         }

         if (segment % (SEGMENTS / 10) == 0) {
            PRINT(" %u0%%", segment / (SEGMENTS / 10));
            fflush(stdout);
         }
      }
      ranges = ranges->next;
   }
   assert(segment == SEGMENTS);

   PRINT(".\nDumping core...\n");
   char *nihil = NULL;
   *nihil = 0; // SEGV here
   fprintf(stderr, "Should not reach here.\n");

   return 0;
}