1 //===-- sanitizer_procmaps_test.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 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
10 //
11 //===----------------------------------------------------------------------===//
12 #if !defined(_WIN32)  // There are no /proc/maps on Windows.
13 
14 #include "sanitizer_common/sanitizer_procmaps.h"
15 #include "gtest/gtest.h"
16 
17 #include <stdlib.h>
18 
noop()19 static void noop() {}
20 extern const char *argv0;
21 
22 namespace __sanitizer {
23 
24 #if SANITIZER_LINUX && !SANITIZER_ANDROID
TEST(MemoryMappingLayout,CodeRange)25 TEST(MemoryMappingLayout, CodeRange) {
26   uptr start, end;
27   bool res = GetCodeRangeForFile("[vdso]", &start, &end);
28   EXPECT_EQ(res, true);
29   EXPECT_GT(start, 0U);
30   EXPECT_LT(start, end);
31 }
32 #endif
33 
TEST(MemoryMappingLayout,DumpListOfModules)34 TEST(MemoryMappingLayout, DumpListOfModules) {
35   const char *last_slash = strrchr(argv0, '/');
36   const char *binary_name = last_slash ? last_slash + 1 : argv0;
37   MemoryMappingLayout memory_mapping(false);
38   const uptr kMaxModules = 100;
39   InternalMmapVector<LoadedModule> modules;
40   modules.reserve(kMaxModules);
41   memory_mapping.DumpListOfModules(&modules);
42   EXPECT_GT(modules.size(), 0U);
43   bool found = false;
44   for (uptr i = 0; i < modules.size(); ++i) {
45     if (modules[i].containsAddress((uptr)&noop)) {
46       // Verify that the module name is sane.
47       if (strstr(modules[i].full_name(), binary_name) != 0)
48         found = true;
49     }
50     modules[i].clear();
51   }
52   EXPECT_TRUE(found);
53 }
54 
TEST(MemoryMapping,LoadedModuleArchAndUUID)55 TEST(MemoryMapping, LoadedModuleArchAndUUID) {
56   if (SANITIZER_MAC) {
57     MemoryMappingLayout memory_mapping(false);
58     const uptr kMaxModules = 100;
59     InternalMmapVector<LoadedModule> modules;
60     modules.reserve(kMaxModules);
61     memory_mapping.DumpListOfModules(&modules);
62     for (uptr i = 0; i < modules.size(); ++i) {
63       ModuleArch arch = modules[i].arch();
64       // Darwin unit tests are only run on i386/x86_64/x86_64h.
65       if (SANITIZER_WORDSIZE == 32) {
66         EXPECT_EQ(arch, kModuleArchI386);
67       } else if (SANITIZER_WORDSIZE == 64) {
68         EXPECT_TRUE(arch == kModuleArchX86_64 || arch == kModuleArchX86_64H);
69       }
70       const u8 *uuid = modules[i].uuid();
71       u8 null_uuid[kModuleUUIDSize] = {0};
72       EXPECT_NE(memcmp(null_uuid, uuid, kModuleUUIDSize), 0);
73     }
74   }
75 }
76 
77 }  // namespace __sanitizer
78 #endif  // !defined(_WIN32)
79