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