llvm-project
81 строка · 2.9 Кб
1//===-- sanitizer_procmaps_linux.cpp --------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Information about the process mappings (Linux-specific parts).
10//===----------------------------------------------------------------------===//
11
12#include "sanitizer_platform.h"
13#if SANITIZER_LINUX
14#include "sanitizer_common.h"
15#include "sanitizer_procmaps.h"
16
17namespace __sanitizer {
18
19void ReadProcMaps(ProcSelfMapsBuff *proc_maps) {
20if (!ReadFileToBuffer("/proc/self/maps", &proc_maps->data,
21&proc_maps->mmaped_size, &proc_maps->len)) {
22proc_maps->data = nullptr;
23proc_maps->mmaped_size = 0;
24proc_maps->len = 0;
25}
26}
27
28static bool IsOneOf(char c, char c1, char c2) {
29return c == c1 || c == c2;
30}
31
32bool MemoryMappingLayout::Next(MemoryMappedSegment *segment) {
33if (Error()) return false; // simulate empty maps
34char *last = data_.proc_self_maps.data + data_.proc_self_maps.len;
35if (data_.current >= last) return false;
36char *next_line =
37(char *)internal_memchr(data_.current, '\n', last - data_.current);
38if (next_line == 0)
39next_line = last;
40// Example: 08048000-08056000 r-xp 00000000 03:0c 64593 /foo/bar
41segment->start = ParseHex(&data_.current);
42CHECK_EQ(*data_.current++, '-');
43segment->end = ParseHex(&data_.current);
44CHECK_EQ(*data_.current++, ' ');
45CHECK(IsOneOf(*data_.current, '-', 'r'));
46segment->protection = 0;
47if (*data_.current++ == 'r') segment->protection |= kProtectionRead;
48CHECK(IsOneOf(*data_.current, '-', 'w'));
49if (*data_.current++ == 'w') segment->protection |= kProtectionWrite;
50CHECK(IsOneOf(*data_.current, '-', 'x'));
51if (*data_.current++ == 'x') segment->protection |= kProtectionExecute;
52CHECK(IsOneOf(*data_.current, 's', 'p'));
53if (*data_.current++ == 's') segment->protection |= kProtectionShared;
54CHECK_EQ(*data_.current++, ' ');
55segment->offset = ParseHex(&data_.current);
56CHECK_EQ(*data_.current++, ' ');
57ParseHex(&data_.current);
58CHECK_EQ(*data_.current++, ':');
59ParseHex(&data_.current);
60CHECK_EQ(*data_.current++, ' ');
61while (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.
66while (data_.current < next_line && *data_.current == ' ') data_.current++;
67// Fill in the filename.
68if (segment->filename) {
69uptr len =
70Min((uptr)(next_line - data_.current), segment->filename_size - 1);
71internal_strncpy(segment->filename, data_.current, len);
72segment->filename[len] = 0;
73}
74
75data_.current = next_line + 1;
76return true;
77}
78
79} // namespace __sanitizer
80
81#endif // SANITIZER_LINUX
82