blob: 17c28b0aea2a2be184af5e0a2fc5d59594c9b7d4 [file] [log] [blame]
Sergey Matveevda9f5e72013-10-14 12:01:05 +00001//===-- asan_posix.cc -----------------------------------------------------===//
Kostya Serebryany5be458c2012-01-09 19:18:27 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of AddressSanitizer, an address sanity checker.
11//
12// Posix-specific details.
13//===----------------------------------------------------------------------===//
Evgeniy Stepanov0af67232013-03-19 14:33:38 +000014
15#include "sanitizer_common/sanitizer_platform.h"
Alexey Samsonova7c602a2014-03-04 08:55:41 +000016#if SANITIZER_POSIX
Kostya Serebryany5be458c2012-01-09 19:18:27 +000017
18#include "asan_internal.h"
19#include "asan_interceptors.h"
Evgeniy Stepanov8152e222012-05-23 15:21:50 +000020#include "asan_mapping.h"
Alexey Samsonovaa051102012-08-09 07:40:58 +000021#include "asan_report.h"
Kostya Serebryany5be458c2012-01-09 19:18:27 +000022#include "asan_stack.h"
Alexey Samsonov1b12eda2012-06-05 08:48:10 +000023#include "sanitizer_common/sanitizer_libc.h"
Timur Iskhodzhanove8a6fbb2015-04-09 14:11:25 +000024#include "sanitizer_common/sanitizer_posix.h"
Alexey Samsonov28a98952012-06-07 06:15:12 +000025#include "sanitizer_common/sanitizer_procmaps.h"
Kostya Serebryany5be458c2012-01-09 19:18:27 +000026
Kostya Serebryany1dd51b52012-01-11 02:21:06 +000027#include <pthread.h>
Alexey Samsonovd6651502012-02-22 14:07:06 +000028#include <stdlib.h>
Kostya Serebryany5be458c2012-01-09 19:18:27 +000029#include <sys/time.h>
30#include <sys/resource.h>
Kostya Serebryanyedb4a8a2012-01-09 23:11:26 +000031#include <unistd.h>
Kostya Serebryany5be458c2012-01-09 19:18:27 +000032
33namespace __asan {
34
Kostya Serebryanycc9fd3c2015-08-06 17:52:54 +000035void AsanOnDeadlySignal(int signo, void *siginfo, void *context) {
Vitaly Bukad9d2a89e2017-09-13 04:46:37 +000036 StartReportDeadlySignal();
Vitaly Buka73c10162017-09-14 02:48:41 +000037 SignalContext sig(siginfo, context);
Vitaly Buka846a2172017-09-14 22:43:53 +000038 ReportDeadlySignal(sig);
Kostya Serebryany5be458c2012-01-09 19:18:27 +000039}
40
Kostya Serebryany1dd51b52012-01-11 02:21:06 +000041// ---------------------- TSD ---------------- {{{1
42
43static pthread_key_t tsd_key;
44static bool tsd_key_inited = false;
Kostya Serebryanyb5eb5a72012-02-07 00:27:15 +000045void AsanTSDInit(void (*destructor)(void *tsd)) {
Kostya Serebryany1dd51b52012-01-11 02:21:06 +000046 CHECK(!tsd_key_inited);
47 tsd_key_inited = true;
Kostya Serebryany5b4267f2013-04-05 14:40:25 +000048 CHECK_EQ(0, pthread_key_create(&tsd_key, destructor));
Kostya Serebryany1dd51b52012-01-11 02:21:06 +000049}
50
51void *AsanTSDGet() {
52 CHECK(tsd_key_inited);
53 return pthread_getspecific(tsd_key);
54}
55
56void AsanTSDSet(void *tsd) {
57 CHECK(tsd_key_inited);
58 pthread_setspecific(tsd_key, tsd);
59}
60
Sergey Matveevda9f5e72013-10-14 12:01:05 +000061void PlatformTSDDtor(void *tsd) {
62 AsanThreadContext *context = (AsanThreadContext*)tsd;
63 if (context->destructor_iterations > 1) {
64 context->destructor_iterations--;
65 CHECK_EQ(0, pthread_setspecific(tsd_key, tsd));
66 return;
67 }
68 AsanThread::TSDDtor(tsd);
69}
Kostya Serebryany5be458c2012-01-09 19:18:27 +000070} // namespace __asan
71
Alexey Samsonova7c602a2014-03-04 08:55:41 +000072#endif // SANITIZER_POSIX