]> git.proxmox.com Git - rustc.git/blob - src/compiler-rt/lib/esan/esan_sideline_linux.cpp
New upstream version 1.19.0+dfsg1
[rustc.git] / src / compiler-rt / lib / esan / esan_sideline_linux.cpp
1 //===-- esan_sideline_linux.cpp ---------------------------------*- C++ -*-===//
2 //
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 EfficiencySanitizer, a family of performance tuners.
11 //
12 // Support for a separate or "sideline" tool thread on Linux.
13 //===----------------------------------------------------------------------===//
14
15 #include "sanitizer_common/sanitizer_platform.h"
16 #if SANITIZER_LINUX
17
18 #include "esan_sideline.h"
19 #include "sanitizer_common/sanitizer_atomic.h"
20 #include "sanitizer_common/sanitizer_common.h"
21 #include "sanitizer_common/sanitizer_linux.h"
22 #include <errno.h>
23 #include <sched.h>
24 #include <sys/prctl.h>
25 #include <sys/signal.h>
26 #include <sys/time.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29
30 namespace __esan {
31
32 static const int SigAltStackSize = 4*1024;
33 static const int SidelineStackSize = 4*1024;
34 static const uptr SidelineIdUninitialized = 1;
35
36 // FIXME: we'll need some kind of TLS (can we trust that a pthread key will
37 // work in our non-POSIX thread?) to access our data in our signal handler
38 // with multiple sideline threads. For now we assume there is only one
39 // sideline thread and we use a dirty solution of a global var.
40 static SidelineThread *TheThread;
41
42 // We aren't passing SA_NODEFER so the same signal is blocked while here.
43 void SidelineThread::handleSidelineSignal(int SigNum, void *SigInfo,
44 void *Ctx) {
45 VPrintf(3, "Sideline signal %d\n", SigNum);
46 CHECK_EQ(SigNum, SIGALRM);
47 // See above about needing TLS to avoid this global var.
48 SidelineThread *Thread = TheThread;
49 if (atomic_load(&Thread->SidelineExit, memory_order_relaxed) != 0)
50 return;
51 Thread->sampleFunc(Thread->FuncArg);
52 }
53
54 void SidelineThread::registerSignal(int SigNum) {
55 __sanitizer_sigaction SigAct;
56 internal_memset(&SigAct, 0, sizeof(SigAct));
57 SigAct.sigaction = handleSidelineSignal;
58 // We do not pass SA_NODEFER as we want to block the same signal.
59 SigAct.sa_flags = SA_ONSTACK | SA_SIGINFO;
60 int Res = internal_sigaction(SigNum, &SigAct, nullptr);
61 CHECK_EQ(Res, 0);
62 }
63
64 int SidelineThread::runSideline(void *Arg) {
65 VPrintf(1, "Sideline thread starting\n");
66 SidelineThread *Thread = static_cast<SidelineThread*>(Arg);
67
68 // If the parent dies, we want to exit also.
69 internal_prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
70
71 // Set up a signal handler on an alternate stack for safety.
72 InternalScopedBuffer<char> StackMap(SigAltStackSize);
73 struct sigaltstack SigAltStack;
74 SigAltStack.ss_sp = StackMap.data();
75 SigAltStack.ss_size = SigAltStackSize;
76 SigAltStack.ss_flags = 0;
77 internal_sigaltstack(&SigAltStack, nullptr);
78
79 // We inherit the signal mask from the app thread. In case
80 // we weren't created at init time, we ensure the mask is empty.
81 __sanitizer_sigset_t SigSet;
82 internal_sigfillset(&SigSet);
83 int Res = internal_sigprocmask(SIG_UNBLOCK, &SigSet, nullptr);
84 CHECK_EQ(Res, 0);
85
86 registerSignal(SIGALRM);
87
88 bool TimerSuccess = Thread->adjustTimer(Thread->Freq);
89 CHECK(TimerSuccess);
90
91 // We loop, doing nothing but handling itimer signals.
92 while (atomic_load(&TheThread->SidelineExit, memory_order_relaxed) == 0)
93 sched_yield();
94
95 if (!Thread->adjustTimer(0))
96 VPrintf(1, "Failed to disable timer\n");
97
98 VPrintf(1, "Sideline thread exiting\n");
99 return 0;
100 }
101
102 bool SidelineThread::launchThread(SidelineFunc takeSample, void *Arg,
103 u32 FreqMilliSec) {
104 // This can only be called once. However, we can't clear a field in
105 // the constructor and check for that here as the constructor for
106 // a static instance is called *after* our module_ctor and thus after
107 // this routine! Thus we rely on the TheThread check below.
108 CHECK(TheThread == nullptr); // Only one sideline thread is supported.
109 TheThread = this;
110 sampleFunc = takeSample;
111 FuncArg = Arg;
112 Freq = FreqMilliSec;
113 atomic_store(&SidelineExit, 0, memory_order_relaxed);
114
115 // We do without a guard page.
116 Stack = static_cast<char*>(MmapOrDie(SidelineStackSize, "SidelineStack"));
117 // We need to handle the return value from internal_clone() not having been
118 // assigned yet (for our CHECK in adjustTimer()) so we ensure this has a
119 // sentinel value.
120 SidelineId = SidelineIdUninitialized;
121 // By omitting CLONE_THREAD, the child is in its own thread group and will not
122 // receive any of the application's signals.
123 SidelineId = internal_clone(
124 runSideline, Stack + SidelineStackSize,
125 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_UNTRACED,
126 this, nullptr /* parent_tidptr */,
127 nullptr /* newtls */, nullptr /* child_tidptr */);
128 int ErrCode;
129 if (internal_iserror(SidelineId, &ErrCode)) {
130 Printf("FATAL: EfficiencySanitizer failed to spawn a thread (code %d).\n",
131 ErrCode);
132 Die();
133 return false; // Not reached.
134 }
135 return true;
136 }
137
138 bool SidelineThread::joinThread() {
139 VPrintf(1, "Joining sideline thread\n");
140 bool Res = true;
141 atomic_store(&SidelineExit, 1, memory_order_relaxed);
142 while (true) {
143 uptr Status = internal_waitpid(SidelineId, nullptr, __WALL);
144 int ErrCode;
145 if (!internal_iserror(Status, &ErrCode))
146 break;
147 if (ErrCode == EINTR)
148 continue;
149 VPrintf(1, "Failed to join sideline thread (errno %d)\n", ErrCode);
150 Res = false;
151 break;
152 }
153 UnmapOrDie(Stack, SidelineStackSize);
154 return Res;
155 }
156
157 // Must be called from the sideline thread itself.
158 bool SidelineThread::adjustTimer(u32 FreqMilliSec) {
159 // The return value of internal_clone() may not have been assigned yet:
160 CHECK(internal_getpid() == SidelineId ||
161 SidelineId == SidelineIdUninitialized);
162 Freq = FreqMilliSec;
163 struct itimerval TimerVal;
164 TimerVal.it_interval.tv_sec = (time_t) Freq / 1000;
165 TimerVal.it_interval.tv_usec = (time_t) (Freq % 1000) * 1000;
166 TimerVal.it_value.tv_sec = (time_t) Freq / 1000;
167 TimerVal.it_value.tv_usec = (time_t) (Freq % 1000) * 1000;
168 // As we're in a different thread group, we cannot use either
169 // ITIMER_PROF or ITIMER_VIRTUAL without taking up scheduled
170 // time ourselves: thus we must use real time.
171 int Res = setitimer(ITIMER_REAL, &TimerVal, nullptr);
172 return (Res == 0);
173 }
174
175 } // namespace __esan
176
177 #endif // SANITIZER_LINUX