1 //===-- sanitizer_linux_libcdep.cc ----------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is shared between AddressSanitizer and ThreadSanitizer
9 // run-time libraries and implements linux-specific functions from
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_platform.h"
16 #include "sanitizer_common.h"
17 #include "sanitizer_flags.h"
18 #include "sanitizer_linux.h"
19 #include "sanitizer_placement_new.h"
20 #include "sanitizer_procmaps.h"
21 #include "sanitizer_stacktrace.h"
25 #include <sys/prctl.h>
26 #include <sys/resource.h>
29 #if !SANITIZER_ANDROID
34 // This function is defined elsewhere if we intercepted pthread_attr_getstack.
35 SANITIZER_WEAK_ATTRIBUTE
36 int __sanitizer_pthread_attr_getstack(void *attr
, void **addr
, size_t *size
) {
37 return pthread_attr_getstack((pthread_attr_t
*)attr
, addr
, size
);
40 namespace __sanitizer
{
42 void GetThreadStackTopAndBottom(bool at_initialization
, uptr
*stack_top
,
44 static const uptr kMaxThreadStackSize
= 1 << 30; // 1Gb
47 if (at_initialization
) {
48 // This is the main thread. Libpthread may not be initialized yet.
50 CHECK_EQ(getrlimit(RLIMIT_STACK
, &rl
), 0);
52 // Find the mapping that contains a stack variable.
53 MemoryMappingLayout
proc_maps(/*cache_enabled*/true);
54 uptr start
, end
, offset
;
56 while (proc_maps
.Next(&start
, &end
, &offset
, 0, 0, /* protection */0)) {
61 CHECK((uptr
)&rl
>= start
&& (uptr
)&rl
< end
);
63 // Get stacksize from rlimit, but clip it so that it does not overlap
64 // with other mappings.
65 uptr stacksize
= rl
.rlim_cur
;
66 if (stacksize
> end
- prev_end
)
67 stacksize
= end
- prev_end
;
68 // When running with unlimited stack size, we still want to set some limit.
69 // The unlimited stack size is caused by 'ulimit -s unlimited'.
70 // Also, for some reason, GNU make spawns subprocesses with unlimited stack.
71 if (stacksize
> kMaxThreadStackSize
)
72 stacksize
= kMaxThreadStackSize
;
74 *stack_bottom
= end
- stacksize
;
78 CHECK_EQ(pthread_getattr_np(pthread_self(), &attr
), 0);
81 __sanitizer_pthread_attr_getstack(&attr
, &stackaddr
, (size_t*)&stacksize
);
82 pthread_attr_destroy(&attr
);
84 CHECK_LE(stacksize
, kMaxThreadStackSize
); // Sanity check.
85 *stack_top
= (uptr
)stackaddr
+ stacksize
;
86 *stack_bottom
= (uptr
)stackaddr
;
89 // Does not compile for Go because dlsym() requires -ldl
91 bool SetEnv(const char *name
, const char *value
) {
92 void *f
= dlsym(RTLD_NEXT
, "setenv");
95 typedef int(*setenv_ft
)(const char *name
, const char *value
, int overwrite
);
97 CHECK_EQ(sizeof(setenv_f
), sizeof(f
));
98 internal_memcpy(&setenv_f
, &f
, sizeof(f
));
99 return setenv_f(name
, value
, 1) == 0;
103 bool SanitizerSetThreadName(const char *name
) {
105 return 0 == prctl(PR_SET_NAME
, (unsigned long)name
, 0, 0, 0); // NOLINT
111 bool SanitizerGetThreadName(char *name
, int max_len
) {
114 if (prctl(PR_GET_NAME
, (unsigned long)buff
, 0, 0, 0)) // NOLINT
116 internal_strncpy(name
, buff
, max_len
);
125 //------------------------- SlowUnwindStack -----------------------------------
127 #define UNWIND_STOP _URC_END_OF_STACK
128 #define UNWIND_CONTINUE _URC_NO_REASON
130 #define UNWIND_STOP _URC_NORMAL_STOP
131 #define UNWIND_CONTINUE _URC_NO_REASON
134 uptr
Unwind_GetIP(struct _Unwind_Context
*ctx
) {
137 _Unwind_VRS_Result res
= _Unwind_VRS_Get(ctx
, _UVRSC_CORE
,
138 15 /* r15 = PC */, _UVRSD_UINT32
, &val
);
139 CHECK(res
== _UVRSR_OK
&& "_Unwind_VRS_Get failed");
140 // Clear the Thumb bit.
141 return val
& ~(uptr
)1;
143 return _Unwind_GetIP(ctx
);
147 struct UnwindTraceArg
{
152 _Unwind_Reason_Code
Unwind_Trace(struct _Unwind_Context
*ctx
, void *param
) {
153 UnwindTraceArg
*arg
= (UnwindTraceArg
*)param
;
154 CHECK_LT(arg
->stack
->size
, arg
->max_depth
);
155 uptr pc
= Unwind_GetIP(ctx
);
156 arg
->stack
->trace
[arg
->stack
->size
++] = pc
;
157 if (arg
->stack
->size
== arg
->max_depth
) return UNWIND_STOP
;
158 return UNWIND_CONTINUE
;
161 void StackTrace::SlowUnwindStack(uptr pc
, uptr max_depth
) {
165 UnwindTraceArg arg
= {this, Min(max_depth
+ 1, kStackTraceMax
)};
166 _Unwind_Backtrace(Unwind_Trace
, &arg
);
167 // We need to pop a few frames so that pc is on top.
168 uptr to_pop
= LocatePcInTrace(pc
);
169 // trace[0] belongs to the current function so we always pop it.
172 PopStackFrames(to_pop
);
176 #endif // !SANITIZER_GO
178 static uptr g_tls_size
;
181 # define DL_INTERNAL_FUNCTION __attribute__((regparm(3), stdcall))
183 # define DL_INTERNAL_FUNCTION
187 #if !defined(SANITIZER_GO) && !SANITIZER_ANDROID
188 typedef void (*get_tls_func
)(size_t*, size_t*) DL_INTERNAL_FUNCTION
;
189 get_tls_func get_tls
;
190 void *get_tls_static_info_ptr
= dlsym(RTLD_NEXT
, "_dl_get_tls_static_info");
191 CHECK_EQ(sizeof(get_tls
), sizeof(get_tls_static_info_ptr
));
192 internal_memcpy(&get_tls
, &get_tls_static_info_ptr
,
193 sizeof(get_tls_static_info_ptr
));
194 CHECK_NE(get_tls
, 0);
196 size_t tls_align
= 0;
197 get_tls(&tls_size
, &tls_align
);
198 g_tls_size
= tls_size
;
206 #if defined(__x86_64__) || defined(__i386__)
207 // sizeof(struct thread) from glibc.
208 // There has been a report of this being different on glibc 2.11 and 2.13. We
209 // don't know when this change happened, so 2.14 is a conservative estimate.
210 #if __GLIBC_PREREQ(2, 14)
211 const uptr kThreadDescriptorSize
= FIRST_32_SECOND_64(1216, 2304);
213 const uptr kThreadDescriptorSize
= FIRST_32_SECOND_64(1168, 2304);
216 uptr
ThreadDescriptorSize() {
217 return kThreadDescriptorSize
;
220 // The offset at which pointer to self is located in the thread descriptor.
221 const uptr kThreadSelfOffset
= FIRST_32_SECOND_64(8, 16);
223 uptr
ThreadSelfOffset() {
224 return kThreadSelfOffset
;
230 asm("mov %%gs:%c1,%0" : "=r"(descr_addr
) : "i"(kThreadSelfOffset
));
232 asm("mov %%fs:%c1,%0" : "=r"(descr_addr
) : "i"(kThreadSelfOffset
));
236 #endif // defined(__x86_64__) || defined(__i386__)
238 void GetThreadStackAndTls(bool main
, uptr
*stk_addr
, uptr
*stk_size
,
239 uptr
*tls_addr
, uptr
*tls_size
) {
241 #if defined(__x86_64__) || defined(__i386__)
242 *tls_addr
= ThreadSelf();
243 *tls_size
= GetTlsSize();
244 *tls_addr
-= *tls_size
;
245 *tls_addr
+= kThreadDescriptorSize
;
251 uptr stack_top
, stack_bottom
;
252 GetThreadStackTopAndBottom(main
, &stack_top
, &stack_bottom
);
253 *stk_addr
= stack_bottom
;
254 *stk_size
= stack_top
- stack_bottom
;
257 // If stack and tls intersect, make them non-intersecting.
258 if (*tls_addr
> *stk_addr
&& *tls_addr
< *stk_addr
+ *stk_size
) {
259 CHECK_GT(*tls_addr
+ *tls_size
, *stk_addr
);
260 CHECK_LE(*tls_addr
+ *tls_size
, *stk_addr
+ *stk_size
);
261 *stk_size
-= *tls_size
;
262 *tls_addr
= *stk_addr
+ *stk_size
;
265 #else // SANITIZER_GO
270 #endif // SANITIZER_GO
273 void AdjustStackSizeLinux(void *attr_
) {
274 pthread_attr_t
*attr
= (pthread_attr_t
*)attr_
;
276 size_t stacksize
= 0;
277 __sanitizer_pthread_attr_getstack(attr
, (void**)&stackaddr
, &stacksize
);
278 // GLibC will return (0 - stacksize) as the stack address in the case when
279 // stacksize is set, but stackaddr is not.
280 bool stack_set
= (stackaddr
!= 0) && (stackaddr
+ stacksize
!= 0);
281 // We place a lot of tool data into TLS, account for that.
282 const uptr minstacksize
= GetTlsSize() + 128*1024;
283 if (stacksize
< minstacksize
) {
285 if (common_flags()->verbosity
&& stacksize
!= 0)
286 Printf("Sanitizer: increasing stacksize %zu->%zu\n", stacksize
,
288 pthread_attr_setstacksize(attr
, minstacksize
);
290 Printf("Sanitizer: pre-allocated stack size is insufficient: "
291 "%zu < %zu\n", stacksize
, minstacksize
);
292 Printf("Sanitizer: pthread_create is likely to fail.\n");
297 #if SANITIZER_ANDROID
298 uptr
GetListOfModules(LoadedModule
*modules
, uptr max_modules
,
299 string_predicate_t filter
) {
302 #else // SANITIZER_ANDROID
303 typedef ElfW(Phdr
) Elf_Phdr
;
305 struct DlIteratePhdrData
{
306 LoadedModule
*modules
;
310 string_predicate_t filter
;
313 static int dl_iterate_phdr_cb(dl_phdr_info
*info
, size_t size
, void *arg
) {
314 DlIteratePhdrData
*data
= (DlIteratePhdrData
*)arg
;
315 if (data
->current_n
== data
->max_n
)
317 InternalScopedBuffer
<char> module_name(kMaxPathLength
);
318 module_name
.data()[0] = '\0';
321 // First module is the binary itself.
322 ReadBinaryName(module_name
.data(), module_name
.size());
323 } else if (info
->dlpi_name
) {
324 internal_strncpy(module_name
.data(), info
->dlpi_name
, module_name
.size());
326 if (module_name
.data()[0] == '\0')
328 if (data
->filter
&& !data
->filter(module_name
.data()))
330 void *mem
= &data
->modules
[data
->current_n
];
331 LoadedModule
*cur_module
= new(mem
) LoadedModule(module_name
.data(),
334 for (int i
= 0; i
< info
->dlpi_phnum
; i
++) {
335 const Elf_Phdr
*phdr
= &info
->dlpi_phdr
[i
];
336 if (phdr
->p_type
== PT_LOAD
) {
337 uptr cur_beg
= info
->dlpi_addr
+ phdr
->p_vaddr
;
338 uptr cur_end
= cur_beg
+ phdr
->p_memsz
;
339 cur_module
->addAddressRange(cur_beg
, cur_end
);
345 uptr
GetListOfModules(LoadedModule
*modules
, uptr max_modules
,
346 string_predicate_t filter
) {
348 DlIteratePhdrData data
= {modules
, 0, true, max_modules
, filter
};
349 dl_iterate_phdr(dl_iterate_phdr_cb
, &data
);
350 return data
.current_n
;
352 #endif // SANITIZER_ANDROID
354 } // namespace __sanitizer
356 #endif // SANITIZER_LINUX