annotate libsanitizer/sanitizer_common/sanitizer_linux.h @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 1830386684a0
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 //
kono
parents:
diff changeset
3 // This file is distributed under the University of Illinois Open Source
kono
parents:
diff changeset
4 // License. See LICENSE.TXT for details.
kono
parents:
diff changeset
5 //
kono
parents:
diff changeset
6 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
7 //
kono
parents:
diff changeset
8 // Linux-specific syscall wrappers and classes.
kono
parents:
diff changeset
9 //
kono
parents:
diff changeset
10 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
11 #ifndef SANITIZER_LINUX_H
kono
parents:
diff changeset
12 #define SANITIZER_LINUX_H
kono
parents:
diff changeset
13
kono
parents:
diff changeset
14 #include "sanitizer_platform.h"
kono
parents:
diff changeset
15 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD
kono
parents:
diff changeset
16 #include "sanitizer_common.h"
kono
parents:
diff changeset
17 #include "sanitizer_internal_defs.h"
kono
parents:
diff changeset
18 #include "sanitizer_platform_limits_netbsd.h"
kono
parents:
diff changeset
19 #include "sanitizer_platform_limits_posix.h"
kono
parents:
diff changeset
20 #include "sanitizer_posix.h"
kono
parents:
diff changeset
21
kono
parents:
diff changeset
22 struct link_map; // Opaque type returned by dlopen().
kono
parents:
diff changeset
23
kono
parents:
diff changeset
24 namespace __sanitizer {
kono
parents:
diff changeset
25 // Dirent structure for getdents(). Note that this structure is different from
kono
parents:
diff changeset
26 // the one in <dirent.h>, which is used by readdir().
kono
parents:
diff changeset
27 struct linux_dirent;
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 struct ProcSelfMapsBuff {
kono
parents:
diff changeset
30 char *data;
kono
parents:
diff changeset
31 uptr mmaped_size;
kono
parents:
diff changeset
32 uptr len;
kono
parents:
diff changeset
33 };
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 struct MemoryMappingLayoutData {
kono
parents:
diff changeset
36 ProcSelfMapsBuff proc_self_maps;
kono
parents:
diff changeset
37 const char *current;
kono
parents:
diff changeset
38 };
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 void ReadProcMaps(ProcSelfMapsBuff *proc_maps);
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 // Syscall wrappers.
kono
parents:
diff changeset
43 uptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count);
kono
parents:
diff changeset
44 uptr internal_sigaltstack(const void* ss, void* oss);
kono
parents:
diff changeset
45 uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
kono
parents:
diff changeset
46 __sanitizer_sigset_t *oldset);
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 // Linux-only syscalls.
kono
parents:
diff changeset
49 #if SANITIZER_LINUX
kono
parents:
diff changeset
50 uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5);
kono
parents:
diff changeset
51 // Used only by sanitizer_stoptheworld. Signal handlers that are actually used
kono
parents:
diff changeset
52 // (like the process-wide error reporting SEGV handler) must use
kono
parents:
diff changeset
53 // internal_sigaction instead.
kono
parents:
diff changeset
54 int internal_sigaction_norestorer(int signum, const void *act, void *oldact);
kono
parents:
diff changeset
55 #if (defined(__x86_64__) || SANITIZER_MIPS64) && !SANITIZER_GO
kono
parents:
diff changeset
56 // Uses a raw system call to avoid interceptors.
kono
parents:
diff changeset
57 int internal_sigaction_syscall(int signum, const void *act, void *oldact);
kono
parents:
diff changeset
58 #endif
kono
parents:
diff changeset
59 void internal_sigdelset(__sanitizer_sigset_t *set, int signum);
kono
parents:
diff changeset
60 #if defined(__x86_64__) || defined(__mips__) || defined(__aarch64__) \
kono
parents:
diff changeset
61 || defined(__powerpc64__) || defined(__s390__) || defined(__i386__) \
kono
parents:
diff changeset
62 || defined(__arm__)
kono
parents:
diff changeset
63 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
kono
parents:
diff changeset
64 int *parent_tidptr, void *newtls, int *child_tidptr);
kono
parents:
diff changeset
65 #endif
kono
parents:
diff changeset
66 #endif // SANITIZER_LINUX
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 // This class reads thread IDs from /proc/<pid>/task using only syscalls.
kono
parents:
diff changeset
69 class ThreadLister {
kono
parents:
diff changeset
70 public:
kono
parents:
diff changeset
71 explicit ThreadLister(int pid);
kono
parents:
diff changeset
72 ~ThreadLister();
kono
parents:
diff changeset
73 // GetNextTID returns -1 if the list of threads is exhausted, or if there has
kono
parents:
diff changeset
74 // been an error.
kono
parents:
diff changeset
75 int GetNextTID();
kono
parents:
diff changeset
76 void Reset();
kono
parents:
diff changeset
77 bool error();
kono
parents:
diff changeset
78
kono
parents:
diff changeset
79 private:
kono
parents:
diff changeset
80 bool GetDirectoryEntries();
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 int pid_;
kono
parents:
diff changeset
83 int descriptor_;
kono
parents:
diff changeset
84 InternalScopedBuffer<char> buffer_;
kono
parents:
diff changeset
85 bool error_;
kono
parents:
diff changeset
86 struct linux_dirent* entry_;
kono
parents:
diff changeset
87 int bytes_read_;
kono
parents:
diff changeset
88 };
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 // Exposed for testing.
kono
parents:
diff changeset
91 uptr ThreadDescriptorSize();
kono
parents:
diff changeset
92 uptr ThreadSelf();
kono
parents:
diff changeset
93 uptr ThreadSelfOffset();
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 // Matches a library's file name against a base name (stripping path and version
kono
parents:
diff changeset
96 // information).
kono
parents:
diff changeset
97 bool LibraryNameIs(const char *full_name, const char *base_name);
kono
parents:
diff changeset
98
kono
parents:
diff changeset
99 // Call cb for each region mapped by map.
kono
parents:
diff changeset
100 void ForEachMappedRegion(link_map *map, void (*cb)(const void *, uptr));
kono
parents:
diff changeset
101
kono
parents:
diff changeset
102 #if SANITIZER_ANDROID
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 #if defined(__aarch64__)
kono
parents:
diff changeset
105 # define __get_tls() \
kono
parents:
diff changeset
106 ({ void** __v; __asm__("mrs %0, tpidr_el0" : "=r"(__v)); __v; })
kono
parents:
diff changeset
107 #elif defined(__arm__)
kono
parents:
diff changeset
108 # define __get_tls() \
kono
parents:
diff changeset
109 ({ void** __v; __asm__("mrc p15, 0, %0, c13, c0, 3" : "=r"(__v)); __v; })
kono
parents:
diff changeset
110 #elif defined(__mips__)
kono
parents:
diff changeset
111 // On mips32r1, this goes via a kernel illegal instruction trap that's
kono
parents:
diff changeset
112 // optimized for v1.
kono
parents:
diff changeset
113 # define __get_tls() \
kono
parents:
diff changeset
114 ({ register void** __v asm("v1"); \
kono
parents:
diff changeset
115 __asm__(".set push\n" \
kono
parents:
diff changeset
116 ".set mips32r2\n" \
kono
parents:
diff changeset
117 "rdhwr %0,$29\n" \
kono
parents:
diff changeset
118 ".set pop\n" : "=r"(__v)); \
kono
parents:
diff changeset
119 __v; })
kono
parents:
diff changeset
120 #elif defined(__i386__)
kono
parents:
diff changeset
121 # define __get_tls() \
kono
parents:
diff changeset
122 ({ void** __v; __asm__("movl %%gs:0, %0" : "=r"(__v)); __v; })
kono
parents:
diff changeset
123 #elif defined(__x86_64__)
kono
parents:
diff changeset
124 # define __get_tls() \
kono
parents:
diff changeset
125 ({ void** __v; __asm__("mov %%fs:0, %0" : "=r"(__v)); __v; })
kono
parents:
diff changeset
126 #else
kono
parents:
diff changeset
127 #error "Unsupported architecture."
kono
parents:
diff changeset
128 #endif
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 // The Android Bionic team has allocated a TLS slot for TSan starting with N,
kono
parents:
diff changeset
131 // given that Android currently doesn't support ELF TLS. It is used to store
kono
parents:
diff changeset
132 // Sanitizers thread specific data.
kono
parents:
diff changeset
133 static const int TLS_SLOT_TSAN = 8;
kono
parents:
diff changeset
134
kono
parents:
diff changeset
135 ALWAYS_INLINE uptr *get_android_tls_ptr() {
kono
parents:
diff changeset
136 return reinterpret_cast<uptr *>(&__get_tls()[TLS_SLOT_TSAN]);
kono
parents:
diff changeset
137 }
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 #endif // SANITIZER_ANDROID
kono
parents:
diff changeset
140
kono
parents:
diff changeset
141 } // namespace __sanitizer
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD
kono
parents:
diff changeset
144 #endif // SANITIZER_LINUX_H