]> git.proxmox.com Git - rustc.git/blob - src/libcompiler_builtins/compiler-rt/lib/sanitizer_common/sanitizer_procmaps_linux.cc
New upstream version 1.20.0+dfsg1
[rustc.git] / src / libcompiler_builtins / 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 CHECK(ReadFileToBuffer("/proc/self/maps", &proc_maps->data,
22 &proc_maps->mmaped_size, &proc_maps->len));
23 }
24
25 static bool IsOneOf(char c, char c1, char c2) {
26 return c == c1 || c == c2;
27 }
28
29 bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset,
30 char filename[], uptr filename_size,
31 uptr *protection, ModuleArch *arch, u8 *uuid) {
32 CHECK(!arch && "not implemented");
33 CHECK(!uuid && "not implemented");
34 char *last = proc_self_maps_.data + proc_self_maps_.len;
35 if (current_ >= last) return false;
36 uptr dummy;
37 if (!start) start = &dummy;
38 if (!end) end = &dummy;
39 if (!offset) offset = &dummy;
40 if (!protection) protection = &dummy;
41 char *next_line = (char*)internal_memchr(current_, '\n', last - current_);
42 if (next_line == 0)
43 next_line = last;
44 // Example: 08048000-08056000 r-xp 00000000 03:0c 64593 /foo/bar
45 *start = ParseHex(&current_);
46 CHECK_EQ(*current_++, '-');
47 *end = ParseHex(&current_);
48 CHECK_EQ(*current_++, ' ');
49 CHECK(IsOneOf(*current_, '-', 'r'));
50 *protection = 0;
51 if (*current_++ == 'r')
52 *protection |= kProtectionRead;
53 CHECK(IsOneOf(*current_, '-', 'w'));
54 if (*current_++ == 'w')
55 *protection |= kProtectionWrite;
56 CHECK(IsOneOf(*current_, '-', 'x'));
57 if (*current_++ == 'x')
58 *protection |= kProtectionExecute;
59 CHECK(IsOneOf(*current_, 's', 'p'));
60 if (*current_++ == 's')
61 *protection |= kProtectionShared;
62 CHECK_EQ(*current_++, ' ');
63 *offset = ParseHex(&current_);
64 CHECK_EQ(*current_++, ' ');
65 ParseHex(&current_);
66 CHECK_EQ(*current_++, ':');
67 ParseHex(&current_);
68 CHECK_EQ(*current_++, ' ');
69 while (IsDecimal(*current_))
70 current_++;
71 // Qemu may lack the trailing space.
72 // https://github.com/google/sanitizers/issues/160
73 // CHECK_EQ(*current_++, ' ');
74 // Skip spaces.
75 while (current_ < next_line && *current_ == ' ')
76 current_++;
77 // Fill in the filename.
78 uptr i = 0;
79 while (current_ < next_line) {
80 if (filename && i < filename_size - 1)
81 filename[i++] = *current_;
82 current_++;
83 }
84 if (filename && i < filename_size)
85 filename[i] = 0;
86 current_ = next_line + 1;
87 return true;
88 }
89
90 } // namespace __sanitizer
91
92 #endif // SANITIZER_LINUX