xref: /netbsd-src/external/gpl3/gcc.old/dist/libsanitizer/sanitizer_common/sanitizer_linux_libcdep.cc (revision 4d342c046e3288fb5a1edcd33cfec48c41c80664)
1 //===-- sanitizer_linux_libcdep.cc ----------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is shared between AddressSanitizer and ThreadSanitizer
9 // run-time libraries and implements linux-specific functions from
10 // sanitizer_libc.h.
11 //===----------------------------------------------------------------------===//
12 
13 #include "sanitizer_platform.h"
14 
15 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD
16 
17 #include "sanitizer_allocator_internal.h"
18 #include "sanitizer_atomic.h"
19 #include "sanitizer_common.h"
20 #include "sanitizer_file.h"
21 #include "sanitizer_flags.h"
22 #include "sanitizer_freebsd.h"
23 #include "sanitizer_linux.h"
24 #include "sanitizer_placement_new.h"
25 #include "sanitizer_procmaps.h"
26 #include "sanitizer_stacktrace.h"
27 
28 #include <dlfcn.h>  // for dlsym()
29 #include <link.h>
30 #include <pthread.h>
31 #include <signal.h>
32 #include <sys/resource.h>
33 #include <syslog.h>
34 
35 #if SANITIZER_FREEBSD
36 #include <pthread_np.h>
37 #include <osreldate.h>
38 #define pthread_getattr_np pthread_attr_get_np
39 #endif
40 
41 #if SANITIZER_LINUX
42 #include <sys/prctl.h>
43 #endif
44 
45 #if SANITIZER_ANDROID
46 #include <android/api-level.h>
47 #endif
48 
49 #if SANITIZER_ANDROID && __ANDROID_API__ < 21
50 #include <android/log.h>
51 #endif
52 
53 #if !SANITIZER_ANDROID
54 #include <elf.h>
55 #include <unistd.h>
56 #endif
57 
58 namespace __sanitizer {
59 
60 SANITIZER_WEAK_ATTRIBUTE int
61 real_sigaction(int signum, const void *act, void *oldact);
62 
63 int internal_sigaction(int signum, const void *act, void *oldact) {
64 #if !SANITIZER_GO
65   if (&real_sigaction)
66     return real_sigaction(signum, act, oldact);
67 #endif
68   return sigaction(signum, (const struct sigaction *)act,
69                    (struct sigaction *)oldact);
70 }
71 
72 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
73                                 uptr *stack_bottom) {
74   CHECK(stack_top);
75   CHECK(stack_bottom);
76   if (at_initialization) {
77     // This is the main thread. Libpthread may not be initialized yet.
78     struct rlimit rl;
79     CHECK_EQ(getrlimit(RLIMIT_STACK, &rl), 0);
80 
81     // Find the mapping that contains a stack variable.
82     MemoryMappingLayout proc_maps(/*cache_enabled*/true);
83     MemoryMappedSegment segment;
84     uptr prev_end = 0;
85     while (proc_maps.Next(&segment)) {
86       if ((uptr)&rl < segment.end) break;
87       prev_end = segment.end;
88     }
89     CHECK((uptr)&rl >= segment.start && (uptr)&rl < segment.end);
90 
91     // Get stacksize from rlimit, but clip it so that it does not overlap
92     // with other mappings.
93     uptr stacksize = rl.rlim_cur;
94     if (stacksize > segment.end - prev_end) stacksize = segment.end - prev_end;
95     // When running with unlimited stack size, we still want to set some limit.
96     // The unlimited stack size is caused by 'ulimit -s unlimited'.
97     // Also, for some reason, GNU make spawns subprocesses with unlimited stack.
98     if (stacksize > kMaxThreadStackSize)
99       stacksize = kMaxThreadStackSize;
100     *stack_top = segment.end;
101     *stack_bottom = segment.end - stacksize;
102     return;
103   }
104   pthread_attr_t attr;
105   pthread_attr_init(&attr);
106   CHECK_EQ(pthread_getattr_np(pthread_self(), &attr), 0);
107   uptr stacksize = 0;
108   void *stackaddr = nullptr;
109   my_pthread_attr_getstack(&attr, &stackaddr, &stacksize);
110   pthread_attr_destroy(&attr);
111 
112   *stack_top = (uptr)stackaddr + stacksize;
113   *stack_bottom = (uptr)stackaddr;
114 }
115 
116 #if !SANITIZER_GO
117 bool SetEnv(const char *name, const char *value) {
118   void *f = dlsym(RTLD_NEXT, "setenv");
119   if (!f)
120     return false;
121   typedef int(*setenv_ft)(const char *name, const char *value, int overwrite);
122   setenv_ft setenv_f;
123   CHECK_EQ(sizeof(setenv_f), sizeof(f));
124   internal_memcpy(&setenv_f, &f, sizeof(f));
125   return setenv_f(name, value, 1) == 0;
126 }
127 #endif
128 
129 bool SanitizerSetThreadName(const char *name) {
130 #ifdef PR_SET_NAME
131   return 0 == prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0);  // NOLINT
132 #else
133   return false;
134 #endif
135 }
136 
137 bool SanitizerGetThreadName(char *name, int max_len) {
138 #ifdef PR_GET_NAME
139   char buff[17];
140   if (prctl(PR_GET_NAME, (unsigned long)buff, 0, 0, 0))  // NOLINT
141     return false;
142   internal_strncpy(name, buff, max_len);
143   name[max_len] = 0;
144   return true;
145 #else
146   return false;
147 #endif
148 }
149 
150 #ifndef __GLIBC_PREREQ
151 #define __GLIBC_PREREQ(x, y) 0
152 #endif
153 
154 #if !SANITIZER_FREEBSD && !SANITIZER_ANDROID && !SANITIZER_GO && \
155     !SANITIZER_NETBSD
156 static uptr g_tls_size;
157 
158 #if defined(__mips__) || defined(__powerpc64__)
159 // TlsPreTcbSize includes size of struct pthread_descr and size of tcb
160 // head structure. It lies before the static tls blocks.
161 static uptr TlsPreTcbSize() {
162 # if defined(__mips__)
163   const uptr kTcbHead = 16; // sizeof (tcbhead_t)
164 # elif defined(__powerpc64__)
165   const uptr kTcbHead = 88; // sizeof (tcbhead_t)
166 # endif
167   const uptr kTlsAlign = 16;
168   const uptr kTlsPreTcbSize =
169     (ThreadDescriptorSize() + kTcbHead + kTlsAlign - 1) & ~(kTlsAlign - 1);
170   InitTlsSize();
171   g_tls_size = (g_tls_size + kTlsPreTcbSize + kTlsAlign -1) & ~(kTlsAlign - 1);
172   return kTlsPreTcbSize;
173 }
174 #endif
175 
176 void InitTlsSize() {
177 // all current supported platforms have 16 bytes stack alignment
178   const size_t kStackAlign = 16;
179   size_t tls_size = 0;
180   size_t tls_align = 0;
181   void *get_tls_static_info_ptr = dlsym(RTLD_NEXT, "_dl_get_tls_static_info");
182 #if defined(__i386__) && !__GLIBC_PREREQ(2, 27)
183   /* On i?86, _dl_get_tls_static_info used to be internal_function, i.e.
184      __attribute__((regparm(3), stdcall)) before glibc 2.27 and is normal
185      function in 2.27 and later.  */
186   if (!dlvsym(RTLD_NEXT, "glob", "GLIBC_2.27")) {
187     typedef void (*get_tls_func)(size_t*, size_t*)
188       __attribute__((regparm(3), stdcall));
189     get_tls_func get_tls;
190     CHECK_EQ(sizeof(get_tls), sizeof(get_tls_static_info_ptr));
191     internal_memcpy(&get_tls, &get_tls_static_info_ptr,
192                     sizeof(get_tls_static_info_ptr));
193     CHECK_NE(get_tls, 0);
194     get_tls(&tls_size, &tls_align);
195   } else
196 #endif
197   {
198     typedef void (*get_tls_func)(size_t*, size_t*);
199     get_tls_func get_tls;
200     CHECK_EQ(sizeof(get_tls), sizeof(get_tls_static_info_ptr));
201     internal_memcpy(&get_tls, &get_tls_static_info_ptr,
202                     sizeof(get_tls_static_info_ptr));
203     CHECK_NE(get_tls, 0);
204     get_tls(&tls_size, &tls_align);
205   }
206   if (tls_align < kStackAlign)
207     tls_align = kStackAlign;
208   g_tls_size = RoundUpTo(tls_size, tls_align);
209 }
210 #else
211 void InitTlsSize() { }
212 #endif  // !SANITIZER_FREEBSD && !SANITIZER_ANDROID && !SANITIZER_GO &&
213         // !SANITIZER_NETBSD
214 
215 #if (defined(__x86_64__) || defined(__i386__) || defined(__mips__) \
216     || defined(__aarch64__) || defined(__powerpc64__) || defined(__s390__) \
217     || defined(__arm__)) && SANITIZER_LINUX && !SANITIZER_ANDROID
218 // sizeof(struct pthread) from glibc.
219 static atomic_uintptr_t kThreadDescriptorSize;
220 
221 uptr ThreadDescriptorSize() {
222   uptr val = atomic_load(&kThreadDescriptorSize, memory_order_relaxed);
223   if (val)
224     return val;
225 #if defined(__x86_64__) || defined(__i386__) || defined(__arm__)
226 #ifdef _CS_GNU_LIBC_VERSION
227   char buf[64];
228   uptr len = confstr(_CS_GNU_LIBC_VERSION, buf, sizeof(buf));
229   if (len < sizeof(buf) && internal_strncmp(buf, "glibc 2.", 8) == 0) {
230     char *end;
231     int minor = internal_simple_strtoll(buf + 8, &end, 10);
232     if (end != buf + 8 && (*end == '\0' || *end == '.' || *end == '-')) {
233       int patch = 0;
234       if (*end == '.')
235         // strtoll will return 0 if no valid conversion could be performed
236         patch = internal_simple_strtoll(end + 1, nullptr, 10);
237 
238       /* sizeof(struct pthread) values from various glibc versions.  */
239       if (SANITIZER_X32)
240         val = 1728;  // Assume only one particular version for x32.
241       // For ARM sizeof(struct pthread) changed in Glibc 2.23.
242       else if (SANITIZER_ARM)
243         val = minor <= 22 ? 1120 : 1216;
244       else if (minor <= 3)
245         val = FIRST_32_SECOND_64(1104, 1696);
246       else if (minor == 4)
247         val = FIRST_32_SECOND_64(1120, 1728);
248       else if (minor == 5)
249         val = FIRST_32_SECOND_64(1136, 1728);
250       else if (minor <= 9)
251         val = FIRST_32_SECOND_64(1136, 1712);
252       else if (minor == 10)
253         val = FIRST_32_SECOND_64(1168, 1776);
254       else if (minor == 11 || (minor == 12 && patch == 1))
255         val = FIRST_32_SECOND_64(1168, 2288);
256       else if (minor <= 14)
257         val = FIRST_32_SECOND_64(1168, 2304);
258       else
259         val = FIRST_32_SECOND_64(1216, 2304);
260     }
261     if (val)
262       atomic_store(&kThreadDescriptorSize, val, memory_order_relaxed);
263     return val;
264   }
265 #endif
266 #elif defined(__mips__)
267   // TODO(sagarthakur): add more values as per different glibc versions.
268   val = FIRST_32_SECOND_64(1152, 1776);
269   if (val)
270     atomic_store(&kThreadDescriptorSize, val, memory_order_relaxed);
271   return val;
272 #elif defined(__aarch64__)
273   // The sizeof (struct pthread) is the same from GLIBC 2.17 to 2.22.
274   val = 1776;
275   atomic_store(&kThreadDescriptorSize, val, memory_order_relaxed);
276   return val;
277 #elif defined(__powerpc64__)
278   val = 1776; // from glibc.ppc64le 2.20-8.fc21
279   atomic_store(&kThreadDescriptorSize, val, memory_order_relaxed);
280   return val;
281 #elif defined(__s390__)
282   val = FIRST_32_SECOND_64(1152, 1776); // valid for glibc 2.22
283   atomic_store(&kThreadDescriptorSize, val, memory_order_relaxed);
284 #endif
285   return 0;
286 }
287 
288 // The offset at which pointer to self is located in the thread descriptor.
289 const uptr kThreadSelfOffset = FIRST_32_SECOND_64(8, 16);
290 
291 uptr ThreadSelfOffset() {
292   return kThreadSelfOffset;
293 }
294 
295 #if defined(__mips__) || defined(__powerpc64__)
296 // TlsPreTcbSize includes size of struct pthread_descr and size of tcb
297 // head structure. It lies before the static tls blocks.
298 static uptr TlsPreTcbSize() {
299 # if defined(__mips__)
300   const uptr kTcbHead = 16; // sizeof (tcbhead_t)
301 # elif defined(__powerpc64__)
302   const uptr kTcbHead = 88; // sizeof (tcbhead_t)
303 # endif
304   const uptr kTlsAlign = 16;
305   const uptr kTlsPreTcbSize =
306       RoundUpTo(ThreadDescriptorSize() + kTcbHead, kTlsAlign);
307   return kTlsPreTcbSize;
308 }
309 #endif
310 
311 uptr ThreadSelf() {
312   uptr descr_addr;
313 # if defined(__i386__)
314   asm("mov %%gs:%c1,%0" : "=r"(descr_addr) : "i"(kThreadSelfOffset));
315 # elif defined(__x86_64__)
316   asm("mov %%fs:%c1,%0" : "=r"(descr_addr) : "i"(kThreadSelfOffset));
317 # elif defined(__mips__)
318   // MIPS uses TLS variant I. The thread pointer (in hardware register $29)
319   // points to the end of the TCB + 0x7000. The pthread_descr structure is
320   // immediately in front of the TCB. TlsPreTcbSize() includes the size of the
321   // TCB and the size of pthread_descr.
322   const uptr kTlsTcbOffset = 0x7000;
323   uptr thread_pointer;
324   asm volatile(".set push;\
325                 .set mips64r2;\
326                 rdhwr %0,$29;\
327                 .set pop" : "=r" (thread_pointer));
328   descr_addr = thread_pointer - kTlsTcbOffset - TlsPreTcbSize();
329 # elif defined(__aarch64__) || defined(__arm__)
330   descr_addr = reinterpret_cast<uptr>(__builtin_thread_pointer()) -
331                                       ThreadDescriptorSize();
332 # elif defined(__s390__)
333   descr_addr = reinterpret_cast<uptr>(__builtin_thread_pointer());
334 # elif defined(__powerpc64__)
335   // PPC64LE uses TLS variant I. The thread pointer (in GPR 13)
336   // points to the end of the TCB + 0x7000. The pthread_descr structure is
337   // immediately in front of the TCB. TlsPreTcbSize() includes the size of the
338   // TCB and the size of pthread_descr.
339   const uptr kTlsTcbOffset = 0x7000;
340   uptr thread_pointer;
341   asm("addi %0,13,%1" : "=r"(thread_pointer) : "I"(-kTlsTcbOffset));
342   descr_addr = thread_pointer - TlsPreTcbSize();
343 # else
344 #  error "unsupported CPU arch"
345 # endif
346   return descr_addr;
347 }
348 #endif  // (x86_64 || i386 || MIPS) && SANITIZER_LINUX
349 
350 #if SANITIZER_FREEBSD
351 static void **ThreadSelfSegbase() {
352   void **segbase = 0;
353 # if defined(__i386__)
354   // sysarch(I386_GET_GSBASE, segbase);
355   __asm __volatile("mov %%gs:0, %0" : "=r" (segbase));
356 # elif defined(__x86_64__)
357   // sysarch(AMD64_GET_FSBASE, segbase);
358   __asm __volatile("movq %%fs:0, %0" : "=r" (segbase));
359 # else
360 #  error "unsupported CPU arch for FreeBSD platform"
361 # endif
362   return segbase;
363 }
364 
365 uptr ThreadSelf() {
366   return (uptr)ThreadSelfSegbase()[2];
367 }
368 #elif SANITIZER_NETBSD
369 uptr ThreadSelf() { return (uptr)pthread_self(); }
370 #endif  // SANITIZER_NETBSD
371 
372 #if !SANITIZER_GO
373 static void GetTls(uptr *addr, uptr *size) {
374 #if SANITIZER_LINUX && !SANITIZER_ANDROID
375 # if defined(__x86_64__) || defined(__i386__) || defined(__s390__)
376   *addr = ThreadSelf();
377   *size = GetTlsSize();
378   *addr -= *size;
379   *addr += ThreadDescriptorSize();
380 # elif defined(__mips__) || defined(__aarch64__) || defined(__powerpc64__) \
381     || defined(__arm__)
382   *addr = ThreadSelf();
383   *size = GetTlsSize();
384 # else
385   *addr = 0;
386   *size = 0;
387 # endif
388 #elif SANITIZER_FREEBSD
389   void** segbase = ThreadSelfSegbase();
390   *addr = 0;
391   *size = 0;
392   if (segbase != 0) {
393     // tcbalign = 16
394     // tls_size = round(tls_static_space, tcbalign);
395     // dtv = segbase[1];
396     // dtv[2] = segbase - tls_static_space;
397     void **dtv = (void**) segbase[1];
398     *addr = (uptr) dtv[2];
399     *size = (*addr == 0) ? 0 : ((uptr) segbase[0] - (uptr) dtv[2]);
400   }
401 #elif SANITIZER_NETBSD
402   // XXX: for now
403   *addr = 0;
404   *size = 0;
405 #elif SANITIZER_ANDROID
406   *addr = 0;
407   *size = 0;
408 #else
409 # error "Unknown OS"
410 #endif
411 }
412 #endif
413 
414 #if !SANITIZER_GO
415 uptr GetTlsSize() {
416 #if SANITIZER_FREEBSD || SANITIZER_ANDROID || SANITIZER_NETBSD
417   uptr addr, size;
418   GetTls(&addr, &size);
419   return size;
420 #elif defined(__mips__) || defined(__powerpc64__)
421   return RoundUpTo(g_tls_size + TlsPreTcbSize(), 16);
422 #else
423   return g_tls_size;
424 #endif
425 }
426 #endif
427 
428 void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
429                           uptr *tls_addr, uptr *tls_size) {
430 #if SANITIZER_GO
431   // Stub implementation for Go.
432   *stk_addr = *stk_size = *tls_addr = *tls_size = 0;
433 #else
434   GetTls(tls_addr, tls_size);
435 
436   uptr stack_top, stack_bottom;
437   GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom);
438   *stk_addr = stack_bottom;
439   *stk_size = stack_top - stack_bottom;
440 
441   if (!main) {
442     // If stack and tls intersect, make them non-intersecting.
443     if (*tls_addr > *stk_addr && *tls_addr < *stk_addr + *stk_size) {
444       CHECK_GT(*tls_addr + *tls_size, *stk_addr);
445       CHECK_LE(*tls_addr + *tls_size, *stk_addr + *stk_size);
446       *stk_size -= *tls_size;
447       *tls_addr = *stk_addr + *stk_size;
448     }
449   }
450 #endif
451 }
452 
453 # if !SANITIZER_FREEBSD || SANITIZER_NETBSD
454 typedef ElfW(Phdr) Elf_Phdr;
455 # elif SANITIZER_WORDSIZE == 32 && __FreeBSD_version <= 902001  // v9.2
456 #  define Elf_Phdr XElf32_Phdr
457 #  define dl_phdr_info xdl_phdr_info
458 #  define dl_iterate_phdr(c, b) xdl_iterate_phdr((c), (b))
459 # endif
460 
461 struct DlIteratePhdrData {
462   InternalMmapVectorNoCtor<LoadedModule> *modules;
463   bool first;
464 };
465 
466 static int dl_iterate_phdr_cb(dl_phdr_info *info, size_t size, void *arg) {
467   DlIteratePhdrData *data = (DlIteratePhdrData*)arg;
468   InternalScopedString module_name(kMaxPathLength);
469   if (data->first) {
470     data->first = false;
471     // First module is the binary itself.
472     ReadBinaryNameCached(module_name.data(), module_name.size());
473   } else if (info->dlpi_name) {
474     module_name.append("%s", info->dlpi_name);
475   }
476   if (module_name[0] == '\0')
477     return 0;
478   LoadedModule cur_module;
479   cur_module.set(module_name.data(), info->dlpi_addr);
480   for (int i = 0; i < info->dlpi_phnum; i++) {
481     const Elf_Phdr *phdr = &info->dlpi_phdr[i];
482     if (phdr->p_type == PT_LOAD) {
483       uptr cur_beg = info->dlpi_addr + phdr->p_vaddr;
484       uptr cur_end = cur_beg + phdr->p_memsz;
485       bool executable = phdr->p_flags & PF_X;
486       bool writable = phdr->p_flags & PF_W;
487       cur_module.addAddressRange(cur_beg, cur_end, executable,
488                                  writable);
489     }
490   }
491   data->modules->push_back(cur_module);
492   return 0;
493 }
494 
495 #if SANITIZER_ANDROID && __ANDROID_API__ < 21
496 extern "C" __attribute__((weak)) int dl_iterate_phdr(
497     int (*)(struct dl_phdr_info *, size_t, void *), void *);
498 #endif
499 
500 static bool requiresProcmaps() {
501 #if SANITIZER_ANDROID && __ANDROID_API__ <= 22
502   // Fall back to /proc/maps if dl_iterate_phdr is unavailable or broken.
503   // The runtime check allows the same library to work with
504   // both K and L (and future) Android releases.
505   return AndroidGetApiLevel() <= ANDROID_LOLLIPOP_MR1;
506 #else
507   return false;
508 #endif
509 }
510 
511 static void procmapsInit(InternalMmapVectorNoCtor<LoadedModule> *modules) {
512   MemoryMappingLayout memory_mapping(/*cache_enabled*/true);
513   memory_mapping.DumpListOfModules(modules);
514 }
515 
516 void ListOfModules::init() {
517   clearOrInit();
518   if (requiresProcmaps()) {
519     procmapsInit(&modules_);
520   } else {
521     DlIteratePhdrData data = {&modules_, true};
522     dl_iterate_phdr(dl_iterate_phdr_cb, &data);
523   }
524 }
525 
526 // When a custom loader is used, dl_iterate_phdr may not contain the full
527 // list of modules. Allow callers to fall back to using procmaps.
528 void ListOfModules::fallbackInit() {
529   if (!requiresProcmaps()) {
530     clearOrInit();
531     procmapsInit(&modules_);
532   } else {
533     clear();
534   }
535 }
536 
537 // getrusage does not give us the current RSS, only the max RSS.
538 // Still, this is better than nothing if /proc/self/statm is not available
539 // for some reason, e.g. due to a sandbox.
540 static uptr GetRSSFromGetrusage() {
541   struct rusage usage;
542   if (getrusage(RUSAGE_SELF, &usage))  // Failed, probably due to a sandbox.
543     return 0;
544   return usage.ru_maxrss << 10;  // ru_maxrss is in Kb.
545 }
546 
547 uptr GetRSS() {
548   if (!common_flags()->can_use_proc_maps_statm)
549     return GetRSSFromGetrusage();
550   fd_t fd = OpenFile("/proc/self/statm", RdOnly);
551   if (fd == kInvalidFd)
552     return GetRSSFromGetrusage();
553   char buf[64];
554   uptr len = internal_read(fd, buf, sizeof(buf) - 1);
555   internal_close(fd);
556   if ((sptr)len <= 0)
557     return 0;
558   buf[len] = 0;
559   // The format of the file is:
560   // 1084 89 69 11 0 79 0
561   // We need the second number which is RSS in pages.
562   char *pos = buf;
563   // Skip the first number.
564   while (*pos >= '0' && *pos <= '9')
565     pos++;
566   // Skip whitespaces.
567   while (!(*pos >= '0' && *pos <= '9') && *pos != 0)
568     pos++;
569   // Read the number.
570   uptr rss = 0;
571   while (*pos >= '0' && *pos <= '9')
572     rss = rss * 10 + *pos++ - '0';
573   return rss * GetPageSizeCached();
574 }
575 
576 // 64-bit Android targets don't provide the deprecated __android_log_write.
577 // Starting with the L release, syslog() works and is preferable to
578 // __android_log_write.
579 #if SANITIZER_LINUX
580 
581 #if SANITIZER_ANDROID
582 static atomic_uint8_t android_log_initialized;
583 
584 void AndroidLogInit() {
585   openlog(GetProcessName(), 0, LOG_USER);
586   atomic_store(&android_log_initialized, 1, memory_order_release);
587 }
588 
589 static bool ShouldLogAfterPrintf() {
590   return atomic_load(&android_log_initialized, memory_order_acquire);
591 }
592 #else
593 void AndroidLogInit() {}
594 
595 static bool ShouldLogAfterPrintf() { return true; }
596 #endif  // SANITIZER_ANDROID
597 
598 void WriteOneLineToSyslog(const char *s) {
599 #if SANITIZER_ANDROID &&__ANDROID_API__ < 21
600   __android_log_write(ANDROID_LOG_INFO, NULL, s);
601 #else
602   syslog(LOG_INFO, "%s", s);
603 #endif
604 }
605 
606 void LogMessageOnPrintf(const char *str) {
607   if (common_flags()->log_to_syslog && ShouldLogAfterPrintf())
608     WriteToSyslog(str);
609 }
610 
611 #if SANITIZER_ANDROID
612 extern "C" __attribute__((weak)) void android_set_abort_message(const char *);
613 void SetAbortMessage(const char *str) {
614   if (&android_set_abort_message) android_set_abort_message(str);
615 }
616 #else
617 void SetAbortMessage(const char *str) {}
618 #endif
619 
620 #endif // SANITIZER_LINUX
621 
622 } // namespace __sanitizer
623 
624 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD
625