]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_procmaps_linux.cc
MFV r342532: 5882 Temporary pool names
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_procmaps_linux.cc
1 //===-- sanitizer_procmaps_linux.cc ---------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Information about the process mappings (Linux-specific parts).
11 //===----------------------------------------------------------------------===//
12
13 #include "sanitizer_platform.h"
14 #if SANITIZER_LINUX
15 #include "sanitizer_common.h"
16 #include "sanitizer_procmaps.h"
17
18 namespace __sanitizer {
19
20 void ReadProcMaps(ProcSelfMapsBuff *proc_maps) {
21   if (!ReadFileToBuffer("/proc/self/maps", &proc_maps->data,
22                         &proc_maps->mmaped_size, &proc_maps->len)) {
23     proc_maps->data = nullptr;
24     proc_maps->mmaped_size = 0;
25     proc_maps->len = 0;
26   }
27 }
28
29 static bool IsOneOf(char c, char c1, char c2) {
30   return c == c1 || c == c2;
31 }
32
33 bool MemoryMappingLayout::Next(MemoryMappedSegment *segment) {
34   char *last = data_.proc_self_maps.data + data_.proc_self_maps.len;
35   if (data_.current >= last) return false;
36   char *next_line =
37       (char *)internal_memchr(data_.current, '\n', last - data_.current);
38   if (next_line == 0)
39     next_line = last;
40   // Example: 08048000-08056000 r-xp 00000000 03:0c 64593   /foo/bar
41   segment->start = ParseHex(&data_.current);
42   CHECK_EQ(*data_.current++, '-');
43   segment->end = ParseHex(&data_.current);
44   CHECK_EQ(*data_.current++, ' ');
45   CHECK(IsOneOf(*data_.current, '-', 'r'));
46   segment->protection = 0;
47   if (*data_.current++ == 'r') segment->protection |= kProtectionRead;
48   CHECK(IsOneOf(*data_.current, '-', 'w'));
49   if (*data_.current++ == 'w') segment->protection |= kProtectionWrite;
50   CHECK(IsOneOf(*data_.current, '-', 'x'));
51   if (*data_.current++ == 'x') segment->protection |= kProtectionExecute;
52   CHECK(IsOneOf(*data_.current, 's', 'p'));
53   if (*data_.current++ == 's') segment->protection |= kProtectionShared;
54   CHECK_EQ(*data_.current++, ' ');
55   segment->offset = ParseHex(&data_.current);
56   CHECK_EQ(*data_.current++, ' ');
57   ParseHex(&data_.current);
58   CHECK_EQ(*data_.current++, ':');
59   ParseHex(&data_.current);
60   CHECK_EQ(*data_.current++, ' ');
61   while (IsDecimal(*data_.current)) data_.current++;
62   // Qemu may lack the trailing space.
63   // https://github.com/google/sanitizers/issues/160
64   // CHECK_EQ(*data_.current++, ' ');
65   // Skip spaces.
66   while (data_.current < next_line && *data_.current == ' ') data_.current++;
67   // Fill in the filename.
68   if (segment->filename) {
69     uptr len =
70         Min((uptr)(next_line - data_.current), segment->filename_size - 1);
71     internal_strncpy(segment->filename, data_.current, len);
72     segment->filename[len] = 0;
73   }
74
75   data_.current = next_line + 1;
76   return true;
77 }
78
79 }  // namespace __sanitizer
80
81 #endif  // SANITIZER_LINUX