annotate libsanitizer/sanitizer_common/sanitizer_linux.h @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 //===-- sanitizer_linux.h ---------------------------------------*- C++ -*-===//
kono
parents:
diff changeset
2 //
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
111
kono
parents:
diff changeset
6 //
kono
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
8 //
kono
parents:
diff changeset
9 // Linux-specific syscall wrappers and classes.
kono
parents:
diff changeset
10 //
kono
parents:
diff changeset
11 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
12 #ifndef SANITIZER_LINUX_H
kono
parents:
diff changeset
13 #define SANITIZER_LINUX_H
kono
parents:
diff changeset
14
kono
parents:
diff changeset
15 #include "sanitizer_platform.h"
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
16 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
17 SANITIZER_OPENBSD || SANITIZER_SOLARIS
111
kono
parents:
diff changeset
18 #include "sanitizer_common.h"
kono
parents:
diff changeset
19 #include "sanitizer_internal_defs.h"
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
20 #include "sanitizer_platform_limits_freebsd.h"
111
kono
parents:
diff changeset
21 #include "sanitizer_platform_limits_netbsd.h"
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
22 #include "sanitizer_platform_limits_openbsd.h"
111
kono
parents:
diff changeset
23 #include "sanitizer_platform_limits_posix.h"
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
24 #include "sanitizer_platform_limits_solaris.h"
111
kono
parents:
diff changeset
25 #include "sanitizer_posix.h"
kono
parents:
diff changeset
26
kono
parents:
diff changeset
27 struct link_map; // Opaque type returned by dlopen().
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 namespace __sanitizer {
kono
parents:
diff changeset
30 // Dirent structure for getdents(). Note that this structure is different from
kono
parents:
diff changeset
31 // the one in <dirent.h>, which is used by readdir().
kono
parents:
diff changeset
32 struct linux_dirent;
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 struct ProcSelfMapsBuff {
kono
parents:
diff changeset
35 char *data;
kono
parents:
diff changeset
36 uptr mmaped_size;
kono
parents:
diff changeset
37 uptr len;
kono
parents:
diff changeset
38 };
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 struct MemoryMappingLayoutData {
kono
parents:
diff changeset
41 ProcSelfMapsBuff proc_self_maps;
kono
parents:
diff changeset
42 const char *current;
kono
parents:
diff changeset
43 };
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 void ReadProcMaps(ProcSelfMapsBuff *proc_maps);
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 // Syscall wrappers.
kono
parents:
diff changeset
48 uptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count);
kono
parents:
diff changeset
49 uptr internal_sigaltstack(const void* ss, void* oss);
kono
parents:
diff changeset
50 uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
kono
parents:
diff changeset
51 __sanitizer_sigset_t *oldset);
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
52 uptr internal_clock_gettime(__sanitizer_clockid_t clk_id, void *tp);
111
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 // Linux-only syscalls.
kono
parents:
diff changeset
55 #if SANITIZER_LINUX
kono
parents:
diff changeset
56 uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5);
kono
parents:
diff changeset
57 // Used only by sanitizer_stoptheworld. Signal handlers that are actually used
kono
parents:
diff changeset
58 // (like the process-wide error reporting SEGV handler) must use
kono
parents:
diff changeset
59 // internal_sigaction instead.
kono
parents:
diff changeset
60 int internal_sigaction_norestorer(int signum, const void *act, void *oldact);
kono
parents:
diff changeset
61 void internal_sigdelset(__sanitizer_sigset_t *set, int signum);
kono
parents:
diff changeset
62 #if defined(__x86_64__) || defined(__mips__) || defined(__aarch64__) \
kono
parents:
diff changeset
63 || defined(__powerpc64__) || defined(__s390__) || defined(__i386__) \
kono
parents:
diff changeset
64 || defined(__arm__)
kono
parents:
diff changeset
65 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
kono
parents:
diff changeset
66 int *parent_tidptr, void *newtls, int *child_tidptr);
kono
parents:
diff changeset
67 #endif
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
68 #elif SANITIZER_FREEBSD
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
69 void internal_sigdelset(__sanitizer_sigset_t *set, int signum);
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
70 #elif SANITIZER_NETBSD
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
71 void internal_sigdelset(__sanitizer_sigset_t *set, int signum);
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
72 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg);
111
kono
parents:
diff changeset
73 #endif // SANITIZER_LINUX
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 // This class reads thread IDs from /proc/<pid>/task using only syscalls.
kono
parents:
diff changeset
76 class ThreadLister {
kono
parents:
diff changeset
77 public:
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
78 explicit ThreadLister(pid_t pid);
111
kono
parents:
diff changeset
79 ~ThreadLister();
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
80 enum Result {
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
81 Error,
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
82 Incomplete,
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
83 Ok,
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
84 };
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
85 Result ListThreads(InternalMmapVector<tid_t> *threads);
111
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 private:
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
88 bool IsAlive(int tid);
111
kono
parents:
diff changeset
89
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
90 pid_t pid_;
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
91 int descriptor_ = -1;
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
92 InternalMmapVector<char> buffer_;
111
kono
parents:
diff changeset
93 };
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 // Exposed for testing.
kono
parents:
diff changeset
96 uptr ThreadDescriptorSize();
kono
parents:
diff changeset
97 uptr ThreadSelf();
kono
parents:
diff changeset
98 uptr ThreadSelfOffset();
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 // Matches a library's file name against a base name (stripping path and version
kono
parents:
diff changeset
101 // information).
kono
parents:
diff changeset
102 bool LibraryNameIs(const char *full_name, const char *base_name);
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 // Call cb for each region mapped by map.
kono
parents:
diff changeset
105 void ForEachMappedRegion(link_map *map, void (*cb)(const void *, uptr));
kono
parents:
diff changeset
106
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
107 // Releases memory pages entirely within the [beg, end] address range.
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
108 // The pages no longer count toward RSS; reads are guaranteed to return 0.
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
109 // Requires (but does not verify!) that pages are MAP_PRIVATE.
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
110 INLINE void ReleaseMemoryPagesToOSAndZeroFill(uptr beg, uptr end) {
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
111 // man madvise on Linux promises zero-fill for anonymous private pages.
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
112 // Testing shows the same behaviour for private (but not anonymous) mappings
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
113 // of shm_open() files, as long as the underlying file is untouched.
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
114 CHECK(SANITIZER_LINUX);
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
115 ReleaseMemoryPagesToOS(beg, end);
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
116 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
117
111
kono
parents:
diff changeset
118 #if SANITIZER_ANDROID
kono
parents:
diff changeset
119
kono
parents:
diff changeset
120 #if defined(__aarch64__)
kono
parents:
diff changeset
121 # define __get_tls() \
kono
parents:
diff changeset
122 ({ void** __v; __asm__("mrs %0, tpidr_el0" : "=r"(__v)); __v; })
kono
parents:
diff changeset
123 #elif defined(__arm__)
kono
parents:
diff changeset
124 # define __get_tls() \
kono
parents:
diff changeset
125 ({ void** __v; __asm__("mrc p15, 0, %0, c13, c0, 3" : "=r"(__v)); __v; })
kono
parents:
diff changeset
126 #elif defined(__mips__)
kono
parents:
diff changeset
127 // On mips32r1, this goes via a kernel illegal instruction trap that's
kono
parents:
diff changeset
128 // optimized for v1.
kono
parents:
diff changeset
129 # define __get_tls() \
kono
parents:
diff changeset
130 ({ register void** __v asm("v1"); \
kono
parents:
diff changeset
131 __asm__(".set push\n" \
kono
parents:
diff changeset
132 ".set mips32r2\n" \
kono
parents:
diff changeset
133 "rdhwr %0,$29\n" \
kono
parents:
diff changeset
134 ".set pop\n" : "=r"(__v)); \
kono
parents:
diff changeset
135 __v; })
kono
parents:
diff changeset
136 #elif defined(__i386__)
kono
parents:
diff changeset
137 # define __get_tls() \
kono
parents:
diff changeset
138 ({ void** __v; __asm__("movl %%gs:0, %0" : "=r"(__v)); __v; })
kono
parents:
diff changeset
139 #elif defined(__x86_64__)
kono
parents:
diff changeset
140 # define __get_tls() \
kono
parents:
diff changeset
141 ({ void** __v; __asm__("mov %%fs:0, %0" : "=r"(__v)); __v; })
kono
parents:
diff changeset
142 #else
kono
parents:
diff changeset
143 #error "Unsupported architecture."
kono
parents:
diff changeset
144 #endif
kono
parents:
diff changeset
145
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
146 // The Android Bionic team has allocated a TLS slot for sanitizers starting
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
147 // with Q, given that Android currently doesn't support ELF TLS. It is used to
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
148 // store sanitizer thread specific data.
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
149 static const int TLS_SLOT_SANITIZER = 6;
111
kono
parents:
diff changeset
150
kono
parents:
diff changeset
151 ALWAYS_INLINE uptr *get_android_tls_ptr() {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
152 return reinterpret_cast<uptr *>(&__get_tls()[TLS_SLOT_SANITIZER]);
111
kono
parents:
diff changeset
153 }
kono
parents:
diff changeset
154
kono
parents:
diff changeset
155 #endif // SANITIZER_ANDROID
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 } // namespace __sanitizer
kono
parents:
diff changeset
158
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
159 #endif
111
kono
parents:
diff changeset
160 #endif // SANITIZER_LINUX_H