File: sanitizer_procmaps_test.cpp

package info (click to toggle)
llvm-toolchain-16 1%3A16.0.6-15~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,634,792 kB
  • sloc: cpp: 6,179,261; ansic: 1,216,205; asm: 741,319; python: 196,614; objc: 75,325; f90: 49,640; lisp: 32,396; pascal: 12,286; sh: 9,394; perl: 7,442; ml: 5,494; awk: 3,523; makefile: 2,723; javascript: 1,206; xml: 886; fortran: 581; cs: 573
file content (138 lines) | stat: -rw-r--r-- 4,719 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
//===-- sanitizer_procmaps_test.cpp ---------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file is a part of ThreadSanitizer/AddressSanitizer runtime.
//
//===----------------------------------------------------------------------===//
#if !defined(_WIN32)  // There are no /proc/maps on Windows.

#  include "sanitizer_common/sanitizer_procmaps.h"

#  include <stdlib.h>
#  include <string.h>

#  include <vector>

#  include "gtest/gtest.h"

static void noop() {}
extern const char *argv0;

namespace __sanitizer {

#if SANITIZER_LINUX && !SANITIZER_ANDROID
TEST(MemoryMappingLayout, CodeRange) {
  uptr start, end;
  bool res = GetCodeRangeForFile("[vdso]", &start, &end);
  EXPECT_EQ(res, true);
  EXPECT_GT(start, 0U);
  EXPECT_LT(start, end);
}
#endif

TEST(MemoryMappingLayout, DumpListOfModules) {
  const char *last_slash = strrchr(argv0, '/');
  const char *binary_name = last_slash ? last_slash + 1 : argv0;
  MemoryMappingLayout memory_mapping(false);
  const uptr kMaxModules = 100;
  InternalMmapVector<LoadedModule> modules;
  modules.reserve(kMaxModules);
  memory_mapping.DumpListOfModules(&modules);
  EXPECT_GT(modules.size(), 0U);
  bool found = false;
  for (uptr i = 0; i < modules.size(); ++i) {
    if (modules[i].containsAddress((uptr)&noop)) {
      // Verify that the module name is sane.
      if (strstr(modules[i].full_name(), binary_name) != 0)
        found = true;
    }
    modules[i].clear();
  }
  EXPECT_TRUE(found);
}

TEST(MemoryMapping, LoadedModuleArchAndUUID) {
  if (SANITIZER_APPLE) {
    MemoryMappingLayout memory_mapping(false);
    const uptr kMaxModules = 100;
    InternalMmapVector<LoadedModule> modules;
    modules.reserve(kMaxModules);
    memory_mapping.DumpListOfModules(&modules);
    for (uptr i = 0; i < modules.size(); ++i) {
      ModuleArch arch = modules[i].arch();
      // Darwin unit tests are only run on i386/x86_64/x86_64h/arm64.
      if (SANITIZER_WORDSIZE == 32) {
        EXPECT_EQ(arch, kModuleArchI386);
      } else if (SANITIZER_WORDSIZE == 64) {
        EXPECT_TRUE(arch == kModuleArchX86_64 || arch == kModuleArchX86_64H || arch == kModuleArchARM64);
      }
      const u8 *uuid = modules[i].uuid();
      u8 null_uuid[kModuleUUIDSize] = {0};
      EXPECT_NE(memcmp(null_uuid, uuid, kModuleUUIDSize), 0);
    }
  }
}

#  if (SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
       SANITIZER_SOLARIS) &&                                       \
      defined(_LP64)
const char *const parse_unix_input = R"(
7fb9862f1000-7fb9862f3000 rw-p 00000000 00:00 0 
Size:                  8 kB
Rss:                   4 kB
7fb9864ae000-7fb9864b1000 r--p 001ba000 fd:01 22413919                   /lib/x86_64-linux-gnu/libc-2.32.so
Size:                 12 kB
Rss:                  12 kB
)";

TEST(MemoryMapping, ParseUnixMemoryProfile) {
  struct entry {
    uptr p;
    uptr rss;
    bool file;
  };
  typedef std::vector<entry> entries_t;
  entries_t entries;
  std::vector<char> input(parse_unix_input,
                          parse_unix_input + strlen(parse_unix_input));
  ParseUnixMemoryProfile(
      [](uptr p, uptr rss, bool file, uptr *mem) {
        reinterpret_cast<entries_t *>(mem)->push_back({p, rss, file});
      },
      reinterpret_cast<uptr *>(&entries), &input[0], input.size());
  EXPECT_EQ(entries.size(), 2ul);
  EXPECT_EQ(entries[0].p, 0x7fb9862f1000ul);
  EXPECT_EQ(entries[0].rss, 4ul << 10);
  EXPECT_EQ(entries[0].file, false);
  EXPECT_EQ(entries[1].p, 0x7fb9864ae000ul);
  EXPECT_EQ(entries[1].rss, 12ul << 10);
  EXPECT_EQ(entries[1].file, true);
}

TEST(MemoryMapping, ParseUnixMemoryProfileTruncated) {
  // ParseUnixMemoryProfile used to crash on truncated inputs.
  // This test allocates 2 pages, protects the second one
  // and places the input at the very end of the first page
  // to test for over-reads.
  uptr page = GetPageSizeCached();
  char *mem = static_cast<char *>(
      MmapOrDie(2 * page, "ParseUnixMemoryProfileTruncated"));
  EXPECT_TRUE(MprotectNoAccess(reinterpret_cast<uptr>(mem + page), page));
  const uptr len = strlen(parse_unix_input);
  for (uptr i = 0; i < len; i++) {
    char *smaps = mem + page - len + i;
    memcpy(smaps, parse_unix_input, len - i);
    ParseUnixMemoryProfile([](uptr p, uptr rss, bool file, uptr *mem) {},
                           nullptr, smaps, len - i);
  }
  UnmapOrDie(mem, 2 * page);
}
#  endif

}  // namespace __sanitizer
#endif  // !defined(_WIN32)