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_linux.h"
18 #include "sanitizer_placement_new.h"
19 #include "sanitizer_procmaps.h"
20 #include "sanitizer_stacktrace.h"
24 #include <sys/prctl.h>
25 #include <sys/resource.h>
28 #if !SANITIZER_ANDROID
33 namespace __sanitizer
{
35 void GetThreadStackTopAndBottom(bool at_initialization
, uptr
*stack_top
,
37 static const uptr kMaxThreadStackSize
= 1 << 30; // 1Gb
40 if (at_initialization
) {
41 // This is the main thread. Libpthread may not be initialized yet.
43 CHECK_EQ(getrlimit(RLIMIT_STACK
, &rl
), 0);
45 // Find the mapping that contains a stack variable.
46 MemoryMappingLayout
proc_maps(/*cache_enabled*/true);
47 uptr start
, end
, offset
;
49 while (proc_maps
.Next(&start
, &end
, &offset
, 0, 0, /* protection */0)) {
54 CHECK((uptr
)&rl
>= start
&& (uptr
)&rl
< end
);
56 // Get stacksize from rlimit, but clip it so that it does not overlap
57 // with other mappings.
58 uptr stacksize
= rl
.rlim_cur
;
59 if (stacksize
> end
- prev_end
)
60 stacksize
= end
- prev_end
;
61 // When running with unlimited stack size, we still want to set some limit.
62 // The unlimited stack size is caused by 'ulimit -s unlimited'.
63 // Also, for some reason, GNU make spawns subprocesses with unlimited stack.
64 if (stacksize
> kMaxThreadStackSize
)
65 stacksize
= kMaxThreadStackSize
;
67 *stack_bottom
= end
- stacksize
;
71 CHECK_EQ(pthread_getattr_np(pthread_self(), &attr
), 0);
74 pthread_attr_getstack(&attr
, &stackaddr
, (size_t*)&stacksize
);
75 pthread_attr_destroy(&attr
);
77 CHECK_LE(stacksize
, kMaxThreadStackSize
); // Sanity check.
78 *stack_top
= (uptr
)stackaddr
+ stacksize
;
79 *stack_bottom
= (uptr
)stackaddr
;
82 // Does not compile for Go because dlsym() requires -ldl
84 bool SetEnv(const char *name
, const char *value
) {
85 void *f
= dlsym(RTLD_NEXT
, "setenv");
88 typedef int(*setenv_ft
)(const char *name
, const char *value
, int overwrite
);
90 CHECK_EQ(sizeof(setenv_f
), sizeof(f
));
91 internal_memcpy(&setenv_f
, &f
, sizeof(f
));
92 return setenv_f(name
, value
, 1) == 0;
96 bool SanitizerSetThreadName(const char *name
) {
98 return 0 == prctl(PR_SET_NAME
, (unsigned long)name
, 0, 0, 0); // NOLINT
104 bool SanitizerGetThreadName(char *name
, int max_len
) {
107 if (prctl(PR_GET_NAME
, (unsigned long)buff
, 0, 0, 0)) // NOLINT
109 internal_strncpy(name
, buff
, max_len
);
118 //------------------------- SlowUnwindStack -----------------------------------
120 #define UNWIND_STOP _URC_END_OF_STACK
121 #define UNWIND_CONTINUE _URC_NO_REASON
123 #define UNWIND_STOP _URC_NORMAL_STOP
124 #define UNWIND_CONTINUE _URC_NO_REASON
127 uptr
Unwind_GetIP(struct _Unwind_Context
*ctx
) {
130 _Unwind_VRS_Result res
= _Unwind_VRS_Get(ctx
, _UVRSC_CORE
,
131 15 /* r15 = PC */, _UVRSD_UINT32
, &val
);
132 CHECK(res
== _UVRSR_OK
&& "_Unwind_VRS_Get failed");
133 // Clear the Thumb bit.
134 return val
& ~(uptr
)1;
136 return _Unwind_GetIP(ctx
);
140 _Unwind_Reason_Code
Unwind_Trace(struct _Unwind_Context
*ctx
, void *param
) {
141 StackTrace
*b
= (StackTrace
*)param
;
142 CHECK(b
->size
< b
->max_size
);
143 uptr pc
= Unwind_GetIP(ctx
);
144 b
->trace
[b
->size
++] = pc
;
145 if (b
->size
== b
->max_size
) return UNWIND_STOP
;
146 return UNWIND_CONTINUE
;
149 static bool MatchPc(uptr cur_pc
, uptr trace_pc
) {
150 return cur_pc
- trace_pc
<= 64 || trace_pc
- cur_pc
<= 64;
153 void StackTrace::SlowUnwindStack(uptr pc
, uptr max_depth
) {
155 this->max_size
= max_depth
;
157 _Unwind_Backtrace(Unwind_Trace
, this);
158 // We need to pop a few frames so that pc is on top.
159 // trace[0] belongs to the current function so we always pop it.
161 /**/ if (size
> 1 && MatchPc(pc
, trace
[1])) to_pop
= 1;
162 else if (size
> 2 && MatchPc(pc
, trace
[2])) to_pop
= 2;
163 else if (size
> 3 && MatchPc(pc
, trace
[3])) to_pop
= 3;
164 else if (size
> 4 && MatchPc(pc
, trace
[4])) to_pop
= 4;
165 else if (size
> 5 && MatchPc(pc
, trace
[5])) to_pop
= 5;
166 this->PopStackFrames(to_pop
);
171 #endif // !SANITIZER_GO
173 static uptr g_tls_size
;
176 # define DL_INTERNAL_FUNCTION __attribute__((regparm(3), stdcall))
178 # define DL_INTERNAL_FUNCTION
182 #if !defined(SANITIZER_GO) && !SANITIZER_ANDROID
183 typedef void (*get_tls_func
)(size_t*, size_t*) DL_INTERNAL_FUNCTION
;
184 get_tls_func get_tls
;
185 void *get_tls_static_info_ptr
= dlsym(RTLD_NEXT
, "_dl_get_tls_static_info");
186 CHECK_EQ(sizeof(get_tls
), sizeof(get_tls_static_info_ptr
));
187 internal_memcpy(&get_tls
, &get_tls_static_info_ptr
,
188 sizeof(get_tls_static_info_ptr
));
189 CHECK_NE(get_tls
, 0);
191 size_t tls_align
= 0;
192 get_tls(&tls_size
, &tls_align
);
193 g_tls_size
= tls_size
;
201 #if defined(__x86_64__) || defined(__i386__)
202 // sizeof(struct thread) from glibc.
203 // There has been a report of this being different on glibc 2.11 and 2.13. We
204 // don't know when this change happened, so 2.14 is a conservative estimate.
205 #if __GLIBC_PREREQ(2, 14)
206 const uptr kThreadDescriptorSize
= FIRST_32_SECOND_64(1216, 2304);
208 const uptr kThreadDescriptorSize
= FIRST_32_SECOND_64(1168, 2304);
211 uptr
ThreadDescriptorSize() {
212 return kThreadDescriptorSize
;
215 // The offset at which pointer to self is located in the thread descriptor.
216 const uptr kThreadSelfOffset
= FIRST_32_SECOND_64(8, 16);
218 uptr
ThreadSelfOffset() {
219 return kThreadSelfOffset
;
225 asm("mov %%gs:%c1,%0" : "=r"(descr_addr
) : "i"(kThreadSelfOffset
));
227 asm("mov %%fs:%c1,%0" : "=r"(descr_addr
) : "i"(kThreadSelfOffset
));
231 #endif // defined(__x86_64__) || defined(__i386__)
233 void GetThreadStackAndTls(bool main
, uptr
*stk_addr
, uptr
*stk_size
,
234 uptr
*tls_addr
, uptr
*tls_size
) {
236 #if defined(__x86_64__) || defined(__i386__)
237 *tls_addr
= ThreadSelf();
238 *tls_size
= GetTlsSize();
239 *tls_addr
-= *tls_size
;
240 *tls_addr
+= kThreadDescriptorSize
;
246 uptr stack_top
, stack_bottom
;
247 GetThreadStackTopAndBottom(main
, &stack_top
, &stack_bottom
);
248 *stk_addr
= stack_bottom
;
249 *stk_size
= stack_top
- stack_bottom
;
252 // If stack and tls intersect, make them non-intersecting.
253 if (*tls_addr
> *stk_addr
&& *tls_addr
< *stk_addr
+ *stk_size
) {
254 CHECK_GT(*tls_addr
+ *tls_size
, *stk_addr
);
255 CHECK_LE(*tls_addr
+ *tls_size
, *stk_addr
+ *stk_size
);
256 *stk_size
-= *tls_size
;
257 *tls_addr
= *stk_addr
+ *stk_size
;
260 #else // SANITIZER_GO
265 #endif // SANITIZER_GO
268 void AdjustStackSizeLinux(void *attr_
, int verbosity
) {
269 pthread_attr_t
*attr
= (pthread_attr_t
*)attr_
;
271 size_t stacksize
= 0;
272 pthread_attr_getstack(attr
, (void**)&stackaddr
, &stacksize
);
273 // GLibC will return (0 - stacksize) as the stack address in the case when
274 // stacksize is set, but stackaddr is not.
275 bool stack_set
= (stackaddr
!= 0) && (stackaddr
+ stacksize
!= 0);
276 // We place a lot of tool data into TLS, account for that.
277 const uptr minstacksize
= GetTlsSize() + 128*1024;
278 if (stacksize
< minstacksize
) {
280 if (verbosity
&& stacksize
!= 0)
281 Printf("Sanitizer: increasing stacksize %zu->%zu\n", stacksize
,
283 pthread_attr_setstacksize(attr
, minstacksize
);
285 Printf("Sanitizer: pre-allocated stack size is insufficient: "
286 "%zu < %zu\n", stacksize
, minstacksize
);
287 Printf("Sanitizer: pthread_create is likely to fail.\n");
292 #if SANITIZER_ANDROID
293 uptr
GetListOfModules(LoadedModule
*modules
, uptr max_modules
,
294 string_predicate_t filter
) {
297 #else // SANITIZER_ANDROID
298 typedef ElfW(Phdr
) Elf_Phdr
;
300 struct DlIteratePhdrData
{
301 LoadedModule
*modules
;
305 string_predicate_t filter
;
308 static int dl_iterate_phdr_cb(dl_phdr_info
*info
, size_t size
, void *arg
) {
309 DlIteratePhdrData
*data
= (DlIteratePhdrData
*)arg
;
310 if (data
->current_n
== data
->max_n
)
312 InternalScopedBuffer
<char> module_name(kMaxPathLength
);
313 module_name
.data()[0] = '\0';
316 // First module is the binary itself.
317 ReadBinaryName(module_name
.data(), module_name
.size());
318 } else if (info
->dlpi_name
) {
319 internal_strncpy(module_name
.data(), info
->dlpi_name
, module_name
.size());
321 if (module_name
.data()[0] == '\0')
323 if (data
->filter
&& !data
->filter(module_name
.data()))
325 void *mem
= &data
->modules
[data
->current_n
];
326 LoadedModule
*cur_module
= new(mem
) LoadedModule(module_name
.data(),
329 for (int i
= 0; i
< info
->dlpi_phnum
; i
++) {
330 const Elf_Phdr
*phdr
= &info
->dlpi_phdr
[i
];
331 if (phdr
->p_type
== PT_LOAD
) {
332 uptr cur_beg
= info
->dlpi_addr
+ phdr
->p_vaddr
;
333 uptr cur_end
= cur_beg
+ phdr
->p_memsz
;
334 cur_module
->addAddressRange(cur_beg
, cur_end
);
340 uptr
GetListOfModules(LoadedModule
*modules
, uptr max_modules
,
341 string_predicate_t filter
) {
343 DlIteratePhdrData data
= {modules
, 0, true, max_modules
, filter
};
344 dl_iterate_phdr(dl_iterate_phdr_cb
, &data
);
345 return data
.current_n
;
347 #endif // SANITIZER_ANDROID
349 } // namespace __sanitizer
351 #endif // SANITIZER_LINUX