blob: 79ca4dfd8fca512444a495ac1d8c7732fd0853a5 [file] [log] [blame]
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001//===-- 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"
Stephen Hines6d186232014-11-26 17:56:19 -080014#if SANITIZER_LINUX
Stephen Hines2d1fdb22014-05-28 23:58:16 -070015#include "sanitizer_common.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070016#include "sanitizer_procmaps.h"
17
Stephen Hines2d1fdb22014-05-28 23:58:16 -070018namespace __sanitizer {
19
Stephen Hines6d186232014-11-26 17:56:19 -080020void ReadProcMaps(ProcSelfMapsBuff *proc_maps) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -070021 proc_maps->len = ReadFileToBuffer("/proc/self/maps", &proc_maps->data,
22 &proc_maps->mmaped_size, 1 << 26);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070023}
24
25static bool IsOneOf(char c, char c1, char c2) {
26 return c == c1 || c == c2;
27}
Stephen Hines2d1fdb22014-05-28 23:58:16 -070028
29bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset,
30 char filename[], uptr filename_size,
31 uptr *protection) {
32 char *last = proc_self_maps_.data + proc_self_maps_.len;
33 if (current_ >= last) return false;
34 uptr dummy;
35 if (!start) start = &dummy;
36 if (!end) end = &dummy;
37 if (!offset) offset = &dummy;
38 if (!protection) protection = &dummy;
Stephen Hines2d1fdb22014-05-28 23:58:16 -070039 char *next_line = (char*)internal_memchr(current_, '\n', last - current_);
40 if (next_line == 0)
41 next_line = last;
42 // Example: 08048000-08056000 r-xp 00000000 03:0c 64593 /foo/bar
43 *start = ParseHex(&current_);
44 CHECK_EQ(*current_++, '-');
45 *end = ParseHex(&current_);
46 CHECK_EQ(*current_++, ' ');
47 CHECK(IsOneOf(*current_, '-', 'r'));
48 *protection = 0;
49 if (*current_++ == 'r')
50 *protection |= kProtectionRead;
51 CHECK(IsOneOf(*current_, '-', 'w'));
52 if (*current_++ == 'w')
53 *protection |= kProtectionWrite;
54 CHECK(IsOneOf(*current_, '-', 'x'));
55 if (*current_++ == 'x')
56 *protection |= kProtectionExecute;
57 CHECK(IsOneOf(*current_, 's', 'p'));
58 if (*current_++ == 's')
59 *protection |= kProtectionShared;
60 CHECK_EQ(*current_++, ' ');
61 *offset = ParseHex(&current_);
62 CHECK_EQ(*current_++, ' ');
63 ParseHex(&current_);
64 CHECK_EQ(*current_++, ':');
65 ParseHex(&current_);
66 CHECK_EQ(*current_++, ' ');
67 while (IsDecimal(*current_))
68 current_++;
69 // Qemu may lack the trailing space.
70 // http://code.google.com/p/address-sanitizer/issues/detail?id=160
71 // CHECK_EQ(*current_++, ' ');
72 // Skip spaces.
73 while (current_ < next_line && *current_ == ' ')
74 current_++;
75 // Fill in the filename.
76 uptr i = 0;
77 while (current_ < next_line) {
78 if (filename && i < filename_size - 1)
79 filename[i++] = *current_;
80 current_++;
81 }
82 if (filename && i < filename_size)
83 filename[i] = 0;
84 current_ = next_line + 1;
Stephen Hines2d1fdb22014-05-28 23:58:16 -070085 return true;
86}
87
Stephen Hines2d1fdb22014-05-28 23:58:16 -070088} // namespace __sanitizer
89
Stephen Hines6d186232014-11-26 17:56:19 -080090#endif // SANITIZER_LINUX