blob: 168b681e97447791a9c38bdf30ce54b4ab2ab503 [file] [log] [blame]
florian15fa8a22015-03-03 14:56:17 +00001/* -*- mode: C; c-basic-offset: 3; -*- */
sewardjde4a1d02002-03-22 01:27:54 +00002
3/*--------------------------------------------------------------------*/
njned6b8242005-06-01 00:03:17 +00004/*--- Implementation of POSIX signals. m_signals.c ---*/
sewardjde4a1d02002-03-22 01:27:54 +00005/*--------------------------------------------------------------------*/
njned6b8242005-06-01 00:03:17 +00006
sewardjde4a1d02002-03-22 01:27:54 +00007/*
njnb9c427c2004-12-01 14:14:42 +00008 This file is part of Valgrind, a dynamic binary instrumentation
9 framework.
sewardjde4a1d02002-03-22 01:27:54 +000010
sewardjb3a1e4b2015-08-21 11:32:26 +000011 Copyright (C) 2000-2015 Julian Seward
sewardjde4a1d02002-03-22 01:27:54 +000012 jseward@acm.org
sewardjde4a1d02002-03-22 01:27:54 +000013
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 02111-1307, USA.
28
njn25e49d8e72002-09-23 09:36:25 +000029 The GNU General Public License is contained in the file COPYING.
sewardjde4a1d02002-03-22 01:27:54 +000030*/
31
jsgf855d93d2003-10-13 22:26:55 +000032/*
sewardjb5f6f512005-03-10 23:59:00 +000033 Signal handling.
jsgf855d93d2003-10-13 22:26:55 +000034
sewardjb5f6f512005-03-10 23:59:00 +000035 There are 4 distinct classes of signal:
jsgf855d93d2003-10-13 22:26:55 +000036
sewardjb5f6f512005-03-10 23:59:00 +000037 1. Synchronous, instruction-generated (SIGILL, FPE, BUS, SEGV and
38 TRAP): these are signals as a result of an instruction fault. If
39 we get one while running client code, then we just do the
40 appropriate thing. If it happens while running Valgrind code, then
41 it indicates a Valgrind bug. Note that we "manually" implement
42 automatic stack growth, such that if a fault happens near the
43 client process stack, it is extended in the same way the kernel
44 would, and the fault is never reported to the client program.
jsgf855d93d2003-10-13 22:26:55 +000045
njn1ee02e32009-05-04 06:54:04 +000046 2. Asynchronous variants of the above signals: If the kernel tries
sewardjb5f6f512005-03-10 23:59:00 +000047 to deliver a sync signal while it is blocked, it just kills the
48 process. Therefore, we can't block those signals if we want to be
49 able to report on bugs in Valgrind. This means that we're also
50 open to receiving those signals from other processes, sent with
51 kill. We could get away with just dropping them, since they aren't
52 really signals that processes send to each other.
jsgf855d93d2003-10-13 22:26:55 +000053
sewardjb5f6f512005-03-10 23:59:00 +000054 3. Synchronous, general signals. If a thread/process sends itself
55 a signal with kill, its expected to be synchronous: ie, the signal
56 will have been delivered by the time the syscall finishes.
57
sewardj67b38c32010-01-01 12:44:12 +000058 4. Asynchronous, general signals. All other signals, sent by
sewardjb5f6f512005-03-10 23:59:00 +000059 another process with kill. These are generally blocked, except for
60 two special cases: we poll for them each time we're about to run a
61 thread for a time quanta, and while running blocking syscalls.
jsgf855d93d2003-10-13 22:26:55 +000062
jsgf855d93d2003-10-13 22:26:55 +000063
sewardjebf58442010-03-01 16:42:56 +000064 In addition, we reserve one signal for internal use: SIGVGKILL.
sewardjb5f6f512005-03-10 23:59:00 +000065 SIGVGKILL is used to terminate threads. When one thread wants
66 another to exit, it will set its exitreason and send it SIGVGKILL
67 if it appears to be blocked in a syscall.
68
69
70 We use a kernel thread for each application thread. When the
71 thread allows itself to be open to signals, it sets the thread
72 signal mask to what the client application set it to. This means
73 that we get the kernel to do all signal routing: under Valgrind,
74 signals get delivered in the same way as in the non-Valgrind case
75 (the exception being for the sync signal set, since they're almost
76 always unblocked).
jsgf855d93d2003-10-13 22:26:55 +000077 */
sewardjde4a1d02002-03-22 01:27:54 +000078
njn1ee02e32009-05-04 06:54:04 +000079/*
80 Some more details...
81
82 First off, we take note of the client's requests (via sys_sigaction
83 and sys_sigprocmask) to set the signal state (handlers for each
84 signal, which are process-wide, + a mask for each signal, which is
85 per-thread). This info is duly recorded in the SCSS (static Client
86 signal state) in m_signals.c, and if the client later queries what
87 the state is, we merely fish the relevant info out of SCSS and give
88 it back.
89
90 However, we set the real signal state in the kernel to something
91 entirely different. This is recorded in SKSS, the static Kernel
92 signal state. What's nice (to the extent that anything is nice w.r.t
93 signals) is that there's a pure function to calculate SKSS from SCSS,
94 calculate_SKSS_from_SCSS. So when the client changes SCSS then we
95 recompute the associated SKSS and apply any changes from the previous
96 SKSS through to the kernel.
97
98 Now, that said, the general scheme we have now is, that regardless of
99 what the client puts into the SCSS (viz, asks for), what we would
100 like to do is as follows:
101
102 (1) run code on the virtual CPU with all signals blocked
103
104 (2) at convenient moments for us (that is, when the VCPU stops, and
105 control is back with the scheduler), ask the kernel "do you have
106 any signals for me?" and if it does, collect up the info, and
107 deliver them to the client (by building sigframes).
108
109 And that's almost what we do. The signal polling is done by
110 VG_(poll_signals), which calls through to VG_(sigtimedwait_zero) to
111 do the dirty work. (of which more later).
112
113 By polling signals, rather than catching them, we get to deal with
114 them only at convenient moments, rather than having to recover from
115 taking a signal while generated code is running.
116
117 Now unfortunately .. the above scheme only works for so-called async
118 signals. An async signal is one which isn't associated with any
119 particular instruction, eg Control-C (SIGINT). For those, it doesn't
120 matter if we don't deliver the signal to the client immediately; it
121 only matters that we deliver it eventually. Hence polling is OK.
122
123 But the other group -- sync signals -- are all related by the fact
124 that they are various ways for the host CPU to fail to execute an
125 instruction: SIGILL, SIGSEGV, SIGFPU. And they can't be deferred,
126 because obviously if a host instruction can't execute, well then we
127 have to immediately do Plan B, whatever that is.
128
129 So the next approximation of what happens is:
130
131 (1) run code on vcpu with all async signals blocked
132
133 (2) at convenient moments (when NOT running the vcpu), poll for async
134 signals.
135
136 (1) and (2) together imply that if the host does deliver a signal to
137 async_signalhandler while the VCPU is running, something's
138 seriously wrong.
139
140 (3) when running code on vcpu, don't block sync signals. Instead
141 register sync_signalhandler and catch any such via that. Of
142 course, that means an ugly recovery path if we do -- the
143 sync_signalhandler has to longjump, exiting out of the generated
144 code, and the assembly-dispatcher thingy that runs it, and gets
145 caught in m_scheduler, which then tells m_signals to deliver the
146 signal.
147
148 Now naturally (ha ha) even that might be tolerable, but there's
149 something worse: dealing with signals delivered to threads in
150 syscalls.
151
152 Obviously from the above, SKSS's signal mask (viz, what we really run
153 with) is way different from SCSS's signal mask (viz, what the client
154 thread thought it asked for). (eg) It may well be that the client
155 did not block control-C, so that it just expects to drop dead if it
156 receives ^C whilst blocked in a syscall, but by default we are
157 running with all async signals blocked, and so that signal could be
158 arbitrarily delayed, or perhaps even lost (not sure).
159
160 So what we have to do, when doing any syscall which SfMayBlock, is to
161 quickly switch in the SCSS-specified signal mask just before the
162 syscall, and switch it back just afterwards, and hope that we don't
florianad4e9792015-07-05 21:53:33 +0000163 get caught up in some weird race condition. This is the primary
njn1ee02e32009-05-04 06:54:04 +0000164 purpose of the ultra-magical pieces of assembly code in
165 coregrind/m_syswrap/syscall-<plat>.S
166
167 -----------
168
169 The ways in which V can come to hear of signals that need to be
170 forwarded to the client as are follows:
171
172 sync signals: can arrive at any time whatsoever. These are caught
173 by sync_signalhandler
174
175 async signals:
176
177 if running generated code
178 then these are blocked, so we don't expect to catch them in
179 async_signalhandler
180
181 else
182 if thread is blocked in a syscall marked SfMayBlock
183 then signals may be delivered to async_sighandler, since we
184 temporarily unblocked them for the duration of the syscall,
185 by using the real (SCSS) mask for this thread
186
187 else we're doing misc housekeeping activities (eg, making a translation,
188 washing our hair, etc). As in the normal case, these signals are
189 blocked, but we can and do poll for them using VG_(poll_signals).
190
191 Now, re VG_(poll_signals), it polls the kernel by doing
192 VG_(sigtimedwait_zero). This is trivial on Linux, since it's just a
193 syscall. But on Darwin and AIX, we have to cobble together the
194 functionality in a tedious, longwinded and probably error-prone way.
sewardj3b290482011-05-06 21:02:55 +0000195
196 Finally, if a gdb is debugging the process under valgrind,
197 the signal can be ignored if gdb tells this. So, before resuming the
198 scheduler/delivering the signal, a call to VG_(gdbserver_report_signal)
199 is done. If this returns True, the signal is delivered.
njn1ee02e32009-05-04 06:54:04 +0000200 */
201
njnc7561b92005-06-19 01:24:32 +0000202#include "pub_core_basics.h"
sewardj4cfea4f2006-10-14 19:26:10 +0000203#include "pub_core_vki.h"
sewardj489bfec2006-10-17 02:00:29 +0000204#include "pub_core_vkiscnums.h"
sewardj45f4e7c2005-09-27 19:20:21 +0000205#include "pub_core_debuglog.h"
njnc7561b92005-06-19 01:24:32 +0000206#include "pub_core_threadstate.h"
sewardj14c7cc52007-02-25 15:08:24 +0000207#include "pub_core_xarray.h"
sewardj45f4e7c2005-09-27 19:20:21 +0000208#include "pub_core_clientstate.h"
njn2521d322005-05-08 14:45:13 +0000209#include "pub_core_aspacemgr.h"
njnd2b17112005-04-19 04:10:25 +0000210#include "pub_core_errormgr.h"
sewardj3b290482011-05-06 21:02:55 +0000211#include "pub_core_gdbserver.h"
njn97405b22005-06-02 03:39:33 +0000212#include "pub_core_libcbase.h"
njn132bfcc2005-06-04 19:16:06 +0000213#include "pub_core_libcassert.h"
njn36a20fa2005-06-03 03:08:39 +0000214#include "pub_core_libcprint.h"
njnf39e9a32005-06-12 02:43:17 +0000215#include "pub_core_libcproc.h"
njnde62cbf2005-06-10 22:08:14 +0000216#include "pub_core_libcsignal.h"
njnf536bbb2005-06-13 04:21:38 +0000217#include "pub_core_machine.h"
njnaf1d7df2005-06-11 01:31:52 +0000218#include "pub_core_mallocfree.h"
njn20242342005-05-16 23:31:24 +0000219#include "pub_core_options.h"
njnc7561b92005-06-19 01:24:32 +0000220#include "pub_core_scheduler.h"
njn0c246472005-05-31 01:00:08 +0000221#include "pub_core_signals.h"
njn24a6efb2005-06-20 03:36:51 +0000222#include "pub_core_sigframe.h" // For VG_(sigframe_create)()
njn945ed2e2005-06-24 03:28:30 +0000223#include "pub_core_stacks.h" // For VG_(change_stack)()
njn24a6efb2005-06-20 03:36:51 +0000224#include "pub_core_stacktrace.h" // For VG_(get_and_pp_StackTrace)()
njn9abd6082005-06-17 21:31:45 +0000225#include "pub_core_syscall.h"
njnc1b01812005-06-17 22:19:06 +0000226#include "pub_core_syswrap.h"
njn43b9a8a2005-05-10 04:37:01 +0000227#include "pub_core_tooliface.h"
sewardjcf4ac712005-11-01 00:03:40 +0000228#include "pub_core_coredump.h"
sewardj985fabb2005-04-24 14:18:14 +0000229
njnd2b17112005-04-19 04:10:25 +0000230
sewardj018f7622002-05-15 21:13:39 +0000231/* ---------------------------------------------------------------------
232 Forwards decls.
233 ------------------------------------------------------------------ */
234
njn5a350be2009-04-30 03:05:05 +0000235static void sync_signalhandler ( Int sigNo, vki_siginfo_t *info,
236 struct vki_ucontext * );
237static void async_signalhandler ( Int sigNo, vki_siginfo_t *info,
238 struct vki_ucontext * );
239static void sigvgkill_handler ( Int sigNo, vki_siginfo_t *info,
240 struct vki_ucontext * );
sewardj018f7622002-05-15 21:13:39 +0000241
sewardjb5f6f512005-03-10 23:59:00 +0000242/* Maximum usable signal. */
243Int VG_(max_signal) = _VKI_NSIG;
nethercote759dda32004-08-07 18:16:56 +0000244
sewardjb5f6f512005-03-10 23:59:00 +0000245#define N_QUEUED_SIGNALS 8
nethercote759dda32004-08-07 18:16:56 +0000246
sewardjb5f6f512005-03-10 23:59:00 +0000247typedef struct SigQueue {
248 Int next;
249 vki_siginfo_t sigs[N_QUEUED_SIGNALS];
250} SigQueue;
nethercote759dda32004-08-07 18:16:56 +0000251
sewardj489bfec2006-10-17 02:00:29 +0000252/* ------ Macros for pulling stuff out of ucontexts ------ */
253
njncda2f0f2009-05-18 02:12:08 +0000254/* Q: what does VG_UCONTEXT_SYSCALL_SYSRES do? A: let's suppose the
njn5a350be2009-04-30 03:05:05 +0000255 machine context (uc) reflects the situation that a syscall had just
256 completed, quite literally -- that is, that the program counter was
257 now at the instruction following the syscall. (or we're slightly
258 downstream, but we're sure no relevant register has yet changed
njncda2f0f2009-05-18 02:12:08 +0000259 value.) Then VG_UCONTEXT_SYSCALL_SYSRES returns a SysRes reflecting
njn5a350be2009-04-30 03:05:05 +0000260 the result of the syscall; it does this by fishing relevant bits of
261 the machine state out of the uc. Of course if the program counter
262 was somewhere else entirely then the result is likely to be
njncda2f0f2009-05-18 02:12:08 +0000263 meaningless, so the caller of VG_UCONTEXT_SYSCALL_SYSRES has to be
njn5a350be2009-04-30 03:05:05 +0000264 very careful to pay attention to the results only when it is sure
265 that the said constraint on the program counter is indeed valid. */
sewardjf5f1e122010-01-02 13:24:58 +0000266
njn605f4882005-05-29 17:50:40 +0000267#if defined(VGP_x86_linux)
njnaf839f52005-06-23 03:27:57 +0000268# define VG_UCONTEXT_INSTR_PTR(uc) ((uc)->uc_mcontext.eip)
269# define VG_UCONTEXT_STACK_PTR(uc) ((uc)->uc_mcontext.esp)
sewardjacaec5f2005-08-19 16:02:59 +0000270# define VG_UCONTEXT_SYSCALL_SYSRES(uc) \
sewardja8d8e232005-06-07 20:04:56 +0000271 /* Convert the value in uc_mcontext.eax into a SysRes. */ \
cerion85665ca2005-06-20 15:51:07 +0000272 VG_(mk_SysRes_x86_linux)( (uc)->uc_mcontext.eax )
sewardj59570ff2010-01-01 11:59:33 +0000273# define VG_UCONTEXT_TO_UnwindStartRegs(srP, uc) \
274 { (srP)->r_pc = (ULong)((uc)->uc_mcontext.eip); \
275 (srP)->r_sp = (ULong)((uc)->uc_mcontext.esp); \
276 (srP)->misc.X86.r_ebp = (uc)->uc_mcontext.ebp; \
277 }
sewardje7aa4ae2005-06-09 12:43:42 +0000278
njn605f4882005-05-29 17:50:40 +0000279#elif defined(VGP_amd64_linux)
njnaf839f52005-06-23 03:27:57 +0000280# define VG_UCONTEXT_INSTR_PTR(uc) ((uc)->uc_mcontext.rip)
281# define VG_UCONTEXT_STACK_PTR(uc) ((uc)->uc_mcontext.rsp)
sewardjacaec5f2005-08-19 16:02:59 +0000282# define VG_UCONTEXT_SYSCALL_SYSRES(uc) \
sewardje7aa4ae2005-06-09 12:43:42 +0000283 /* Convert the value in uc_mcontext.rax into a SysRes. */ \
cerion85665ca2005-06-20 15:51:07 +0000284 VG_(mk_SysRes_amd64_linux)( (uc)->uc_mcontext.rax )
sewardj59570ff2010-01-01 11:59:33 +0000285# define VG_UCONTEXT_TO_UnwindStartRegs(srP, uc) \
286 { (srP)->r_pc = (uc)->uc_mcontext.rip; \
287 (srP)->r_sp = (uc)->uc_mcontext.rsp; \
288 (srP)->misc.AMD64.r_rbp = (uc)->uc_mcontext.rbp; \
289 }
sewardje7aa4ae2005-06-09 12:43:42 +0000290
cerion85665ca2005-06-20 15:51:07 +0000291#elif defined(VGP_ppc32_linux)
sewardja36dcfa2005-11-25 02:16:58 +0000292/* Comments from Paul Mackerras 25 Nov 05:
293
294 > I'm tracking down a problem where V's signal handling doesn't
295 > work properly on a ppc440gx running 2.4.20. The problem is that
296 > the ucontext being presented to V's sighandler seems completely
297 > bogus.
298
299 > V's kernel headers and hence ucontext layout are derived from
300 > 2.6.9. I compared include/asm-ppc/ucontext.h from 2.4.20 and
301 > 2.6.13.
302
303 > Can I just check my interpretation: the 2.4.20 one contains the
304 > uc_mcontext field in line, whereas the 2.6.13 one has a pointer
305 > to said struct? And so if V is using the 2.6.13 struct then a
306 > 2.4.20 one will make no sense to it.
307
308 Not quite... what is inline in the 2.4.20 version is a
309 sigcontext_struct, not an mcontext. The sigcontext looks like
310 this:
311
312 struct sigcontext_struct {
313 unsigned long _unused[4];
314 int signal;
315 unsigned long handler;
316 unsigned long oldmask;
317 struct pt_regs *regs;
318 };
319
320 The regs pointer of that struct ends up at the same offset as the
321 uc_regs of the 2.6 struct ucontext, and a struct pt_regs is the
322 same as the mc_gregs field of the mcontext. In fact the integer
323 regs are followed in memory by the floating point regs on 2.4.20.
324
325 Thus if you are using the 2.6 definitions, it should work on 2.4.20
326 provided that you go via uc->uc_regs rather than looking in
327 uc->uc_mcontext directly.
328
329 There is another subtlety: 2.4.20 doesn't save the vector regs when
330 delivering a signal, and 2.6.x only saves the vector regs if the
331 process has ever used an altivec instructions. If 2.6.x does save
332 the vector regs, it sets the MSR_VEC bit in
333 uc->uc_regs->mc_gregs[PT_MSR], otherwise it clears it. That bit
334 will always be clear under 2.4.20. So you can use that bit to tell
335 whether uc->uc_regs->mc_vregs is valid. */
sewardjf5f1e122010-01-02 13:24:58 +0000336# define VG_UCONTEXT_INSTR_PTR(uc) ((uc)->uc_regs->mc_gregs[VKI_PT_NIP])
337# define VG_UCONTEXT_STACK_PTR(uc) ((uc)->uc_regs->mc_gregs[VKI_PT_R1])
sewardj39a7c1d2005-11-24 03:54:38 +0000338# define VG_UCONTEXT_SYSCALL_SYSRES(uc) \
339 /* Convert the values in uc_mcontext r3,cr into a SysRes. */ \
340 VG_(mk_SysRes_ppc32_linux)( \
sewardja36dcfa2005-11-25 02:16:58 +0000341 (uc)->uc_regs->mc_gregs[VKI_PT_R3], \
342 (((uc)->uc_regs->mc_gregs[VKI_PT_CCR] >> 28) & 1) \
sewardj39a7c1d2005-11-24 03:54:38 +0000343 )
sewardjf5f1e122010-01-02 13:24:58 +0000344# define VG_UCONTEXT_TO_UnwindStartRegs(srP, uc) \
345 { (srP)->r_pc = (ULong)((uc)->uc_regs->mc_gregs[VKI_PT_NIP]); \
346 (srP)->r_sp = (ULong)((uc)->uc_regs->mc_gregs[VKI_PT_R1]); \
347 (srP)->misc.PPC32.r_lr = (uc)->uc_regs->mc_gregs[VKI_PT_LNK]; \
348 }
cerion85665ca2005-06-20 15:51:07 +0000349
carllcae0cc22014-08-07 23:17:29 +0000350#elif defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux)
sewardjf5f1e122010-01-02 13:24:58 +0000351# define VG_UCONTEXT_INSTR_PTR(uc) ((uc)->uc_mcontext.gp_regs[VKI_PT_NIP])
352# define VG_UCONTEXT_STACK_PTR(uc) ((uc)->uc_mcontext.gp_regs[VKI_PT_R1])
sewardjdcb6ec82006-01-19 03:44:10 +0000353 /* Dubious hack: if there is an error, only consider the lowest 8
354 bits of r3. memcheck/tests/post-syscall shows a case where an
355 interrupted syscall should have produced a ucontext with 0x4
356 (VKI_EINTR) in r3 but is in fact producing 0x204. */
357 /* Awaiting clarification from PaulM. Evidently 0x204 is
358 ERESTART_RESTARTBLOCK, which shouldn't have made it into user
359 space. */
360 static inline SysRes VG_UCONTEXT_SYSCALL_SYSRES( struct vki_ucontext* uc )
361 {
362 ULong err = (uc->uc_mcontext.gp_regs[VKI_PT_CCR] >> 28) & 1;
363 ULong r3 = uc->uc_mcontext.gp_regs[VKI_PT_R3];
364 if (err) r3 &= 0xFF;
365 return VG_(mk_SysRes_ppc64_linux)( r3, err );
366 }
sewardjf5f1e122010-01-02 13:24:58 +0000367# define VG_UCONTEXT_TO_UnwindStartRegs(srP, uc) \
368 { (srP)->r_pc = (uc)->uc_mcontext.gp_regs[VKI_PT_NIP]; \
369 (srP)->r_sp = (uc)->uc_mcontext.gp_regs[VKI_PT_R1]; \
370 (srP)->misc.PPC64.r_lr = (uc)->uc_mcontext.gp_regs[VKI_PT_LNK]; \
371 }
sewardj2c48c7b2005-11-29 13:05:56 +0000372
sewardj59570ff2010-01-01 11:59:33 +0000373#elif defined(VGP_arm_linux)
374# define VG_UCONTEXT_INSTR_PTR(uc) ((uc)->uc_mcontext.arm_pc)
375# define VG_UCONTEXT_STACK_PTR(uc) ((uc)->uc_mcontext.arm_sp)
376# define VG_UCONTEXT_SYSCALL_SYSRES(uc) \
377 /* Convert the value in uc_mcontext.rax into a SysRes. */ \
378 VG_(mk_SysRes_arm_linux)( (uc)->uc_mcontext.arm_r0 )
379# define VG_UCONTEXT_TO_UnwindStartRegs(srP, uc) \
380 { (srP)->r_pc = (uc)->uc_mcontext.arm_pc; \
381 (srP)->r_sp = (uc)->uc_mcontext.arm_sp; \
382 (srP)->misc.ARM.r14 = (uc)->uc_mcontext.arm_lr; \
383 (srP)->misc.ARM.r12 = (uc)->uc_mcontext.arm_ip; \
384 (srP)->misc.ARM.r11 = (uc)->uc_mcontext.arm_fp; \
sewardjfa5ce562010-09-23 22:05:59 +0000385 (srP)->misc.ARM.r7 = (uc)->uc_mcontext.arm_r7; \
sewardj59570ff2010-01-01 11:59:33 +0000386 }
387
sewardjf0c12502014-01-12 12:54:00 +0000388#elif defined(VGP_arm64_linux)
389# define VG_UCONTEXT_INSTR_PTR(uc) ((UWord)((uc)->uc_mcontext.pc))
390# define VG_UCONTEXT_STACK_PTR(uc) ((UWord)((uc)->uc_mcontext.sp))
391# define VG_UCONTEXT_SYSCALL_SYSRES(uc) \
392 /* Convert the value in uc_mcontext.regs[0] into a SysRes. */ \
393 VG_(mk_SysRes_arm64_linux)( (uc)->uc_mcontext.regs[0] )
394# define VG_UCONTEXT_TO_UnwindStartRegs(srP, uc) \
395 { (srP)->r_pc = (uc)->uc_mcontext.pc; \
396 (srP)->r_sp = (uc)->uc_mcontext.sp; \
397 (srP)->misc.ARM64.x29 = (uc)->uc_mcontext.regs[29]; \
398 (srP)->misc.ARM64.x30 = (uc)->uc_mcontext.regs[30]; \
399 }
400
njnf76d27a2009-05-28 01:53:07 +0000401#elif defined(VGP_x86_darwin)
402
403 static inline Addr VG_UCONTEXT_INSTR_PTR( void* ucV ) {
404 ucontext_t* uc = (ucontext_t*)ucV;
405 struct __darwin_mcontext32* mc = uc->uc_mcontext;
406 struct __darwin_i386_thread_state* ss = &mc->__ss;
407 return ss->__eip;
408 }
409 static inline Addr VG_UCONTEXT_STACK_PTR( void* ucV ) {
410 ucontext_t* uc = (ucontext_t*)ucV;
411 struct __darwin_mcontext32* mc = uc->uc_mcontext;
412 struct __darwin_i386_thread_state* ss = &mc->__ss;
413 return ss->__esp;
414 }
415 static inline SysRes VG_UCONTEXT_SYSCALL_SYSRES( void* ucV,
416 UWord scclass ) {
417 /* this is complicated by the problem that there are 3 different
418 kinds of syscalls, each with its own return convention.
419 NB: scclass is a host word, hence UWord is good for both
420 amd64-darwin and x86-darwin */
421 ucontext_t* uc = (ucontext_t*)ucV;
422 struct __darwin_mcontext32* mc = uc->uc_mcontext;
423 struct __darwin_i386_thread_state* ss = &mc->__ss;
424 /* duplicates logic in m_syswrap.getSyscallStatusFromGuestState */
425 UInt carry = 1 & ss->__eflags;
426 UInt err = 0;
427 UInt wLO = 0;
428 UInt wHI = 0;
429 switch (scclass) {
430 case VG_DARWIN_SYSCALL_CLASS_UNIX:
431 err = carry;
432 wLO = ss->__eax;
433 wHI = ss->__edx;
434 break;
435 case VG_DARWIN_SYSCALL_CLASS_MACH:
436 wLO = ss->__eax;
437 break;
438 case VG_DARWIN_SYSCALL_CLASS_MDEP:
439 wLO = ss->__eax;
440 break;
441 default:
442 vg_assert(0);
443 break;
444 }
445 return VG_(mk_SysRes_x86_darwin)( scclass, err ? True : False,
446 wHI, wLO );
447 }
sewardj67b38c32010-01-01 12:44:12 +0000448 static inline
449 void VG_UCONTEXT_TO_UnwindStartRegs( UnwindStartRegs* srP,
450 void* ucV ) {
451 ucontext_t* uc = (ucontext_t*)(ucV);
njnf76d27a2009-05-28 01:53:07 +0000452 struct __darwin_mcontext32* mc = uc->uc_mcontext;
453 struct __darwin_i386_thread_state* ss = &mc->__ss;
sewardj67b38c32010-01-01 12:44:12 +0000454 srP->r_pc = (ULong)(ss->__eip);
455 srP->r_sp = (ULong)(ss->__esp);
456 srP->misc.X86.r_ebp = (UInt)(ss->__ebp);
njnf76d27a2009-05-28 01:53:07 +0000457 }
458
459#elif defined(VGP_amd64_darwin)
460
461 static inline Addr VG_UCONTEXT_INSTR_PTR( void* ucV ) {
sewardj625c3e72012-03-27 08:44:17 +0000462 ucontext_t* uc = (ucontext_t*)ucV;
463 struct __darwin_mcontext64* mc = uc->uc_mcontext;
464 struct __darwin_x86_thread_state64* ss = &mc->__ss;
465 return ss->__rip;
njnf76d27a2009-05-28 01:53:07 +0000466 }
467 static inline Addr VG_UCONTEXT_STACK_PTR( void* ucV ) {
sewardj625c3e72012-03-27 08:44:17 +0000468 ucontext_t* uc = (ucontext_t*)ucV;
469 struct __darwin_mcontext64* mc = uc->uc_mcontext;
470 struct __darwin_x86_thread_state64* ss = &mc->__ss;
471 return ss->__rsp;
njnf76d27a2009-05-28 01:53:07 +0000472 }
473 static inline SysRes VG_UCONTEXT_SYSCALL_SYSRES( void* ucV,
474 UWord scclass ) {
sewardj625c3e72012-03-27 08:44:17 +0000475 /* This is copied from the x86-darwin case. I'm not sure if it
476 is correct. */
477 ucontext_t* uc = (ucontext_t*)ucV;
478 struct __darwin_mcontext64* mc = uc->uc_mcontext;
479 struct __darwin_x86_thread_state64* ss = &mc->__ss;
480 /* duplicates logic in m_syswrap.getSyscallStatusFromGuestState */
481 ULong carry = 1 & ss->__rflags;
482 ULong err = 0;
483 ULong wLO = 0;
484 ULong wHI = 0;
485 switch (scclass) {
486 case VG_DARWIN_SYSCALL_CLASS_UNIX:
487 err = carry;
488 wLO = ss->__rax;
489 wHI = ss->__rdx;
490 break;
491 case VG_DARWIN_SYSCALL_CLASS_MACH:
492 wLO = ss->__rax;
493 break;
494 case VG_DARWIN_SYSCALL_CLASS_MDEP:
495 wLO = ss->__rax;
496 break;
497 default:
498 vg_assert(0);
499 break;
500 }
501 return VG_(mk_SysRes_amd64_darwin)( scclass, err ? True : False,
502 wHI, wLO );
njnf76d27a2009-05-28 01:53:07 +0000503 }
njnea2d6fd2010-07-01 00:20:20 +0000504 static inline
505 void VG_UCONTEXT_TO_UnwindStartRegs( UnwindStartRegs* srP,
506 void* ucV ) {
sewardj625c3e72012-03-27 08:44:17 +0000507 ucontext_t* uc = (ucontext_t*)ucV;
508 struct __darwin_mcontext64* mc = uc->uc_mcontext;
509 struct __darwin_x86_thread_state64* ss = &mc->__ss;
510 srP->r_pc = (ULong)(ss->__rip);
511 srP->r_sp = (ULong)(ss->__rsp);
512 srP->misc.AMD64.r_rbp = (ULong)(ss->__rbp);
njnf76d27a2009-05-28 01:53:07 +0000513 }
514
sewardjb5b87402011-03-07 16:05:35 +0000515#elif defined(VGP_s390x_linux)
516
517# define VG_UCONTEXT_INSTR_PTR(uc) ((uc)->uc_mcontext.regs.psw.addr)
518# define VG_UCONTEXT_STACK_PTR(uc) ((uc)->uc_mcontext.regs.gprs[15])
519# define VG_UCONTEXT_FRAME_PTR(uc) ((uc)->uc_mcontext.regs.gprs[11])
520# define VG_UCONTEXT_SYSCALL_SYSRES(uc) \
521 VG_(mk_SysRes_s390x_linux)((uc)->uc_mcontext.regs.gprs[2])
522# define VG_UCONTEXT_LINK_REG(uc) ((uc)->uc_mcontext.regs.gprs[14])
523
524# define VG_UCONTEXT_TO_UnwindStartRegs(srP, uc) \
525 { (srP)->r_pc = (ULong)((uc)->uc_mcontext.regs.psw.addr); \
526 (srP)->r_sp = (ULong)((uc)->uc_mcontext.regs.gprs[15]); \
527 (srP)->misc.S390X.r_fp = (uc)->uc_mcontext.regs.gprs[11]; \
528 (srP)->misc.S390X.r_lr = (uc)->uc_mcontext.regs.gprs[14]; \
529 }
530
sewardj5db15402012-06-07 09:13:21 +0000531#elif defined(VGP_mips32_linux)
532# define VG_UCONTEXT_INSTR_PTR(uc) ((UWord)(((uc)->uc_mcontext.sc_pc)))
533# define VG_UCONTEXT_STACK_PTR(uc) ((UWord)((uc)->uc_mcontext.sc_regs[29]))
534# define VG_UCONTEXT_FRAME_PTR(uc) ((uc)->uc_mcontext.sc_regs[30])
535# define VG_UCONTEXT_SYSCALL_NUM(uc) ((uc)->uc_mcontext.sc_regs[2])
536# define VG_UCONTEXT_SYSCALL_SYSRES(uc) \
537 /* Convert the value in uc_mcontext.rax into a SysRes. */ \
538 VG_(mk_SysRes_mips32_linux)( (uc)->uc_mcontext.sc_regs[2], \
539 (uc)->uc_mcontext.sc_regs[3], \
540 (uc)->uc_mcontext.sc_regs[7])
541
542# define VG_UCONTEXT_TO_UnwindStartRegs(srP, uc) \
543 { (srP)->r_pc = (uc)->uc_mcontext.sc_pc; \
544 (srP)->r_sp = (uc)->uc_mcontext.sc_regs[29]; \
545 (srP)->misc.MIPS32.r30 = (uc)->uc_mcontext.sc_regs[30]; \
546 (srP)->misc.MIPS32.r31 = (uc)->uc_mcontext.sc_regs[31]; \
547 (srP)->misc.MIPS32.r28 = (uc)->uc_mcontext.sc_regs[28]; \
548 }
549
petarj4df0bfc2013-02-27 23:17:33 +0000550#elif defined(VGP_mips64_linux)
551# define VG_UCONTEXT_INSTR_PTR(uc) (((uc)->uc_mcontext.sc_pc))
552# define VG_UCONTEXT_STACK_PTR(uc) ((uc)->uc_mcontext.sc_regs[29])
553# define VG_UCONTEXT_FRAME_PTR(uc) ((uc)->uc_mcontext.sc_regs[30])
554# define VG_UCONTEXT_SYSCALL_NUM(uc) ((uc)->uc_mcontext.sc_regs[2])
555# define VG_UCONTEXT_SYSCALL_SYSRES(uc) \
556 /* Convert the value in uc_mcontext.rax into a SysRes. */ \
557 VG_(mk_SysRes_mips64_linux)((uc)->uc_mcontext.sc_regs[2], \
558 (uc)->uc_mcontext.sc_regs[3], \
559 (uc)->uc_mcontext.sc_regs[7])
560
561# define VG_UCONTEXT_TO_UnwindStartRegs(srP, uc) \
562 { (srP)->r_pc = (uc)->uc_mcontext.sc_pc; \
563 (srP)->r_sp = (uc)->uc_mcontext.sc_regs[29]; \
564 (srP)->misc.MIPS64.r30 = (uc)->uc_mcontext.sc_regs[30]; \
565 (srP)->misc.MIPS64.r31 = (uc)->uc_mcontext.sc_regs[31]; \
566 (srP)->misc.MIPS64.r28 = (uc)->uc_mcontext.sc_regs[28]; \
567 }
sewardj8eb8bab2015-07-21 14:44:28 +0000568
sewardj112711a2015-04-10 12:30:09 +0000569#elif defined(VGP_tilegx_linux)
570# define VG_UCONTEXT_INSTR_PTR(uc) ((uc)->uc_mcontext.pc)
571# define VG_UCONTEXT_STACK_PTR(uc) ((uc)->uc_mcontext.sp)
572# define VG_UCONTEXT_FRAME_PTR(uc) ((uc)->uc_mcontext.gregs[52])
573# define VG_UCONTEXT_SYSCALL_NUM(uc) ((uc)->uc_mcontext.gregs[10])
574# define VG_UCONTEXT_SYSCALL_SYSRES(uc) \
575 /* Convert the value in uc_mcontext.rax into a SysRes. */ \
florian72dabf42015-04-20 21:13:03 +0000576 VG_(mk_SysRes_tilegx_linux)((uc)->uc_mcontext.gregs[0])
sewardj112711a2015-04-10 12:30:09 +0000577# define VG_UCONTEXT_TO_UnwindStartRegs(srP, uc) \
578 { (srP)->r_pc = (uc)->uc_mcontext.pc; \
579 (srP)->r_sp = (uc)->uc_mcontext.sp; \
580 (srP)->misc.TILEGX.r52 = (uc)->uc_mcontext.gregs[52]; \
581 (srP)->misc.TILEGX.r55 = (uc)->uc_mcontext.lr; \
582 }
sewardj8eb8bab2015-07-21 14:44:28 +0000583
584#elif defined(VGP_x86_solaris)
585# define VG_UCONTEXT_INSTR_PTR(uc) ((Addr)(uc)->uc_mcontext.gregs[VKI_EIP])
586# define VG_UCONTEXT_STACK_PTR(uc) ((Addr)(uc)->uc_mcontext.gregs[VKI_UESP])
587# define VG_UCONTEXT_SYSCALL_SYSRES(uc) \
588 VG_(mk_SysRes_x86_solaris)((uc)->uc_mcontext.gregs[VKI_EFL] & 1, \
589 (uc)->uc_mcontext.gregs[VKI_EAX], \
590 (uc)->uc_mcontext.gregs[VKI_EFL] & 1 \
591 ? 0 : (uc)->uc_mcontext.gregs[VKI_EDX])
592# define VG_UCONTEXT_TO_UnwindStartRegs(srP, uc) \
593 { (srP)->r_pc = (ULong)(uc)->uc_mcontext.gregs[VKI_EIP]; \
594 (srP)->r_sp = (ULong)(uc)->uc_mcontext.gregs[VKI_UESP]; \
595 (srP)->misc.X86.r_ebp = (uc)->uc_mcontext.gregs[VKI_EBP]; \
596 }
597
598#elif defined(VGP_amd64_solaris)
599# define VG_UCONTEXT_INSTR_PTR(uc) ((Addr)(uc)->uc_mcontext.gregs[VKI_REG_RIP])
600# define VG_UCONTEXT_STACK_PTR(uc) ((Addr)(uc)->uc_mcontext.gregs[VKI_REG_RSP])
601# define VG_UCONTEXT_SYSCALL_SYSRES(uc) \
602 VG_(mk_SysRes_amd64_solaris)((uc)->uc_mcontext.gregs[VKI_REG_RFL] & 1, \
603 (uc)->uc_mcontext.gregs[VKI_REG_RAX], \
604 (uc)->uc_mcontext.gregs[VKI_REG_RFL] & 1 \
605 ? 0 : (uc)->uc_mcontext.gregs[VKI_REG_RDX])
606# define VG_UCONTEXT_TO_UnwindStartRegs(srP, uc) \
607 { (srP)->r_pc = (uc)->uc_mcontext.gregs[VKI_REG_RIP]; \
608 (srP)->r_sp = (uc)->uc_mcontext.gregs[VKI_REG_RSP]; \
609 (srP)->misc.AMD64.r_rbp = (uc)->uc_mcontext.gregs[VKI_REG_RBP]; \
610 }
sewardj112711a2015-04-10 12:30:09 +0000611#else
njn605f4882005-05-29 17:50:40 +0000612# error Unknown platform
613#endif
614
sewardj489bfec2006-10-17 02:00:29 +0000615
616/* ------ Macros for pulling stuff out of siginfos ------ */
617
618/* These macros allow use of uniform names when working with
floriand2190292015-01-08 21:05:03 +0000619 both the Linux and Darwin vki definitions. */
sewardj489bfec2006-10-17 02:00:29 +0000620#if defined(VGO_linux)
621# define VKI_SIGINFO_si_addr _sifields._sigfault._addr
622# define VKI_SIGINFO_si_pid _sifields._kill._pid
sewardj8eb8bab2015-07-21 14:44:28 +0000623#elif defined(VGO_darwin) || defined(VGO_solaris)
njnf76d27a2009-05-28 01:53:07 +0000624# define VKI_SIGINFO_si_addr si_addr
625# define VKI_SIGINFO_si_pid si_pid
sewardj489bfec2006-10-17 02:00:29 +0000626#else
627# error Unknown OS
628#endif
629
630
nethercote759dda32004-08-07 18:16:56 +0000631/* ---------------------------------------------------------------------
sewardj018f7622002-05-15 21:13:39 +0000632 HIGH LEVEL STUFF TO DO WITH SIGNALS: POLICY (MOSTLY)
633 ------------------------------------------------------------------ */
634
sewardjde4a1d02002-03-22 01:27:54 +0000635/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000636 Signal state for this process.
637 ------------------------------------------------------------------ */
638
sewardj018f7622002-05-15 21:13:39 +0000639
nethercote73b526f2004-10-31 18:48:21 +0000640/* Base-ment of these arrays[_VKI_NSIG].
sewardjb48e5002002-05-13 00:16:03 +0000641
nethercote73b526f2004-10-31 18:48:21 +0000642 Valid signal numbers are 1 .. _VKI_NSIG inclusive.
sewardjb48e5002002-05-13 00:16:03 +0000643 Rather than subtracting 1 for indexing these arrays, which
644 is tedious and error-prone, they are simply dimensioned 1 larger,
645 and entry [0] is not used.
646 */
647
sewardjb48e5002002-05-13 00:16:03 +0000648
sewardj018f7622002-05-15 21:13:39 +0000649/* -----------------------------------------------------
650 Static client signal state (SCSS). This is the state
651 that the client thinks it has the kernel in.
652 SCSS records verbatim the client's settings. These
653 are mashed around only when SKSS is calculated from it.
654 -------------------------------------------------- */
sewardjb48e5002002-05-13 00:16:03 +0000655
sewardj018f7622002-05-15 21:13:39 +0000656typedef
657 struct {
658 void* scss_handler; /* VKI_SIG_DFL or VKI_SIG_IGN or ptr to
659 client's handler */
660 UInt scss_flags;
nethercote73b526f2004-10-31 18:48:21 +0000661 vki_sigset_t scss_mask;
sewardjb5f6f512005-03-10 23:59:00 +0000662 void* scss_restorer; /* where sigreturn goes */
njncda2f0f2009-05-18 02:12:08 +0000663 void* scss_sa_tramp; /* sa_tramp setting, Darwin only */
664 /* re _restorer and _sa_tramp, we merely record the values
665 supplied when the client does 'sigaction' and give them back
666 when requested. Otherwise they are simply ignored. */
sewardj018f7622002-05-15 21:13:39 +0000667 }
668 SCSS_Per_Signal;
sewardjb48e5002002-05-13 00:16:03 +0000669
sewardj018f7622002-05-15 21:13:39 +0000670typedef
671 struct {
sewardj2342c972002-05-22 23:34:20 +0000672 /* per-signal info */
nethercote73b526f2004-10-31 18:48:21 +0000673 SCSS_Per_Signal scss_per_sig[1+_VKI_NSIG];
sewardj2342c972002-05-22 23:34:20 +0000674
sewardj018f7622002-05-15 21:13:39 +0000675 /* Additional elements to SCSS not stored here:
676 - for each thread, the thread's blocking mask
677 - for each thread in WaitSIG, the set of waited-on sigs
678 */
679 }
680 SCSS;
sewardjb48e5002002-05-13 00:16:03 +0000681
njn695c16e2005-03-27 03:40:28 +0000682static SCSS scss;
sewardj018f7622002-05-15 21:13:39 +0000683
684
685/* -----------------------------------------------------
686 Static kernel signal state (SKSS). This is the state
687 that we have the kernel in. It is computed from SCSS.
688 -------------------------------------------------- */
689
690/* Let's do:
691 sigprocmask assigns to all thread masks
692 so that at least everything is always consistent
693 Flags:
jsgf855d93d2003-10-13 22:26:55 +0000694 SA_SIGINFO -- we always set it, and honour it for the client
sewardj018f7622002-05-15 21:13:39 +0000695 SA_NOCLDSTOP -- passed to kernel
sewardjb5f6f512005-03-10 23:59:00 +0000696 SA_ONESHOT or SA_RESETHAND -- pass through
jsgf855d93d2003-10-13 22:26:55 +0000697 SA_RESTART -- we observe this but set our handlers to always restart
sewardj8eb8bab2015-07-21 14:44:28 +0000698 (this doesn't apply to the Solaris port)
jsgf855d93d2003-10-13 22:26:55 +0000699 SA_NOMASK or SA_NODEFER -- we observe this, but our handlers block everything
sewardjb5f6f512005-03-10 23:59:00 +0000700 SA_ONSTACK -- pass through
701 SA_NOCLDWAIT -- pass through
sewardjb48e5002002-05-13 00:16:03 +0000702*/
sewardjde4a1d02002-03-22 01:27:54 +0000703
sewardj77e466c2002-04-14 02:29:29 +0000704
sewardj018f7622002-05-15 21:13:39 +0000705typedef
706 struct {
707 void* skss_handler; /* VKI_SIG_DFL or VKI_SIG_IGN
708 or ptr to our handler */
709 UInt skss_flags;
710 /* There is no skss_mask, since we know that we will always ask
jsgf855d93d2003-10-13 22:26:55 +0000711 for all signals to be blocked in our sighandlers. */
sewardj018f7622002-05-15 21:13:39 +0000712 /* Also there is no skss_restorer. */
713 }
714 SKSS_Per_Signal;
sewardjde4a1d02002-03-22 01:27:54 +0000715
sewardj018f7622002-05-15 21:13:39 +0000716typedef
717 struct {
nethercote73b526f2004-10-31 18:48:21 +0000718 SKSS_Per_Signal skss_per_sig[1+_VKI_NSIG];
sewardj018f7622002-05-15 21:13:39 +0000719 }
720 SKSS;
721
njn695c16e2005-03-27 03:40:28 +0000722static SKSS skss;
sewardjde4a1d02002-03-22 01:27:54 +0000723
sewardj3b290482011-05-06 21:02:55 +0000724/* returns True if signal is to be ignored.
725 To check this, possibly call gdbserver with tid. */
philippeb3014692015-05-17 13:38:25 +0000726static Bool is_sig_ign(vki_siginfo_t *info, ThreadId tid)
jsgf855d93d2003-10-13 22:26:55 +0000727{
philippeb3014692015-05-17 13:38:25 +0000728 vg_assert(info->si_signo >= 1 && info->si_signo <= _VKI_NSIG);
sewardj2e93c502002-04-12 11:12:52 +0000729
philippeb3014692015-05-17 13:38:25 +0000730 /* If VG_(gdbserver_report_signal) tells to report the signal,
731 then verify if this signal is not to be ignored. GDB might have
732 modified si_signo, so we check after the call to gdbserver. */
733 return !VG_(gdbserver_report_signal) (info, tid)
734 || scss.scss_per_sig[info->si_signo].scss_handler == VKI_SIG_IGN;
jsgf855d93d2003-10-13 22:26:55 +0000735}
sewardjb48e5002002-05-13 00:16:03 +0000736
sewardj018f7622002-05-15 21:13:39 +0000737/* ---------------------------------------------------------------------
738 Compute the SKSS required by the current SCSS.
739 ------------------------------------------------------------------ */
740
sewardj4f29ddf2002-05-03 22:29:04 +0000741static
sewardj018f7622002-05-15 21:13:39 +0000742void pp_SKSS ( void )
743{
744 Int sig;
745 VG_(printf)("\n\nSKSS:\n");
nethercote73b526f2004-10-31 18:48:21 +0000746 for (sig = 1; sig <= _VKI_NSIG; sig++) {
dirk61780382007-10-01 10:33:41 +0000747 VG_(printf)("sig %d: handler %p, flags 0x%x\n", sig,
njn695c16e2005-03-27 03:40:28 +0000748 skss.skss_per_sig[sig].skss_handler,
749 skss.skss_per_sig[sig].skss_flags );
sewardj77e466c2002-04-14 02:29:29 +0000750
sewardj018f7622002-05-15 21:13:39 +0000751 }
sewardj018f7622002-05-15 21:13:39 +0000752}
753
sewardj018f7622002-05-15 21:13:39 +0000754/* This is the core, clever bit. Computation is as follows:
755
756 For each signal
757 handler = if client has a handler, then our handler
jsgf855d93d2003-10-13 22:26:55 +0000758 else if client is DFL, then our handler as well
759 else (client must be IGN)
sewardjb5f6f512005-03-10 23:59:00 +0000760 then hander is IGN
sewardj018f7622002-05-15 21:13:39 +0000761*/
762static
763void calculate_SKSS_from_SCSS ( SKSS* dst )
764{
765 Int sig;
sewardj018f7622002-05-15 21:13:39 +0000766 UInt scss_flags;
767 UInt skss_flags;
768
nethercote73b526f2004-10-31 18:48:21 +0000769 for (sig = 1; sig <= _VKI_NSIG; sig++) {
jsgf855d93d2003-10-13 22:26:55 +0000770 void *skss_handler;
771 void *scss_handler;
772
njn695c16e2005-03-27 03:40:28 +0000773 scss_handler = scss.scss_per_sig[sig].scss_handler;
774 scss_flags = scss.scss_per_sig[sig].scss_flags;
sewardj018f7622002-05-15 21:13:39 +0000775
jsgf855d93d2003-10-13 22:26:55 +0000776 switch(sig) {
777 case VKI_SIGSEGV:
778 case VKI_SIGBUS:
779 case VKI_SIGFPE:
780 case VKI_SIGILL:
sewardjb5f6f512005-03-10 23:59:00 +0000781 case VKI_SIGTRAP:
jsgf855d93d2003-10-13 22:26:55 +0000782 /* For these, we always want to catch them and report, even
783 if the client code doesn't. */
njn695c16e2005-03-27 03:40:28 +0000784 skss_handler = sync_signalhandler;
jsgf855d93d2003-10-13 22:26:55 +0000785 break;
786
sewardjb5f6f512005-03-10 23:59:00 +0000787 case VKI_SIGCONT:
788 /* Let the kernel handle SIGCONT unless the client is actually
789 catching it. */
njn5a350be2009-04-30 03:05:05 +0000790 case VKI_SIGCHLD:
791 case VKI_SIGWINCH:
792 case VKI_SIGURG:
793 /* For signals which are have a default action of Ignore,
794 only set a handler if the client has set a signal handler.
795 Otherwise the kernel will interrupt a syscall which
796 wouldn't have otherwise been interrupted. */
njn695c16e2005-03-27 03:40:28 +0000797 if (scss.scss_per_sig[sig].scss_handler == VKI_SIG_DFL)
sewardjb5f6f512005-03-10 23:59:00 +0000798 skss_handler = VKI_SIG_DFL;
njn695c16e2005-03-27 03:40:28 +0000799 else if (scss.scss_per_sig[sig].scss_handler == VKI_SIG_IGN)
jsgf855d93d2003-10-13 22:26:55 +0000800 skss_handler = VKI_SIG_IGN;
sewardjb5f6f512005-03-10 23:59:00 +0000801 else
njn695c16e2005-03-27 03:40:28 +0000802 skss_handler = async_signalhandler;
jsgf855d93d2003-10-13 22:26:55 +0000803 break;
jsgf855d93d2003-10-13 22:26:55 +0000804
sewardjb5f6f512005-03-10 23:59:00 +0000805 default:
njna530fc62005-03-13 04:46:36 +0000806 // VKI_SIGVG* are runtime variables, so we can't make them
807 // cases in the switch, so we handle them in the 'default' case.
njn351d0062005-06-21 22:23:59 +0000808 if (sig == VG_SIGVGKILL)
sewardjb5f6f512005-03-10 23:59:00 +0000809 skss_handler = sigvgkill_handler;
sewardjb5f6f512005-03-10 23:59:00 +0000810 else {
811 if (scss_handler == VKI_SIG_IGN)
812 skss_handler = VKI_SIG_IGN;
813 else
njn695c16e2005-03-27 03:40:28 +0000814 skss_handler = async_signalhandler;
sewardjb5f6f512005-03-10 23:59:00 +0000815 }
816 break;
817 }
sewardj018f7622002-05-15 21:13:39 +0000818
sewardj018f7622002-05-15 21:13:39 +0000819 /* Flags */
820
821 skss_flags = 0;
jsgf855d93d2003-10-13 22:26:55 +0000822
sewardjb5f6f512005-03-10 23:59:00 +0000823 /* SA_NOCLDSTOP, SA_NOCLDWAIT: pass to kernel */
824 skss_flags |= scss_flags & (VKI_SA_NOCLDSTOP | VKI_SA_NOCLDWAIT);
jsgf855d93d2003-10-13 22:26:55 +0000825
sewardj018f7622002-05-15 21:13:39 +0000826 /* SA_ONESHOT: ignore client setting */
sewardjb5f6f512005-03-10 23:59:00 +0000827
sewardj8eb8bab2015-07-21 14:44:28 +0000828# if !defined(VGO_solaris)
sewardja8d8e232005-06-07 20:04:56 +0000829 /* SA_RESTART: ignore client setting and always set it for us.
830 Though we never rely on the kernel to restart a
jsgf855d93d2003-10-13 22:26:55 +0000831 syscall, we observe whether it wanted to restart the syscall
sewardja8d8e232005-06-07 20:04:56 +0000832 or not, which is needed by
833 VG_(fixup_guest_state_after_syscall_interrupted) */
sewardj018f7622002-05-15 21:13:39 +0000834 skss_flags |= VKI_SA_RESTART;
sewardj8eb8bab2015-07-21 14:44:28 +0000835#else
836 /* The above does not apply to the Solaris port, where the kernel does
837 not directly restart syscalls, but instead it checks SA_RESTART flag
838 and if it is set then it returns ERESTART to libc and the library
839 actually restarts the syscall. */
840 skss_flags |= scss_flags & VKI_SA_RESTART;
841# endif
jsgf855d93d2003-10-13 22:26:55 +0000842
843 /* SA_NOMASK: ignore it */
844
sewardj2342c972002-05-22 23:34:20 +0000845 /* SA_ONSTACK: client setting is irrelevant here */
sewardjb5f6f512005-03-10 23:59:00 +0000846 /* We don't set a signal stack, so ignore */
sewardj018f7622002-05-15 21:13:39 +0000847
jsgf855d93d2003-10-13 22:26:55 +0000848 /* always ask for SA_SIGINFO */
849 skss_flags |= VKI_SA_SIGINFO;
sewardj018f7622002-05-15 21:13:39 +0000850
fitzhardinge4f10ada2004-06-03 10:00:42 +0000851 /* use our own restorer */
852 skss_flags |= VKI_SA_RESTORER;
853
jsgf855d93d2003-10-13 22:26:55 +0000854 /* Create SKSS entry for this signal. */
sewardj6a3c26e2002-05-23 17:09:43 +0000855 if (sig != VKI_SIGKILL && sig != VKI_SIGSTOP)
856 dst->skss_per_sig[sig].skss_handler = skss_handler;
857 else
858 dst->skss_per_sig[sig].skss_handler = VKI_SIG_DFL;
859
sewardj018f7622002-05-15 21:13:39 +0000860 dst->skss_per_sig[sig].skss_flags = skss_flags;
861 }
862
863 /* Sanity checks. */
nethercote5fd72bb2004-11-04 19:28:38 +0000864 vg_assert(dst->skss_per_sig[VKI_SIGKILL].skss_handler == VKI_SIG_DFL);
865 vg_assert(dst->skss_per_sig[VKI_SIGSTOP].skss_handler == VKI_SIG_DFL);
sewardj018f7622002-05-15 21:13:39 +0000866
867 if (0)
868 pp_SKSS();
869}
870
871
872/* ---------------------------------------------------------------------
873 After a possible SCSS change, update SKSS and the kernel itself.
874 ------------------------------------------------------------------ */
875
njn9abd6082005-06-17 21:31:45 +0000876// We need two levels of macro-expansion here to convert __NR_rt_sigreturn
877// to a number before converting it to a string... sigh.
sewardj03d8aa82005-10-14 11:25:49 +0000878extern void my_sigreturn(void);
njn9abd6082005-06-17 21:31:45 +0000879
880#if defined(VGP_x86_linux)
njn5a350be2009-04-30 03:05:05 +0000881# define _MY_SIGRETURN(name) \
sewardjd9fc3822005-11-18 23:50:43 +0000882 ".text\n" \
philippe9fdca562012-04-16 22:06:47 +0000883 ".globl my_sigreturn\n" \
njn9abd6082005-06-17 21:31:45 +0000884 "my_sigreturn:\n" \
885 " movl $" #name ", %eax\n" \
sewardj2fedc642005-11-19 02:02:57 +0000886 " int $0x80\n" \
887 ".previous\n"
sewardj59570ff2010-01-01 11:59:33 +0000888
njn9abd6082005-06-17 21:31:45 +0000889#elif defined(VGP_amd64_linux)
njn5a350be2009-04-30 03:05:05 +0000890# define _MY_SIGRETURN(name) \
sewardjd9fc3822005-11-18 23:50:43 +0000891 ".text\n" \
philippe9fdca562012-04-16 22:06:47 +0000892 ".globl my_sigreturn\n" \
njn9abd6082005-06-17 21:31:45 +0000893 "my_sigreturn:\n" \
894 " movq $" #name ", %rax\n" \
sewardj2fedc642005-11-19 02:02:57 +0000895 " syscall\n" \
896 ".previous\n"
sewardj59570ff2010-01-01 11:59:33 +0000897
cerion85665ca2005-06-20 15:51:07 +0000898#elif defined(VGP_ppc32_linux)
njn5a350be2009-04-30 03:05:05 +0000899# define _MY_SIGRETURN(name) \
sewardjd9fc3822005-11-18 23:50:43 +0000900 ".text\n" \
philippe9fdca562012-04-16 22:06:47 +0000901 ".globl my_sigreturn\n" \
cerion85665ca2005-06-20 15:51:07 +0000902 "my_sigreturn:\n" \
903 " li 0, " #name "\n" \
sewardj2fedc642005-11-19 02:02:57 +0000904 " sc\n" \
905 ".previous\n"
sewardj59570ff2010-01-01 11:59:33 +0000906
carllcae0cc22014-08-07 23:17:29 +0000907#elif defined(VGP_ppc64be_linux)
njn5a350be2009-04-30 03:05:05 +0000908# define _MY_SIGRETURN(name) \
cerion297c88f2005-12-22 15:53:12 +0000909 ".align 2\n" \
910 ".globl my_sigreturn\n" \
911 ".section \".opd\",\"aw\"\n" \
912 ".align 3\n" \
sewardj2c48c7b2005-11-29 13:05:56 +0000913 "my_sigreturn:\n" \
cerion297c88f2005-12-22 15:53:12 +0000914 ".quad .my_sigreturn,.TOC.@tocbase,0\n" \
915 ".previous\n" \
916 ".type .my_sigreturn,@function\n" \
917 ".globl .my_sigreturn\n" \
918 ".my_sigreturn:\n" \
sewardj2c48c7b2005-11-29 13:05:56 +0000919 " li 0, " #name "\n" \
cerion297c88f2005-12-22 15:53:12 +0000920 " sc\n"
sewardj59570ff2010-01-01 11:59:33 +0000921
carll582d5822014-08-07 23:35:54 +0000922#elif defined(VGP_ppc64le_linux)
923/* Little Endian supports ELF version 2. In the future, it may
924 * support other versions.
925 */
926# define _MY_SIGRETURN(name) \
927 ".align 2\n" \
928 ".globl my_sigreturn\n" \
929 ".type .my_sigreturn,@function\n" \
930 "my_sigreturn:\n" \
931 "#if _CALL_ELF == 2 \n" \
932 "0: addis 2,12,.TOC.-0b@ha\n" \
933 " addi 2,2,.TOC.-0b@l\n" \
934 " .localentry my_sigreturn,.-my_sigreturn\n" \
935 "#endif \n" \
936 " sc\n" \
937 " .size my_sigreturn,.-my_sigreturn\n"
938
sewardj59570ff2010-01-01 11:59:33 +0000939#elif defined(VGP_arm_linux)
940# define _MY_SIGRETURN(name) \
941 ".text\n" \
philippe9fdca562012-04-16 22:06:47 +0000942 ".globl my_sigreturn\n" \
sewardj59570ff2010-01-01 11:59:33 +0000943 "my_sigreturn:\n\t" \
944 " mov r7, #" #name "\n\t" \
945 " svc 0x00000000\n" \
946 ".previous\n"
947
sewardjf0c12502014-01-12 12:54:00 +0000948#elif defined(VGP_arm64_linux)
949# define _MY_SIGRETURN(name) \
950 ".text\n" \
951 ".globl my_sigreturn\n" \
952 "my_sigreturn:\n\t" \
953 " mov x8, #" #name "\n\t" \
954 " svc 0x0\n" \
955 ".previous\n"
956
njnf76d27a2009-05-28 01:53:07 +0000957#elif defined(VGP_x86_darwin)
958# define _MY_SIGRETURN(name) \
959 ".text\n" \
philippe9fdca562012-04-16 22:06:47 +0000960 ".globl my_sigreturn\n" \
njnf76d27a2009-05-28 01:53:07 +0000961 "my_sigreturn:\n" \
rhyskidda53331c2015-10-15 07:01:57 +0000962 " movl $" VG_STRINGIFY(__NR_DARWIN_FAKE_SIGRETURN) ",%eax\n" \
963 " int $0x80\n"
sewardj59570ff2010-01-01 11:59:33 +0000964
njnf76d27a2009-05-28 01:53:07 +0000965#elif defined(VGP_amd64_darwin)
njnf76d27a2009-05-28 01:53:07 +0000966# define _MY_SIGRETURN(name) \
967 ".text\n" \
philippe9fdca562012-04-16 22:06:47 +0000968 ".globl my_sigreturn\n" \
njnf76d27a2009-05-28 01:53:07 +0000969 "my_sigreturn:\n" \
rhyskidda53331c2015-10-15 07:01:57 +0000970 " movq $" VG_STRINGIFY(__NR_DARWIN_FAKE_SIGRETURN) ",%rax\n" \
971 " syscall\n"
sewardj59570ff2010-01-01 11:59:33 +0000972
sewardjb5b87402011-03-07 16:05:35 +0000973#elif defined(VGP_s390x_linux)
974# define _MY_SIGRETURN(name) \
975 ".text\n" \
philippe9fdca562012-04-16 22:06:47 +0000976 ".globl my_sigreturn\n" \
sewardjb5b87402011-03-07 16:05:35 +0000977 "my_sigreturn:\n" \
978 " svc " #name "\n" \
979 ".previous\n"
980
sewardj5db15402012-06-07 09:13:21 +0000981#elif defined(VGP_mips32_linux)
982# define _MY_SIGRETURN(name) \
983 ".text\n" \
984 "my_sigreturn:\n" \
985 " li $2, " #name "\n" /* apparently $2 is v0 */ \
986 " syscall\n" \
987 ".previous\n"
988
petarj4df0bfc2013-02-27 23:17:33 +0000989#elif defined(VGP_mips64_linux)
990# define _MY_SIGRETURN(name) \
991 ".text\n" \
992 "my_sigreturn:\n" \
993 " li $2, " #name "\n" \
994 " syscall\n" \
995 ".previous\n"
996
sewardj112711a2015-04-10 12:30:09 +0000997#elif defined(VGP_tilegx_linux)
998# define _MY_SIGRETURN(name) \
999 ".text\n" \
1000 "my_sigreturn:\n" \
1001 " moveli r10 ," #name "\n" \
1002 " swint1\n" \
1003 ".previous\n"
1004
sewardj8eb8bab2015-07-21 14:44:28 +00001005#elif defined(VGP_x86_solaris) || defined(VGP_amd64_solaris)
1006/* Not used on Solaris. */
1007# define _MY_SIGRETURN(name) \
1008 ".text\n" \
1009 ".globl my_sigreturn\n" \
1010 "my_sigreturn:\n" \
1011 "ud2\n" \
1012 ".previous\n"
1013
njn9abd6082005-06-17 21:31:45 +00001014#else
1015# error Unknown platform
1016#endif
1017
njn5a350be2009-04-30 03:05:05 +00001018#define MY_SIGRETURN(name) _MY_SIGRETURN(name)
njn9abd6082005-06-17 21:31:45 +00001019asm(
njn5a350be2009-04-30 03:05:05 +00001020 MY_SIGRETURN(__NR_rt_sigreturn)
njn9abd6082005-06-17 21:31:45 +00001021);
1022
1023
nethercote9dd11512004-08-04 15:31:30 +00001024static void handle_SCSS_change ( Bool force_update )
sewardj018f7622002-05-15 21:13:39 +00001025{
nethercote73b526f2004-10-31 18:48:21 +00001026 Int res, sig;
1027 SKSS skss_old;
njncda2f0f2009-05-18 02:12:08 +00001028 vki_sigaction_toK_t ksa;
1029 vki_sigaction_fromK_t ksa_old;
sewardj018f7622002-05-15 21:13:39 +00001030
sewardj018f7622002-05-15 21:13:39 +00001031 /* Remember old SKSS and calculate new one. */
njn695c16e2005-03-27 03:40:28 +00001032 skss_old = skss;
1033 calculate_SKSS_from_SCSS ( &skss );
sewardj018f7622002-05-15 21:13:39 +00001034
1035 /* Compare the new SKSS entries vs the old ones, and update kernel
1036 where they differ. */
sewardjb5f6f512005-03-10 23:59:00 +00001037 for (sig = 1; sig <= VG_(max_signal); sig++) {
sewardj018f7622002-05-15 21:13:39 +00001038
1039 /* Trying to do anything with SIGKILL is pointless; just ignore
1040 it. */
1041 if (sig == VKI_SIGKILL || sig == VKI_SIGSTOP)
1042 continue;
1043
sewardj018f7622002-05-15 21:13:39 +00001044 if (!force_update) {
1045 if ((skss_old.skss_per_sig[sig].skss_handler
njn695c16e2005-03-27 03:40:28 +00001046 == skss.skss_per_sig[sig].skss_handler)
sewardj018f7622002-05-15 21:13:39 +00001047 && (skss_old.skss_per_sig[sig].skss_flags
njn695c16e2005-03-27 03:40:28 +00001048 == skss.skss_per_sig[sig].skss_flags))
sewardj018f7622002-05-15 21:13:39 +00001049 /* no difference */
1050 continue;
1051 }
1052
njn695c16e2005-03-27 03:40:28 +00001053 ksa.ksa_handler = skss.skss_per_sig[sig].skss_handler;
1054 ksa.sa_flags = skss.skss_per_sig[sig].skss_flags;
njnf76d27a2009-05-28 01:53:07 +00001055# if !defined(VGP_ppc32_linux) && \
sewardj5db15402012-06-07 09:13:21 +00001056 !defined(VGP_x86_darwin) && !defined(VGP_amd64_darwin) && \
sewardj8eb8bab2015-07-21 14:44:28 +00001057 !defined(VGP_mips32_linux) && !defined(VGO_solaris)
njn9abd6082005-06-17 21:31:45 +00001058 ksa.sa_restorer = my_sigreturn;
sewardjce2a6152005-07-08 18:25:13 +00001059# endif
sewardj162bebd2005-07-09 10:43:45 +00001060 /* Re above ifdef (also the assertion below), PaulM says:
1061 The sa_restorer field is not used at all on ppc. Glibc
1062 converts the sigaction you give it into a kernel sigaction,
1063 but it doesn't put anything in the sa_restorer field.
1064 */
fitzhardinge4f10ada2004-06-03 10:00:42 +00001065
sewardjb5f6f512005-03-10 23:59:00 +00001066 /* block all signals in handler */
nethercote73b526f2004-10-31 18:48:21 +00001067 VG_(sigfillset)( &ksa.sa_mask );
1068 VG_(sigdelset)( &ksa.sa_mask, VKI_SIGKILL );
1069 VG_(sigdelset)( &ksa.sa_mask, VKI_SIGSTOP );
sewardj018f7622002-05-15 21:13:39 +00001070
sewardjb5f6f512005-03-10 23:59:00 +00001071 if (VG_(clo_trace_signals) && VG_(clo_verbosity) > 2)
sewardj738856f2009-07-15 14:48:32 +00001072 VG_(dmsg)("setting ksig %d to: hdlr %p, flags 0x%lx, "
1073 "mask(msb..lsb) 0x%llx 0x%llx\n",
1074 sig, ksa.ksa_handler,
1075 (UWord)ksa.sa_flags,
1076 _VKI_NSIG_WORDS > 1 ? (ULong)ksa.sa_mask.sig[1] : 0,
1077 (ULong)ksa.sa_mask.sig[0]);
sewardj018f7622002-05-15 21:13:39 +00001078
nethercote73b526f2004-10-31 18:48:21 +00001079 res = VG_(sigaction)( sig, &ksa, &ksa_old );
sewardj018f7622002-05-15 21:13:39 +00001080 vg_assert(res == 0);
1081
1082 /* Since we got the old sigaction more or less for free, might
1083 as well extract the maximum sanity-check value from it. */
1084 if (!force_update) {
1085 vg_assert(ksa_old.ksa_handler
1086 == skss_old.skss_per_sig[sig].skss_handler);
sewardj8eb8bab2015-07-21 14:44:28 +00001087# if defined(VGO_solaris)
1088 if (ksa_old.ksa_handler == VKI_SIG_DFL
1089 || ksa_old.ksa_handler == VKI_SIG_IGN) {
1090 /* The Solaris kernel ignores signal flags (except SA_NOCLDWAIT
1091 and SA_NOCLDSTOP) and a signal mask if a handler is set to
1092 SIG_DFL or SIG_IGN. */
1093 skss_old.skss_per_sig[sig].skss_flags
1094 &= (VKI_SA_NOCLDWAIT | VKI_SA_NOCLDSTOP);
1095 vg_assert(VG_(isemptysigset)( &ksa_old.sa_mask ));
1096 VG_(sigfillset)( &ksa_old.sa_mask );
1097 }
1098# endif
nethercote73b526f2004-10-31 18:48:21 +00001099 vg_assert(ksa_old.sa_flags
sewardj018f7622002-05-15 21:13:39 +00001100 == skss_old.skss_per_sig[sig].skss_flags);
njnf76d27a2009-05-28 01:53:07 +00001101# if !defined(VGP_ppc32_linux) && \
sewardj5db15402012-06-07 09:13:21 +00001102 !defined(VGP_x86_darwin) && !defined(VGP_amd64_darwin) && \
sewardj8eb8bab2015-07-21 14:44:28 +00001103 !defined(VGP_mips32_linux) && !defined(VGP_mips64_linux) && \
1104 !defined(VGO_solaris)
sewardjf0c12502014-01-12 12:54:00 +00001105 vg_assert(ksa_old.sa_restorer == my_sigreturn);
sewardjce2a6152005-07-08 18:25:13 +00001106# endif
nethercote73b526f2004-10-31 18:48:21 +00001107 VG_(sigaddset)( &ksa_old.sa_mask, VKI_SIGKILL );
1108 VG_(sigaddset)( &ksa_old.sa_mask, VKI_SIGSTOP );
1109 vg_assert(VG_(isfullsigset)( &ksa_old.sa_mask ));
sewardj018f7622002-05-15 21:13:39 +00001110 }
1111 }
sewardj018f7622002-05-15 21:13:39 +00001112}
1113
1114
1115/* ---------------------------------------------------------------------
1116 Update/query SCSS in accordance with client requests.
1117 ------------------------------------------------------------------ */
1118
sewardj2342c972002-05-22 23:34:20 +00001119/* Logic for this alt-stack stuff copied directly from do_sigaltstack
1120 in kernel/signal.[ch] */
1121
1122/* True if we are on the alternate signal stack. */
sewardjb5f6f512005-03-10 23:59:00 +00001123static Bool on_sig_stack ( ThreadId tid, Addr m_SP )
sewardj2342c972002-05-22 23:34:20 +00001124{
fitzhardinge98c4dc02004-03-16 08:27:29 +00001125 ThreadState *tst = VG_(get_ThreadState)(tid);
1126
njn5a350be2009-04-30 03:05:05 +00001127 return (m_SP - (Addr)tst->altstack.ss_sp < (Addr)tst->altstack.ss_size);
sewardj2342c972002-05-22 23:34:20 +00001128}
1129
nethercote511e4062004-09-11 13:34:08 +00001130static Int sas_ss_flags ( ThreadId tid, Addr m_SP )
sewardj2342c972002-05-22 23:34:20 +00001131{
fitzhardinge98c4dc02004-03-16 08:27:29 +00001132 ThreadState *tst = VG_(get_ThreadState)(tid);
1133
1134 return (tst->altstack.ss_size == 0
sewardj2342c972002-05-22 23:34:20 +00001135 ? VKI_SS_DISABLE
nethercote511e4062004-09-11 13:34:08 +00001136 : on_sig_stack(tid, m_SP) ? VKI_SS_ONSTACK : 0);
sewardj2342c972002-05-22 23:34:20 +00001137}
1138
1139
sewardja8d8e232005-06-07 20:04:56 +00001140SysRes VG_(do_sys_sigaltstack) ( ThreadId tid, vki_stack_t* ss, vki_stack_t* oss )
sewardj2342c972002-05-22 23:34:20 +00001141{
njn502badb2005-05-08 02:04:49 +00001142 Addr m_SP;
sewardj2342c972002-05-22 23:34:20 +00001143
1144 vg_assert(VG_(is_valid_tid)(tid));
njnf536bbb2005-06-13 04:21:38 +00001145 m_SP = VG_(get_SP)(tid);
sewardj2342c972002-05-22 23:34:20 +00001146
1147 if (VG_(clo_trace_signals))
floriana5e06c32015-08-05 21:16:09 +00001148 VG_(dmsg)("sys_sigaltstack: tid %u, "
sewardj738856f2009-07-15 14:48:32 +00001149 "ss %p{%p,sz=%llu,flags=0x%llx}, oss %p (current SP %p)\n",
1150 tid, (void*)ss,
1151 ss ? ss->ss_sp : 0,
1152 (ULong)(ss ? ss->ss_size : 0),
1153 (ULong)(ss ? ss->ss_flags : 0),
1154 (void*)oss, (void*)m_SP);
sewardj2342c972002-05-22 23:34:20 +00001155
1156 if (oss != NULL) {
fitzhardinge98c4dc02004-03-16 08:27:29 +00001157 oss->ss_sp = VG_(threads)[tid].altstack.ss_sp;
1158 oss->ss_size = VG_(threads)[tid].altstack.ss_size;
njn5a350be2009-04-30 03:05:05 +00001159 oss->ss_flags = VG_(threads)[tid].altstack.ss_flags
1160 | sas_ss_flags(tid, m_SP);
sewardj2342c972002-05-22 23:34:20 +00001161 }
1162
1163 if (ss != NULL) {
njnf536bbb2005-06-13 04:21:38 +00001164 if (on_sig_stack(tid, VG_(get_SP)(tid))) {
sewardja8d8e232005-06-07 20:04:56 +00001165 return VG_(mk_SysRes_Error)( VKI_EPERM );
sewardj2342c972002-05-22 23:34:20 +00001166 }
1167 if (ss->ss_flags != VKI_SS_DISABLE
1168 && ss->ss_flags != VKI_SS_ONSTACK
1169 && ss->ss_flags != 0) {
sewardja8d8e232005-06-07 20:04:56 +00001170 return VG_(mk_SysRes_Error)( VKI_EINVAL );
sewardj2342c972002-05-22 23:34:20 +00001171 }
1172 if (ss->ss_flags == VKI_SS_DISABLE) {
fitzhardinge98c4dc02004-03-16 08:27:29 +00001173 VG_(threads)[tid].altstack.ss_flags = VKI_SS_DISABLE;
sewardj2342c972002-05-22 23:34:20 +00001174 } else {
1175 if (ss->ss_size < VKI_MINSIGSTKSZ) {
sewardja8d8e232005-06-07 20:04:56 +00001176 return VG_(mk_SysRes_Error)( VKI_ENOMEM );
sewardj2342c972002-05-22 23:34:20 +00001177 }
jsgf855d93d2003-10-13 22:26:55 +00001178
nethercote20283c62004-11-04 19:43:22 +00001179 VG_(threads)[tid].altstack.ss_sp = ss->ss_sp;
1180 VG_(threads)[tid].altstack.ss_size = ss->ss_size;
fitzhardinge98c4dc02004-03-16 08:27:29 +00001181 VG_(threads)[tid].altstack.ss_flags = 0;
sewardj2342c972002-05-22 23:34:20 +00001182 }
sewardj2342c972002-05-22 23:34:20 +00001183 }
sewardja8d8e232005-06-07 20:04:56 +00001184 return VG_(mk_SysRes_Success)( 0 );
sewardj2342c972002-05-22 23:34:20 +00001185}
1186
1187
sewardja8d8e232005-06-07 20:04:56 +00001188SysRes VG_(do_sys_sigaction) ( Int signo,
njncda2f0f2009-05-18 02:12:08 +00001189 const vki_sigaction_toK_t* new_act,
1190 vki_sigaction_fromK_t* old_act )
sewardj018f7622002-05-15 21:13:39 +00001191{
sewardj018f7622002-05-15 21:13:39 +00001192 if (VG_(clo_trace_signals))
njn18a71e82010-07-06 04:21:47 +00001193 VG_(dmsg)("sys_sigaction: sigNo %d, "
sewardj738856f2009-07-15 14:48:32 +00001194 "new %#lx, old %#lx, new flags 0x%llx\n",
1195 signo, (UWord)new_act, (UWord)old_act,
1196 (ULong)(new_act ? new_act->sa_flags : 0));
sewardj018f7622002-05-15 21:13:39 +00001197
1198 /* Rule out various error conditions. The aim is to ensure that if
1199 when the call is passed to the kernel it will definitely
1200 succeed. */
1201
1202 /* Reject out-of-range signal numbers. */
sewardjb5f6f512005-03-10 23:59:00 +00001203 if (signo < 1 || signo > VG_(max_signal)) goto bad_signo;
sewardj018f7622002-05-15 21:13:39 +00001204
jsgf855d93d2003-10-13 22:26:55 +00001205 /* don't let them use our signals */
njn351d0062005-06-21 22:23:59 +00001206 if ( (signo > VG_SIGVGRTUSERMAX)
jsgf855d93d2003-10-13 22:26:55 +00001207 && new_act
sewardja8d8e232005-06-07 20:04:56 +00001208 && !(new_act->ksa_handler == VKI_SIG_DFL
1209 || new_act->ksa_handler == VKI_SIG_IGN) )
nethercote9c42a0f2003-11-17 10:37:19 +00001210 goto bad_signo_reserved;
jsgf855d93d2003-10-13 22:26:55 +00001211
sewardj018f7622002-05-15 21:13:39 +00001212 /* Reject attempts to set a handler (or set ignore) for SIGKILL. */
1213 if ( (signo == VKI_SIGKILL || signo == VKI_SIGSTOP)
1214 && new_act
1215 && new_act->ksa_handler != VKI_SIG_DFL)
1216 goto bad_sigkill_or_sigstop;
1217
1218 /* If the client supplied non-NULL old_act, copy the relevant SCSS
1219 entry into it. */
1220 if (old_act) {
njn695c16e2005-03-27 03:40:28 +00001221 old_act->ksa_handler = scss.scss_per_sig[signo].scss_handler;
1222 old_act->sa_flags = scss.scss_per_sig[signo].scss_flags;
1223 old_act->sa_mask = scss.scss_per_sig[signo].scss_mask;
sewardj8eb8bab2015-07-21 14:44:28 +00001224# if !defined(VGP_x86_darwin) && !defined(VGP_amd64_darwin) && \
1225 !defined(VGO_solaris)
njn695c16e2005-03-27 03:40:28 +00001226 old_act->sa_restorer = scss.scss_per_sig[signo].scss_restorer;
sewardj489bfec2006-10-17 02:00:29 +00001227# endif
sewardj018f7622002-05-15 21:13:39 +00001228 }
1229
1230 /* And now copy new SCSS entry from new_act. */
1231 if (new_act) {
njn695c16e2005-03-27 03:40:28 +00001232 scss.scss_per_sig[signo].scss_handler = new_act->ksa_handler;
1233 scss.scss_per_sig[signo].scss_flags = new_act->sa_flags;
1234 scss.scss_per_sig[signo].scss_mask = new_act->sa_mask;
sewardj489bfec2006-10-17 02:00:29 +00001235
njncda2f0f2009-05-18 02:12:08 +00001236 scss.scss_per_sig[signo].scss_restorer = NULL;
sewardj8eb8bab2015-07-21 14:44:28 +00001237# if !defined(VGP_x86_darwin) && !defined(VGP_amd64_darwin) && \
1238 !defined(VGO_solaris)
njn695c16e2005-03-27 03:40:28 +00001239 scss.scss_per_sig[signo].scss_restorer = new_act->sa_restorer;
sewardj489bfec2006-10-17 02:00:29 +00001240# endif
sewardjb5f6f512005-03-10 23:59:00 +00001241
njncda2f0f2009-05-18 02:12:08 +00001242 scss.scss_per_sig[signo].scss_sa_tramp = NULL;
njnf76d27a2009-05-28 01:53:07 +00001243# if defined(VGP_x86_darwin) || defined(VGP_amd64_darwin)
1244 scss.scss_per_sig[signo].scss_sa_tramp = new_act->sa_tramp;
1245# endif
njncda2f0f2009-05-18 02:12:08 +00001246
njn695c16e2005-03-27 03:40:28 +00001247 VG_(sigdelset)(&scss.scss_per_sig[signo].scss_mask, VKI_SIGKILL);
1248 VG_(sigdelset)(&scss.scss_per_sig[signo].scss_mask, VKI_SIGSTOP);
sewardj018f7622002-05-15 21:13:39 +00001249 }
1250
1251 /* All happy bunnies ... */
1252 if (new_act) {
nethercote9dd11512004-08-04 15:31:30 +00001253 handle_SCSS_change( False /* lazy update */ );
sewardj018f7622002-05-15 21:13:39 +00001254 }
sewardja8d8e232005-06-07 20:04:56 +00001255 return VG_(mk_SysRes_Success)( 0 );
sewardj018f7622002-05-15 21:13:39 +00001256
1257 bad_signo:
sewardjec6e27c2007-11-17 21:31:48 +00001258 if (VG_(showing_core_errors)() && !VG_(clo_xml)) {
sewardj738856f2009-07-15 14:48:32 +00001259 VG_(umsg)("Warning: bad signal number %d in sigaction()\n", signo);
sewardj9a3d8bd2005-05-23 14:47:52 +00001260 }
sewardja8d8e232005-06-07 20:04:56 +00001261 return VG_(mk_SysRes_Error)( VKI_EINVAL );
sewardj018f7622002-05-15 21:13:39 +00001262
nethercote9c42a0f2003-11-17 10:37:19 +00001263 bad_signo_reserved:
sewardjec6e27c2007-11-17 21:31:48 +00001264 if (VG_(showing_core_errors)() && !VG_(clo_xml)) {
sewardj738856f2009-07-15 14:48:32 +00001265 VG_(umsg)("Warning: ignored attempt to set %s handler in sigaction();\n",
philippe886fde32012-03-29 21:56:47 +00001266 VG_(signame)(signo));
sewardj738856f2009-07-15 14:48:32 +00001267 VG_(umsg)(" the %s signal is used internally by Valgrind\n",
philippe886fde32012-03-29 21:56:47 +00001268 VG_(signame)(signo));
fitzhardingebf459872003-11-18 16:55:33 +00001269 }
sewardja8d8e232005-06-07 20:04:56 +00001270 return VG_(mk_SysRes_Error)( VKI_EINVAL );
nethercote9c42a0f2003-11-17 10:37:19 +00001271
sewardj018f7622002-05-15 21:13:39 +00001272 bad_sigkill_or_sigstop:
sewardjec6e27c2007-11-17 21:31:48 +00001273 if (VG_(showing_core_errors)() && !VG_(clo_xml)) {
sewardj738856f2009-07-15 14:48:32 +00001274 VG_(umsg)("Warning: ignored attempt to set %s handler in sigaction();\n",
philippe886fde32012-03-29 21:56:47 +00001275 VG_(signame)(signo));
sewardj738856f2009-07-15 14:48:32 +00001276 VG_(umsg)(" the %s signal is uncatchable\n",
philippe886fde32012-03-29 21:56:47 +00001277 VG_(signame)(signo));
sewardj9a3d8bd2005-05-23 14:47:52 +00001278 }
sewardja8d8e232005-06-07 20:04:56 +00001279 return VG_(mk_SysRes_Error)( VKI_EINVAL );
sewardj018f7622002-05-15 21:13:39 +00001280}
1281
1282
1283static
1284void do_sigprocmask_bitops ( Int vki_how,
nethercote73b526f2004-10-31 18:48:21 +00001285 vki_sigset_t* orig_set,
1286 vki_sigset_t* modifier )
sewardj018f7622002-05-15 21:13:39 +00001287{
1288 switch (vki_how) {
1289 case VKI_SIG_BLOCK:
nethercote73b526f2004-10-31 18:48:21 +00001290 VG_(sigaddset_from_set)( orig_set, modifier );
sewardj018f7622002-05-15 21:13:39 +00001291 break;
1292 case VKI_SIG_UNBLOCK:
nethercote73b526f2004-10-31 18:48:21 +00001293 VG_(sigdelset_from_set)( orig_set, modifier );
sewardj018f7622002-05-15 21:13:39 +00001294 break;
1295 case VKI_SIG_SETMASK:
1296 *orig_set = *modifier;
1297 break;
1298 default:
njne427a662002-10-02 11:08:25 +00001299 VG_(core_panic)("do_sigprocmask_bitops");
sewardj018f7622002-05-15 21:13:39 +00001300 break;
1301 }
1302}
1303
tomf7781e92005-11-02 15:31:21 +00001304static
njn5a350be2009-04-30 03:05:05 +00001305HChar* format_sigset ( const vki_sigset_t* set )
tomf7781e92005-11-02 15:31:21 +00001306{
florian7b7d5942014-12-19 20:29:22 +00001307 static HChar buf[_VKI_NSIG_WORDS * 16 + 1];
tomf7781e92005-11-02 15:31:21 +00001308 int w;
1309
1310 VG_(strcpy)(buf, "");
1311
1312 for (w = _VKI_NSIG_WORDS - 1; w >= 0; w--)
1313 {
sewardja8ffda62008-07-18 18:23:24 +00001314# if _VKI_NSIG_BPW == 32
1315 VG_(sprintf)(buf + VG_(strlen)(buf), "%08llx",
1316 set ? (ULong)set->sig[w] : 0);
1317# elif _VKI_NSIG_BPW == 64
1318 VG_(sprintf)(buf + VG_(strlen)(buf), "%16llx",
1319 set ? (ULong)set->sig[w] : 0);
1320# else
1321# error "Unsupported value for _VKI_NSIG_BPW"
1322# endif
tomf7781e92005-11-02 15:31:21 +00001323 }
1324
1325 return buf;
1326}
1327
jsgf855d93d2003-10-13 22:26:55 +00001328/*
1329 This updates the thread's signal mask. There's no such thing as a
1330 process-wide signal mask.
sewardj018f7622002-05-15 21:13:39 +00001331
1332 Note that the thread signal masks are an implicit part of SCSS,
1333 which is why this routine is allowed to mess with them.
1334*/
1335static
1336void do_setmask ( ThreadId tid,
1337 Int how,
nethercote73b526f2004-10-31 18:48:21 +00001338 vki_sigset_t* newset,
1339 vki_sigset_t* oldset )
sewardj018f7622002-05-15 21:13:39 +00001340{
sewardj018f7622002-05-15 21:13:39 +00001341 if (VG_(clo_trace_signals))
floriana5e06c32015-08-05 21:16:09 +00001342 VG_(dmsg)("do_setmask: tid = %u how = %d (%s), newset = %p (%s)\n",
sewardj738856f2009-07-15 14:48:32 +00001343 tid, how,
1344 how==VKI_SIG_BLOCK ? "SIG_BLOCK" : (
1345 how==VKI_SIG_UNBLOCK ? "SIG_UNBLOCK" : (
1346 how==VKI_SIG_SETMASK ? "SIG_SETMASK" : "???")),
1347 newset, newset ? format_sigset(newset) : "NULL" );
sewardj018f7622002-05-15 21:13:39 +00001348
jsgf855d93d2003-10-13 22:26:55 +00001349 /* Just do this thread. */
1350 vg_assert(VG_(is_valid_tid)(tid));
1351 if (oldset) {
sewardjb5f6f512005-03-10 23:59:00 +00001352 *oldset = VG_(threads)[tid].sig_mask;
jsgf855d93d2003-10-13 22:26:55 +00001353 if (VG_(clo_trace_signals))
njn18a71e82010-07-06 04:21:47 +00001354 VG_(dmsg)("\toldset=%p %s\n", oldset, format_sigset(oldset));
sewardj018f7622002-05-15 21:13:39 +00001355 }
sewardj018f7622002-05-15 21:13:39 +00001356 if (newset) {
jsgf855d93d2003-10-13 22:26:55 +00001357 do_sigprocmask_bitops (how, &VG_(threads)[tid].sig_mask, newset );
nethercote73b526f2004-10-31 18:48:21 +00001358 VG_(sigdelset)(&VG_(threads)[tid].sig_mask, VKI_SIGKILL);
1359 VG_(sigdelset)(&VG_(threads)[tid].sig_mask, VKI_SIGSTOP);
sewardjb5f6f512005-03-10 23:59:00 +00001360 VG_(threads)[tid].tmp_sig_mask = VG_(threads)[tid].sig_mask;
sewardj018f7622002-05-15 21:13:39 +00001361 }
1362}
1363
1364
sewardja8d8e232005-06-07 20:04:56 +00001365SysRes VG_(do_sys_sigprocmask) ( ThreadId tid,
1366 Int how,
1367 vki_sigset_t* set,
1368 vki_sigset_t* oldset )
sewardj018f7622002-05-15 21:13:39 +00001369{
jsgf855d93d2003-10-13 22:26:55 +00001370 switch(how) {
njncda2f0f2009-05-18 02:12:08 +00001371 case VKI_SIG_BLOCK:
1372 case VKI_SIG_UNBLOCK:
1373 case VKI_SIG_SETMASK:
1374 vg_assert(VG_(is_valid_tid)(tid));
1375 do_setmask ( tid, how, set, oldset );
1376 return VG_(mk_SysRes_Success)( 0 );
jsgf855d93d2003-10-13 22:26:55 +00001377
njncda2f0f2009-05-18 02:12:08 +00001378 default:
sewardj738856f2009-07-15 14:48:32 +00001379 VG_(dmsg)("sigprocmask: unknown 'how' field %d\n", how);
njncda2f0f2009-05-18 02:12:08 +00001380 return VG_(mk_SysRes_Error)( VKI_EINVAL );
sewardj018f7622002-05-15 21:13:39 +00001381 }
1382}
1383
1384
sewardj018f7622002-05-15 21:13:39 +00001385/* ---------------------------------------------------------------------
1386 LOW LEVEL STUFF TO DO WITH SIGNALS: IMPLEMENTATION
1387 ------------------------------------------------------------------ */
sewardj77e466c2002-04-14 02:29:29 +00001388
sewardj2e93c502002-04-12 11:12:52 +00001389/* ---------------------------------------------------------------------
1390 Handy utilities to block/restore all host signals.
1391 ------------------------------------------------------------------ */
1392
1393/* Block all host signals, dumping the old mask in *saved_mask. */
njn444eba12005-05-12 03:47:31 +00001394static void block_all_host_signals ( /* OUT */ vki_sigset_t* saved_mask )
sewardj2e93c502002-04-12 11:12:52 +00001395{
1396 Int ret;
nethercote73b526f2004-10-31 18:48:21 +00001397 vki_sigset_t block_procmask;
1398 VG_(sigfillset)(&block_procmask);
1399 ret = VG_(sigprocmask)
sewardj2e93c502002-04-12 11:12:52 +00001400 (VKI_SIG_SETMASK, &block_procmask, saved_mask);
1401 vg_assert(ret == 0);
1402}
1403
1404/* Restore the blocking mask using the supplied saved one. */
njn444eba12005-05-12 03:47:31 +00001405static void restore_all_host_signals ( /* IN */ vki_sigset_t* saved_mask )
sewardj2e93c502002-04-12 11:12:52 +00001406{
1407 Int ret;
nethercote73b526f2004-10-31 18:48:21 +00001408 ret = VG_(sigprocmask)(VKI_SIG_SETMASK, saved_mask, NULL);
sewardj2e93c502002-04-12 11:12:52 +00001409 vg_assert(ret == 0);
1410}
sewardjde4a1d02002-03-22 01:27:54 +00001411
njn444eba12005-05-12 03:47:31 +00001412void VG_(clear_out_queued_signals)( ThreadId tid, vki_sigset_t* saved_mask )
1413{
1414 block_all_host_signals(saved_mask);
1415 if (VG_(threads)[tid].sig_queue != NULL) {
florian77eb20b2014-09-11 21:19:17 +00001416 VG_(free)(VG_(threads)[tid].sig_queue);
njn444eba12005-05-12 03:47:31 +00001417 VG_(threads)[tid].sig_queue = NULL;
1418 }
1419 restore_all_host_signals(saved_mask);
1420}
1421
sewardjde4a1d02002-03-22 01:27:54 +00001422/* ---------------------------------------------------------------------
1423 The signal simulation proper. A simplified version of what the
1424 Linux kernel does.
1425 ------------------------------------------------------------------ */
1426
sewardjde4a1d02002-03-22 01:27:54 +00001427/* Set up a stack frame (VgSigContext) for the client's signal
nethercotefedd8102004-09-13 15:19:34 +00001428 handler. */
sewardj2c5ffbe2005-03-12 13:32:06 +00001429static
njn5a350be2009-04-30 03:05:05 +00001430void push_signal_frame ( ThreadId tid, const vki_siginfo_t *siginfo,
1431 const struct vki_ucontext *uc )
sewardjde4a1d02002-03-22 01:27:54 +00001432{
sewardj8eb8bab2015-07-21 14:44:28 +00001433 Bool on_altstack;
nethercote6eec4602004-09-13 14:15:36 +00001434 Addr esp_top_of_frame;
sewardj2e93c502002-04-12 11:12:52 +00001435 ThreadState* tst;
jsgf855d93d2003-10-13 22:26:55 +00001436 Int sigNo = siginfo->si_signo;
sewardj2e93c502002-04-12 11:12:52 +00001437
sewardjb5f6f512005-03-10 23:59:00 +00001438 vg_assert(sigNo >= 1 && sigNo <= VG_(max_signal));
sewardj018f7622002-05-15 21:13:39 +00001439 vg_assert(VG_(is_valid_tid)(tid));
1440 tst = & VG_(threads)[tid];
sewardj2e93c502002-04-12 11:12:52 +00001441
sewardja672ea32006-04-29 18:03:14 +00001442 if (VG_(clo_trace_signals)) {
floriana5e06c32015-08-05 21:16:09 +00001443 VG_(dmsg)("push_signal_frame (thread %u): signal %d\n", tid, sigNo);
sewardja672ea32006-04-29 18:03:14 +00001444 VG_(get_and_pp_StackTrace)(tid, 10);
1445 }
jsgf855d93d2003-10-13 22:26:55 +00001446
sewardj2342c972002-05-22 23:34:20 +00001447 if (/* this signal asked to run on an alt stack */
njn695c16e2005-03-27 03:40:28 +00001448 (scss.scss_per_sig[sigNo].scss_flags & VKI_SA_ONSTACK )
sewardj2342c972002-05-22 23:34:20 +00001449 && /* there is a defined and enabled alt stack, which we're not
1450 already using. Logic from get_sigframe in
1451 arch/i386/kernel/signal.c. */
njnf536bbb2005-06-13 04:21:38 +00001452 sas_ss_flags(tid, VG_(get_SP)(tid)) == 0
sewardj2342c972002-05-22 23:34:20 +00001453 ) {
sewardj8eb8bab2015-07-21 14:44:28 +00001454 on_altstack = True;
sewardj2342c972002-05-22 23:34:20 +00001455 esp_top_of_frame
fitzhardinge98c4dc02004-03-16 08:27:29 +00001456 = (Addr)(tst->altstack.ss_sp) + tst->altstack.ss_size;
sewardj2342c972002-05-22 23:34:20 +00001457 if (VG_(clo_trace_signals))
floriana5e06c32015-08-05 21:16:09 +00001458 VG_(dmsg)("delivering signal %d (%s) to thread %u: "
sewardj738856f2009-07-15 14:48:32 +00001459 "on ALT STACK (%p-%p; %ld bytes)\n",
philippe886fde32012-03-29 21:56:47 +00001460 sigNo, VG_(signame)(sigNo), tid, tst->altstack.ss_sp,
sewardj738856f2009-07-15 14:48:32 +00001461 (UChar *)tst->altstack.ss_sp + tst->altstack.ss_size,
1462 (Word)tst->altstack.ss_size );
sewardj2342c972002-05-22 23:34:20 +00001463 } else {
sewardj8eb8bab2015-07-21 14:44:28 +00001464 on_altstack = False;
njnaf839f52005-06-23 03:27:57 +00001465 esp_top_of_frame = VG_(get_SP)(tid) - VG_STACK_REDZONE_SZB;
sewardj2342c972002-05-22 23:34:20 +00001466 }
sewardjb5f6f512005-03-10 23:59:00 +00001467
sewardj8eb8bab2015-07-21 14:44:28 +00001468 /* Signal delivery to tools */
1469 VG_TRACK( pre_deliver_signal, tid, sigNo, on_altstack );
1470
njn695c16e2005-03-27 03:40:28 +00001471 vg_assert(scss.scss_per_sig[sigNo].scss_handler != VKI_SIG_IGN);
1472 vg_assert(scss.scss_per_sig[sigNo].scss_handler != VKI_SIG_DFL);
sewardjb5f6f512005-03-10 23:59:00 +00001473
1474 /* This may fail if the client stack is busted; if that happens,
1475 the whole process will exit rather than simply calling the
1476 signal handler. */
sewardj8eb8bab2015-07-21 14:44:28 +00001477 VG_(sigframe_create) (tid, on_altstack, esp_top_of_frame, siginfo, uc,
sewardj985fabb2005-04-24 14:18:14 +00001478 scss.scss_per_sig[sigNo].scss_handler,
1479 scss.scss_per_sig[sigNo].scss_flags,
1480 &tst->sig_mask,
1481 scss.scss_per_sig[sigNo].scss_restorer);
sewardjde4a1d02002-03-22 01:27:54 +00001482}
1483
sewardjde4a1d02002-03-22 01:27:54 +00001484
florian2b8059a2012-10-14 16:45:23 +00001485const HChar *VG_(signame)(Int sigNo)
sewardjde4a1d02002-03-22 01:27:54 +00001486{
florianf44ff622014-12-20 16:52:08 +00001487 static HChar buf[20]; // large enough
sewardjb48e5002002-05-13 00:16:03 +00001488
jsgf855d93d2003-10-13 22:26:55 +00001489 switch(sigNo) {
sewardj489bfec2006-10-17 02:00:29 +00001490 case VKI_SIGHUP: return "SIGHUP";
1491 case VKI_SIGINT: return "SIGINT";
1492 case VKI_SIGQUIT: return "SIGQUIT";
1493 case VKI_SIGILL: return "SIGILL";
1494 case VKI_SIGTRAP: return "SIGTRAP";
1495 case VKI_SIGABRT: return "SIGABRT";
1496 case VKI_SIGBUS: return "SIGBUS";
1497 case VKI_SIGFPE: return "SIGFPE";
1498 case VKI_SIGKILL: return "SIGKILL";
1499 case VKI_SIGUSR1: return "SIGUSR1";
1500 case VKI_SIGUSR2: return "SIGUSR2";
1501 case VKI_SIGSEGV: return "SIGSEGV";
sewardj8eb8bab2015-07-21 14:44:28 +00001502 case VKI_SIGSYS: return "SIGSYS";
sewardj489bfec2006-10-17 02:00:29 +00001503 case VKI_SIGPIPE: return "SIGPIPE";
1504 case VKI_SIGALRM: return "SIGALRM";
1505 case VKI_SIGTERM: return "SIGTERM";
1506# if defined(VKI_SIGSTKFLT)
1507 case VKI_SIGSTKFLT: return "SIGSTKFLT";
1508# endif
1509 case VKI_SIGCHLD: return "SIGCHLD";
1510 case VKI_SIGCONT: return "SIGCONT";
1511 case VKI_SIGSTOP: return "SIGSTOP";
1512 case VKI_SIGTSTP: return "SIGTSTP";
1513 case VKI_SIGTTIN: return "SIGTTIN";
1514 case VKI_SIGTTOU: return "SIGTTOU";
1515 case VKI_SIGURG: return "SIGURG";
1516 case VKI_SIGXCPU: return "SIGXCPU";
1517 case VKI_SIGXFSZ: return "SIGXFSZ";
1518 case VKI_SIGVTALRM: return "SIGVTALRM";
1519 case VKI_SIGPROF: return "SIGPROF";
1520 case VKI_SIGWINCH: return "SIGWINCH";
1521 case VKI_SIGIO: return "SIGIO";
njncda2f0f2009-05-18 02:12:08 +00001522# if defined(VKI_SIGPWR)
sewardj489bfec2006-10-17 02:00:29 +00001523 case VKI_SIGPWR: return "SIGPWR";
njncda2f0f2009-05-18 02:12:08 +00001524# endif
sewardj8eb8bab2015-07-21 14:44:28 +00001525# if defined(VKI_SIGUNUSED) && (VKI_SIGUNUSED != VKI_SIGSYS)
sewardj489bfec2006-10-17 02:00:29 +00001526 case VKI_SIGUNUSED: return "SIGUNUSED";
1527# endif
sewardjde4a1d02002-03-22 01:27:54 +00001528
sewardj8eb8bab2015-07-21 14:44:28 +00001529 /* Solaris-specific signals. */
1530# if defined(VKI_SIGEMT)
1531 case VKI_SIGEMT: return "SIGEMT";
1532# endif
1533# if defined(VKI_SIGWAITING)
1534 case VKI_SIGWAITING: return "SIGWAITING";
1535# endif
1536# if defined(VKI_SIGLWP)
1537 case VKI_SIGLWP: return "SIGLWP";
1538# endif
1539# if defined(VKI_SIGFREEZE)
1540 case VKI_SIGFREEZE: return "SIGFREEZE";
1541# endif
1542# if defined(VKI_SIGTHAW)
1543 case VKI_SIGTHAW: return "SIGTHAW";
1544# endif
1545# if defined(VKI_SIGCANCEL)
1546 case VKI_SIGCANCEL: return "SIGCANCEL";
1547# endif
1548# if defined(VKI_SIGLOST)
1549 case VKI_SIGLOST: return "SIGLOST";
1550# endif
1551# if defined(VKI_SIGXRES)
1552 case VKI_SIGXRES: return "SIGXRES";
1553# endif
1554# if defined(VKI_SIGJVM1)
1555 case VKI_SIGJVM1: return "SIGJVM1";
1556# endif
1557# if defined(VKI_SIGJVM2)
1558 case VKI_SIGJVM2: return "SIGJVM2";
1559# endif
1560
njncda2f0f2009-05-18 02:12:08 +00001561# if defined(VKI_SIGRTMIN) && defined(VKI_SIGRTMAX)
jsgf855d93d2003-10-13 22:26:55 +00001562 case VKI_SIGRTMIN ... VKI_SIGRTMAX:
sewardjb5f6f512005-03-10 23:59:00 +00001563 VG_(sprintf)(buf, "SIGRT%d", sigNo-VKI_SIGRTMIN);
jsgf855d93d2003-10-13 22:26:55 +00001564 return buf;
njncda2f0f2009-05-18 02:12:08 +00001565# endif
sewardjde4a1d02002-03-22 01:27:54 +00001566
jsgf855d93d2003-10-13 22:26:55 +00001567 default:
1568 VG_(sprintf)(buf, "SIG%d", sigNo);
1569 return buf;
1570 }
1571}
sewardjde4a1d02002-03-22 01:27:54 +00001572
jsgf855d93d2003-10-13 22:26:55 +00001573/* Hit ourselves with a signal using the default handler */
1574void VG_(kill_self)(Int sigNo)
1575{
njnf76d27a2009-05-28 01:53:07 +00001576 Int r;
njncda2f0f2009-05-18 02:12:08 +00001577 vki_sigset_t mask, origmask;
1578 vki_sigaction_toK_t sa, origsa2;
1579 vki_sigaction_fromK_t origsa;
sewardj018f7622002-05-15 21:13:39 +00001580
jsgf855d93d2003-10-13 22:26:55 +00001581 sa.ksa_handler = VKI_SIG_DFL;
nethercote73b526f2004-10-31 18:48:21 +00001582 sa.sa_flags = 0;
sewardj8eb8bab2015-07-21 14:44:28 +00001583# if !defined(VGP_x86_darwin) && !defined(VGP_amd64_darwin) && \
1584 !defined(VGO_solaris)
nethercote73b526f2004-10-31 18:48:21 +00001585 sa.sa_restorer = 0;
sewardj489bfec2006-10-17 02:00:29 +00001586# endif
nethercote73b526f2004-10-31 18:48:21 +00001587 VG_(sigemptyset)(&sa.sa_mask);
sewardj2e93c502002-04-12 11:12:52 +00001588
nethercote73b526f2004-10-31 18:48:21 +00001589 VG_(sigaction)(sigNo, &sa, &origsa);
sewardj018f7622002-05-15 21:13:39 +00001590
sewardjb5f6f512005-03-10 23:59:00 +00001591 VG_(sigemptyset)(&mask);
1592 VG_(sigaddset)(&mask, sigNo);
1593 VG_(sigprocmask)(VKI_SIG_UNBLOCK, &mask, &origmask);
jsgf855d93d2003-10-13 22:26:55 +00001594
njnf76d27a2009-05-28 01:53:07 +00001595 r = VG_(kill)(VG_(getpid)(), sigNo);
sewardj8eb8bab2015-07-21 14:44:28 +00001596# if !defined(VGO_darwin)
njnf76d27a2009-05-28 01:53:07 +00001597 /* This sometimes fails with EPERM on Darwin. I don't know why. */
sewardjc7ffc942011-03-28 16:26:42 +00001598 vg_assert(r == 0);
1599# endif
jsgf855d93d2003-10-13 22:26:55 +00001600
njncda2f0f2009-05-18 02:12:08 +00001601 VG_(convert_sigaction_fromK_to_toK)( &origsa, &origsa2 );
1602 VG_(sigaction)(sigNo, &origsa2, NULL);
nethercote73b526f2004-10-31 18:48:21 +00001603 VG_(sigprocmask)(VKI_SIG_SETMASK, &origmask, NULL);
jsgf855d93d2003-10-13 22:26:55 +00001604}
1605
njn36fce1b2009-04-28 01:55:01 +00001606// The si_code describes where the signal came from. Some come from the
1607// kernel, eg.: seg faults, illegal opcodes. Some come from the user, eg.:
1608// from kill() (SI_USER), or timer_settime() (SI_TIMER), or an async I/O
1609// request (SI_ASYNCIO). There's lots of implementation-defined leeway in
njnf76d27a2009-05-28 01:53:07 +00001610// POSIX, but the user vs. kernal distinction is what we want here. We also
1611// pass in some other details that can help when si_code is unreliable.
1612static Bool is_signal_from_kernel(ThreadId tid, int signum, int si_code)
njn36fce1b2009-04-28 01:55:01 +00001613{
sewardj8eb8bab2015-07-21 14:44:28 +00001614# if defined(VGO_linux) || defined(VGO_solaris)
njn36fce1b2009-04-28 01:55:01 +00001615 // On Linux, SI_USER is zero, negative values are from the user, positive
1616 // values are from the kernel. There are SI_FROMUSER and SI_FROMKERNEL
1617 // macros but we don't use them here because other platforms don't have
1618 // them.
1619 return ( si_code > VKI_SI_USER ? True : False );
sewardj6e9de462011-06-28 07:25:29 +00001620
1621# elif defined(VGO_darwin)
njnf76d27a2009-05-28 01:53:07 +00001622 // On Darwin 9.6.0, the si_code is completely unreliable. It should be the
1623 // case that 0 means "user", and >0 means "kernel". But:
1624 // - For SIGSEGV, it seems quite reliable.
1625 // - For SIGBUS, it's always 2.
1626 // - For SIGFPE, it's often 0, even for kernel ones (eg.
1627 // div-by-integer-zero always gives zero).
1628 // - For SIGILL, it's unclear.
1629 // - For SIGTRAP, it's always 1.
1630 // You can see the "NOTIMP" (not implemented) status of a number of the
1631 // sub-cases in sys/signal.h. Hopefully future versions of Darwin will
1632 // get this right.
1633
1634 // If we're blocked waiting on a syscall, it must be a user signal, because
1635 // the kernel won't generate sync signals within syscalls.
1636 if (VG_(threads)[tid].status == VgTs_WaitSys) {
1637 return False;
1638
1639 // If it's a SIGSEGV, use the proper condition, since it's fairly reliable.
1640 } else if (SIGSEGV == signum) {
1641 return ( si_code > 0 ? True : False );
1642
1643 // If it's anything else, assume it's kernel-generated. Reason being that
1644 // kernel-generated sync signals are more common, and it's probable that
1645 // misdiagnosing a user signal as a kernel signal is better than the
1646 // opposite.
1647 } else {
1648 return True;
1649 }
sewardj6e9de462011-06-28 07:25:29 +00001650# else
1651# error Unknown OS
1652# endif
njn36fce1b2009-04-28 01:55:01 +00001653}
1654
jsgf855d93d2003-10-13 22:26:55 +00001655/*
sewardjb5f6f512005-03-10 23:59:00 +00001656 Perform the default action of a signal. If the signal is fatal, it
Elliott Hughesa0664b92017-04-18 17:46:52 -07001657 terminates all other threads, but it doesn't actually kill
1658 the process and calling thread.
jsgf855d93d2003-10-13 22:26:55 +00001659
1660 If we're not being quiet, then print out some more detail about
1661 fatal signals (esp. core dumping signals).
1662 */
njn695c16e2005-03-27 03:40:28 +00001663static void default_action(const vki_siginfo_t *info, ThreadId tid)
jsgf855d93d2003-10-13 22:26:55 +00001664{
1665 Int sigNo = info->si_signo;
sewardjb5f6f512005-03-10 23:59:00 +00001666 Bool terminate = False; /* kills process */
1667 Bool core = False; /* kills process w/ core */
1668 struct vki_rlimit corelim;
1669 Bool could_core;
jsgf855d93d2003-10-13 22:26:55 +00001670
sewardjb5f6f512005-03-10 23:59:00 +00001671 vg_assert(VG_(is_running_thread)(tid));
1672
jsgf855d93d2003-10-13 22:26:55 +00001673 switch(sigNo) {
njncda2f0f2009-05-18 02:12:08 +00001674 case VKI_SIGQUIT: /* core */
1675 case VKI_SIGILL: /* core */
1676 case VKI_SIGABRT: /* core */
1677 case VKI_SIGFPE: /* core */
1678 case VKI_SIGSEGV: /* core */
1679 case VKI_SIGBUS: /* core */
1680 case VKI_SIGTRAP: /* core */
sewardj8eb8bab2015-07-21 14:44:28 +00001681 case VKI_SIGSYS: /* core */
njncda2f0f2009-05-18 02:12:08 +00001682 case VKI_SIGXCPU: /* core */
1683 case VKI_SIGXFSZ: /* core */
sewardj8eb8bab2015-07-21 14:44:28 +00001684
1685 /* Solaris-specific signals. */
1686# if defined(VKI_SIGEMT)
1687 case VKI_SIGEMT: /* core */
1688# endif
1689
njncda2f0f2009-05-18 02:12:08 +00001690 terminate = True;
1691 core = True;
1692 break;
jsgf855d93d2003-10-13 22:26:55 +00001693
njncda2f0f2009-05-18 02:12:08 +00001694 case VKI_SIGHUP: /* term */
1695 case VKI_SIGINT: /* term */
1696 case VKI_SIGKILL: /* term - we won't see this */
1697 case VKI_SIGPIPE: /* term */
1698 case VKI_SIGALRM: /* term */
1699 case VKI_SIGTERM: /* term */
1700 case VKI_SIGUSR1: /* term */
1701 case VKI_SIGUSR2: /* term */
1702 case VKI_SIGIO: /* term */
1703# if defined(VKI_SIGPWR)
1704 case VKI_SIGPWR: /* term */
1705# endif
njncda2f0f2009-05-18 02:12:08 +00001706 case VKI_SIGPROF: /* term */
1707 case VKI_SIGVTALRM: /* term */
1708# if defined(VKI_SIGRTMIN) && defined(VKI_SIGRTMAX)
1709 case VKI_SIGRTMIN ... VKI_SIGRTMAX: /* term */
1710# endif
sewardj8eb8bab2015-07-21 14:44:28 +00001711
1712 /* Solaris-specific signals. */
1713# if defined(VKI_SIGLOST)
1714 case VKI_SIGLOST: /* term */
1715# endif
1716
njncda2f0f2009-05-18 02:12:08 +00001717 terminate = True;
1718 break;
jsgf855d93d2003-10-13 22:26:55 +00001719 }
1720
1721 vg_assert(!core || (core && terminate));
1722
fitzhardinge98abfc72003-12-16 02:05:15 +00001723 if (VG_(clo_trace_signals))
sewardj738856f2009-07-15 14:48:32 +00001724 VG_(dmsg)("delivering %d (code %d) to default handler; action: %s%s\n",
1725 sigNo, info->si_code, terminate ? "terminate" : "ignore",
1726 core ? "+core" : "");
fitzhardinge98abfc72003-12-16 02:05:15 +00001727
sewardjb5f6f512005-03-10 23:59:00 +00001728 if (!terminate)
1729 return; /* nothing to do */
fitzhardinge4a4d1082004-03-15 23:46:54 +00001730
sewardjb5f6f512005-03-10 23:59:00 +00001731 could_core = core;
1732
1733 if (core) {
1734 /* If they set the core-size limit to zero, don't generate a
1735 core file */
fitzhardinge61a53412004-03-15 23:44:11 +00001736
sewardjb5f6f512005-03-10 23:59:00 +00001737 VG_(getrlimit)(VKI_RLIMIT_CORE, &corelim);
fitzhardinge61a53412004-03-15 23:44:11 +00001738
sewardjb5f6f512005-03-10 23:59:00 +00001739 if (corelim.rlim_cur == 0)
1740 core = False;
1741 }
fitzhardinge61a53412004-03-15 23:44:11 +00001742
Elliott Hughesa0664b92017-04-18 17:46:52 -07001743 if ( VG_(clo_verbosity) >= 1
1744 || (could_core && is_signal_from_kernel(tid, sigNo, info->si_code))
1745 || VG_(clo_xml) ) {
1746 if (VG_(clo_xml)) {
1747 VG_(printf_xml)("<fatal_signal>\n");
1748 VG_(printf_xml)(" <tid>%d</tid>\n", tid);
1749 ThreadState* tst = VG_(get_ThreadState)(tid);
1750 if (tst->thread_name) {
1751 VG_(printf_xml)(" <threadname>%s</threadname>\n",
1752 tst->thread_name);
1753 }
1754 VG_(printf_xml)(" <signo>%d</signo>\n", sigNo);
1755 VG_(printf_xml)(" <signame>%s</signame>\n", VG_(signame)(sigNo));
1756 VG_(printf_xml)(" <sicode>%d</sicode>\n", info->si_code);
1757 } else {
1758 VG_(umsg)(
1759 "\n"
1760 "Process terminating with default action of signal %d (%s)%s\n",
1761 sigNo, VG_(signame)(sigNo), core ? ": dumping core" : "");
1762 }
jsgf855d93d2003-10-13 22:26:55 +00001763
sewardjb5f6f512005-03-10 23:59:00 +00001764 /* Be helpful - decode some more details about this fault */
njnf76d27a2009-05-28 01:53:07 +00001765 if (is_signal_from_kernel(tid, sigNo, info->si_code)) {
florian2b8059a2012-10-14 16:45:23 +00001766 const HChar *event = NULL;
sewardjb5f6f512005-03-10 23:59:00 +00001767 Bool haveaddr = True;
jsgf855d93d2003-10-13 22:26:55 +00001768
sewardjb5f6f512005-03-10 23:59:00 +00001769 switch(sigNo) {
1770 case VKI_SIGSEGV:
1771 switch(info->si_code) {
sewardj738856f2009-07-15 14:48:32 +00001772 case VKI_SEGV_MAPERR: event = "Access not within mapped region";
1773 break;
1774 case VKI_SEGV_ACCERR: event = "Bad permissions for mapped region";
1775 break;
njn059539d2009-04-28 08:00:23 +00001776 case VKI_SEGV_MADE_UP_GPF:
sewardjb5f6f512005-03-10 23:59:00 +00001777 /* General Protection Fault: The CPU/kernel
1778 isn't telling us anything useful, but this
1779 is commonly the result of exceeding a
sewardj74b4cca2005-10-20 01:37:15 +00001780 segment limit. */
1781 event = "General Protection Fault";
sewardjb5f6f512005-03-10 23:59:00 +00001782 haveaddr = False;
jsgf855d93d2003-10-13 22:26:55 +00001783 break;
1784 }
sewardj45f4e7c2005-09-27 19:20:21 +00001785#if 0
1786 {
florian7b7d5942014-12-19 20:29:22 +00001787 HChar buf[50]; // large enough
sewardj45f4e7c2005-09-27 19:20:21 +00001788 VG_(am_show_nsegments)(0,"post segfault");
1789 VG_(sprintf)(buf, "/bin/cat /proc/%d/maps", VG_(getpid)());
1790 VG_(system)(buf);
1791 }
1792#endif
sewardjb5f6f512005-03-10 23:59:00 +00001793 break;
jsgf855d93d2003-10-13 22:26:55 +00001794
sewardjb5f6f512005-03-10 23:59:00 +00001795 case VKI_SIGILL:
1796 switch(info->si_code) {
tom148250b2005-11-12 00:13:20 +00001797 case VKI_ILL_ILLOPC: event = "Illegal opcode"; break;
1798 case VKI_ILL_ILLOPN: event = "Illegal operand"; break;
1799 case VKI_ILL_ILLADR: event = "Illegal addressing mode"; break;
1800 case VKI_ILL_ILLTRP: event = "Illegal trap"; break;
1801 case VKI_ILL_PRVOPC: event = "Privileged opcode"; break;
1802 case VKI_ILL_PRVREG: event = "Privileged register"; break;
1803 case VKI_ILL_COPROC: event = "Coprocessor error"; break;
1804 case VKI_ILL_BADSTK: event = "Internal stack error"; break;
sewardjb5f6f512005-03-10 23:59:00 +00001805 }
1806 break;
1807
1808 case VKI_SIGFPE:
1809 switch (info->si_code) {
tom148250b2005-11-12 00:13:20 +00001810 case VKI_FPE_INTDIV: event = "Integer divide by zero"; break;
1811 case VKI_FPE_INTOVF: event = "Integer overflow"; break;
1812 case VKI_FPE_FLTDIV: event = "FP divide by zero"; break;
1813 case VKI_FPE_FLTOVF: event = "FP overflow"; break;
1814 case VKI_FPE_FLTUND: event = "FP underflow"; break;
1815 case VKI_FPE_FLTRES: event = "FP inexact"; break;
1816 case VKI_FPE_FLTINV: event = "FP invalid operation"; break;
1817 case VKI_FPE_FLTSUB: event = "FP subscript out of range"; break;
sewardj8eb8bab2015-07-21 14:44:28 +00001818
1819 /* Solaris-specific codes. */
1820# if defined(VKI_FPE_FLTDEN)
1821 case VKI_FPE_FLTDEN: event = "FP denormalize"; break;
1822# endif
sewardjb5f6f512005-03-10 23:59:00 +00001823 }
1824 break;
1825
1826 case VKI_SIGBUS:
1827 switch (info->si_code) {
tom148250b2005-11-12 00:13:20 +00001828 case VKI_BUS_ADRALN: event = "Invalid address alignment"; break;
1829 case VKI_BUS_ADRERR: event = "Non-existent physical address"; break;
1830 case VKI_BUS_OBJERR: event = "Hardware error"; break;
sewardjb5f6f512005-03-10 23:59:00 +00001831 }
1832 break;
sewardj3059d272007-12-21 01:24:59 +00001833 } /* switch (sigNo) */
sewardjb5f6f512005-03-10 23:59:00 +00001834
Elliott Hughesa0664b92017-04-18 17:46:52 -07001835 if (VG_(clo_xml)) {
1836 if (event != NULL)
1837 VG_(printf_xml)(" <event>%s</event>\n", event);
1838 if (haveaddr)
1839 VG_(printf_xml)(" <siaddr>%p</siaddr>\n",
1840 info->VKI_SIGINFO_si_addr);
1841 } else {
1842 if (event != NULL) {
1843 if (haveaddr)
1844 VG_(umsg)(" %s at address %p\n",
1845 event, info->VKI_SIGINFO_si_addr);
1846 else
1847 VG_(umsg)(" %s\n", event);
1848 }
1849 }
jsgf855d93d2003-10-13 22:26:55 +00001850 }
sewardj3059d272007-12-21 01:24:59 +00001851 /* Print a stack trace. Be cautious if the thread's SP is in an
1852 obviously stupid place (not mapped readable) that would
1853 likely cause a segfault. */
1854 if (VG_(is_valid_tid)(tid)) {
florian2baf7532012-07-26 02:41:31 +00001855 Word first_ip_delta = 0;
sewardj8eb8bab2015-07-21 14:44:28 +00001856#if defined(VGO_linux) || defined(VGO_solaris)
florianede9caf2012-07-15 01:31:45 +00001857 /* Make sure that the address stored in the stack pointer is
1858 located in a mapped page. That is not necessarily so. E.g.
1859 consider the scenario where the stack pointer was decreased
1860 and now has a value that is just below the end of a page that has
1861 not been mapped yet. In that case VG_(am_is_valid_for_client)
1862 will consider the address of the stack pointer invalid and that
1863 would cause a back-trace of depth 1 to be printed, instead of a
1864 full back-trace. */
1865 if (tid == 1) { // main thread
1866 Addr esp = VG_(get_SP)(tid);
1867 Addr base = VG_PGROUNDDN(esp - VG_STACK_REDZONE_SZB);
Elliott Hughesa0664b92017-04-18 17:46:52 -07001868 if (VG_(am_addr_is_in_extensible_client_stack)(base)
1869 && VG_(extend_stack)(tid, base)) {
florianede9caf2012-07-15 01:31:45 +00001870 if (VG_(clo_trace_signals))
1871 VG_(dmsg)(" -> extended stack base to %#lx\n",
1872 VG_PGROUNDDN(esp));
1873 }
1874 }
1875#endif
florian2baf7532012-07-26 02:41:31 +00001876#if defined(VGA_s390x)
1877 if (sigNo == VKI_SIGILL) {
1878 /* The guest instruction address has been adjusted earlier to
1879 point to the insn following the one that could not be decoded.
1880 When printing the back-trace here we need to undo that
1881 adjustment so the first line in the back-trace reports the
1882 correct address. */
1883 Addr addr = (Addr)info->VKI_SIGINFO_si_addr;
1884 UChar byte = ((UChar *)addr)[0];
1885 Int insn_length = ((((byte >> 6) + 1) >> 1) + 1) << 1;
1886
1887 first_ip_delta = -insn_length;
1888 }
1889#endif
sewardj3059d272007-12-21 01:24:59 +00001890 ExeContext* ec = VG_(am_is_valid_for_client)
1891 (VG_(get_SP)(tid), sizeof(Addr), VKI_PROT_READ)
florian2baf7532012-07-26 02:41:31 +00001892 ? VG_(record_ExeContext)( tid, first_ip_delta )
florianbb4f5da2012-07-23 15:40:41 +00001893 : VG_(record_depth_1_ExeContext)( tid,
florian2baf7532012-07-26 02:41:31 +00001894 first_ip_delta );
sewardj3059d272007-12-21 01:24:59 +00001895 vg_assert(ec);
1896 VG_(pp_ExeContext)( ec );
fitzhardinge126c64f2003-12-08 21:58:37 +00001897 }
sewardj95d86c02007-12-18 01:49:23 +00001898 if (sigNo == VKI_SIGSEGV
florianfcd96352013-01-21 20:29:54 +00001899 && is_signal_from_kernel(tid, sigNo, info->si_code)
sewardj95d86c02007-12-18 01:49:23 +00001900 && info->si_code == VKI_SEGV_MAPERR) {
sewardj738856f2009-07-15 14:48:32 +00001901 VG_(umsg)(" If you believe this happened as a result of a stack\n" );
1902 VG_(umsg)(" overflow in your program's main thread (unlikely but\n");
1903 VG_(umsg)(" possible), you can try to increase the size of the\n" );
1904 VG_(umsg)(" main thread stack using the --main-stacksize= flag.\n" );
sewardj95d86c02007-12-18 01:49:23 +00001905 // FIXME: assumes main ThreadId == 1
1906 if (VG_(is_valid_tid)(1)) {
sewardj738856f2009-07-15 14:48:32 +00001907 VG_(umsg)(
philippedfa54a52013-03-13 21:44:07 +00001908 " The main thread stack size used in this run was %lu.\n",
1909 VG_(threads)[1].client_stack_szB);
sewardj95d86c02007-12-18 01:49:23 +00001910 }
1911 }
Elliott Hughesa0664b92017-04-18 17:46:52 -07001912 if (VG_(clo_xml)) {
1913 /* postamble */
1914 VG_(printf_xml)("</fatal_signal>\n");
1915 VG_(printf_xml)("\n");
1916 }
sewardjb5f6f512005-03-10 23:59:00 +00001917 }
1918
philippe2d1f2562014-09-19 08:57:29 +00001919 if (VG_(clo_vgdb) != Vg_VgdbNo
1920 && VG_(dyn_vgdb_error) <= VG_(get_n_errs_shown)() + 1) {
1921 /* Note: we add + 1 to n_errs_shown as the fatal signal was not
1922 reported through error msg, and so was not counted. */
philippeb3014692015-05-17 13:38:25 +00001923 VG_(gdbserver_report_fatal_signal) (info, tid);
philippe2d1f2562014-09-19 08:57:29 +00001924 }
1925
sewardjb5f6f512005-03-10 23:59:00 +00001926 if (core) {
florian90694062015-05-16 16:17:52 +00001927 static const struct vki_rlimit zero = { 0, 0 };
fitzhardinge4a4d1082004-03-15 23:46:54 +00001928
njn67229832005-08-28 04:38:12 +00001929 VG_(make_coredump)(tid, info, corelim.rlim_cur);
fitzhardinged65dcad2004-03-13 02:06:58 +00001930
sewardjb5f6f512005-03-10 23:59:00 +00001931 /* Make sure we don't get a confusing kernel-generated
1932 coredump when we finally exit */
1933 VG_(setrlimit)(VKI_RLIMIT_CORE, &zero);
1934 }
fitzhardinged65dcad2004-03-13 02:06:58 +00001935
sewardj1d887112005-05-30 21:44:08 +00001936 // what's this for?
1937 //VG_(threads)[VG_(master_tid)].os_state.fatalsig = sigNo;
sewardjde4a1d02002-03-22 01:27:54 +00001938
Elliott Hughesa0664b92017-04-18 17:46:52 -07001939 /* everyone but tid dies */
sewardjb5f6f512005-03-10 23:59:00 +00001940 VG_(nuke_all_threads_except)(tid, VgSrc_FatalSig);
Elliott Hughesa0664b92017-04-18 17:46:52 -07001941 VG_(reap_threads)(tid);
1942 /* stash fatal signal in this thread */
sewardjb5f6f512005-03-10 23:59:00 +00001943 VG_(threads)[tid].exitreason = VgSrc_FatalSig;
1944 VG_(threads)[tid].os_state.fatalsig = sigNo;
sewardjb48e5002002-05-13 00:16:03 +00001945}
1946
sewardjb5f6f512005-03-10 23:59:00 +00001947/*
1948 This does the business of delivering a signal to a thread. It may
1949 be called from either a real signal handler, or from normal code to
1950 cause the thread to enter the signal handler.
sewardj5e2f0012004-12-13 14:10:34 +00001951
sewardjb5f6f512005-03-10 23:59:00 +00001952 This updates the thread state, but it does not set it to be
1953 Runnable.
1954*/
njn5a350be2009-04-30 03:05:05 +00001955static void deliver_signal ( ThreadId tid, const vki_siginfo_t *info,
1956 const struct vki_ucontext *uc )
sewardjde4a1d02002-03-22 01:27:54 +00001957{
jsgf855d93d2003-10-13 22:26:55 +00001958 Int sigNo = info->si_signo;
njn695c16e2005-03-27 03:40:28 +00001959 SCSS_Per_Signal *handler = &scss.scss_per_sig[sigNo];
fitzhardinge98abfc72003-12-16 02:05:15 +00001960 void *handler_fn;
jsgf855d93d2003-10-13 22:26:55 +00001961 ThreadState *tst = VG_(get_ThreadState)(tid);
1962
1963 if (VG_(clo_trace_signals))
floriana5e06c32015-08-05 21:16:09 +00001964 VG_(dmsg)("delivering signal %d (%s):%d to thread %u\n",
philippe886fde32012-03-29 21:56:47 +00001965 sigNo, VG_(signame)(sigNo), info->si_code, tid );
jsgf855d93d2003-10-13 22:26:55 +00001966
njn351d0062005-06-21 22:23:59 +00001967 if (sigNo == VG_SIGVGKILL) {
sewardjb5f6f512005-03-10 23:59:00 +00001968 /* If this is a SIGVGKILL, we're expecting it to interrupt any
1969 blocked syscall. It doesn't matter whether the VCPU state is
1970 set to restart or not, because we don't expect it will
1971 execute any more client instructions. */
1972 vg_assert(VG_(is_exiting)(tid));
jsgf855d93d2003-10-13 22:26:55 +00001973 return;
1974 }
1975
sewardjb5f6f512005-03-10 23:59:00 +00001976 /* If the client specifies SIG_IGN, treat it as SIG_DFL.
jsgf855d93d2003-10-13 22:26:55 +00001977
njn9ec0f3e2005-03-13 06:00:47 +00001978 If deliver_signal() is being called on a thread, we want
sewardjb5f6f512005-03-10 23:59:00 +00001979 the signal to get through no matter what; if they're ignoring
1980 it, then we do this override (this is so we can send it SIGSEGV,
1981 etc). */
fitzhardinge98abfc72003-12-16 02:05:15 +00001982 handler_fn = handler->scss_handler;
sewardjb5f6f512005-03-10 23:59:00 +00001983 if (handler_fn == VKI_SIG_IGN)
fitzhardinge98abfc72003-12-16 02:05:15 +00001984 handler_fn = VKI_SIG_DFL;
1985
1986 vg_assert(handler_fn != VKI_SIG_IGN);
jsgf855d93d2003-10-13 22:26:55 +00001987
fitzhardinge98abfc72003-12-16 02:05:15 +00001988 if (handler_fn == VKI_SIG_DFL) {
njn695c16e2005-03-27 03:40:28 +00001989 default_action(info, tid);
jsgf855d93d2003-10-13 22:26:55 +00001990 } else {
1991 /* Create a signal delivery frame, and set the client's %ESP and
1992 %EIP so that when execution continues, we will enter the
1993 signal handler with the frame on top of the client's stack,
sewardjb5f6f512005-03-10 23:59:00 +00001994 as it expects.
1995
1996 Signal delivery can fail if the client stack is too small or
1997 missing, and we can't push the frame. If that happens,
1998 push_signal_frame will cause the whole process to exit when
1999 we next hit the scheduler.
2000 */
jsgf855d93d2003-10-13 22:26:55 +00002001 vg_assert(VG_(is_valid_tid)(tid));
sewardjb5f6f512005-03-10 23:59:00 +00002002
tomadacaf92007-12-21 10:24:24 +00002003 push_signal_frame ( tid, info, uc );
jsgf855d93d2003-10-13 22:26:55 +00002004
2005 if (handler->scss_flags & VKI_SA_ONESHOT) {
2006 /* Do the ONESHOT thing. */
2007 handler->scss_handler = VKI_SIG_DFL;
2008
nethercote9dd11512004-08-04 15:31:30 +00002009 handle_SCSS_change( False /* lazy update */ );
jsgf855d93d2003-10-13 22:26:55 +00002010 }
sewardjb5f6f512005-03-10 23:59:00 +00002011
2012 /* At this point:
2013 tst->sig_mask is the current signal mask
2014 tst->tmp_sig_mask is the same as sig_mask, unless we're in sigsuspend
2015 handler->scss_mask is the mask set by the handler
2016
2017 Handler gets a mask of tmp_sig_mask|handler_mask|signo
2018 */
2019 tst->sig_mask = tst->tmp_sig_mask;
2020 if (!(handler->scss_flags & VKI_SA_NOMASK)) {
2021 VG_(sigaddset_from_set)(&tst->sig_mask, &handler->scss_mask);
2022 VG_(sigaddset)(&tst->sig_mask, sigNo);
sewardjb5f6f512005-03-10 23:59:00 +00002023 tst->tmp_sig_mask = tst->sig_mask;
2024 }
2025 }
2026
2027 /* Thread state is ready to go - just add Runnable */
2028}
2029
njn06244e72005-06-21 22:27:19 +00002030static void resume_scheduler(ThreadId tid)
2031{
2032 ThreadState *tst = VG_(get_ThreadState)(tid);
2033
2034 vg_assert(tst->os_state.lwpid == VG_(gettid)());
2035
2036 if (tst->sched_jmpbuf_valid) {
2037 /* Can't continue; must longjmp back to the scheduler and thus
2038 enter the sighandler immediately. */
sewardj6c591e12011-04-11 16:17:51 +00002039 VG_MINIMAL_LONGJMP(tst->sched_jmpbuf);
njn06244e72005-06-21 22:27:19 +00002040 }
2041}
2042
njn9ec0f3e2005-03-13 06:00:47 +00002043static void synth_fault_common(ThreadId tid, Addr addr, Int si_code)
2044{
2045 vki_siginfo_t info;
2046
2047 vg_assert(VG_(threads)[tid].status == VgTs_Runnable);
2048
njn5a350be2009-04-30 03:05:05 +00002049 VG_(memset)(&info, 0, sizeof(info));
njn9ec0f3e2005-03-13 06:00:47 +00002050 info.si_signo = VKI_SIGSEGV;
2051 info.si_code = si_code;
sewardj489bfec2006-10-17 02:00:29 +00002052 info.VKI_SIGINFO_si_addr = (void*)addr;
njn9ec0f3e2005-03-13 06:00:47 +00002053
philippe448e2bf2013-03-03 17:52:31 +00002054 /* Even if gdbserver indicates to ignore the signal, we must deliver it.
2055 So ignore the return value of VG_(gdbserver_report_signal). */
philippeb3014692015-05-17 13:38:25 +00002056 (void) VG_(gdbserver_report_signal) (&info, tid);
sewardj3b290482011-05-06 21:02:55 +00002057
njn9ec0f3e2005-03-13 06:00:47 +00002058 /* If they're trying to block the signal, force it to be delivered */
2059 if (VG_(sigismember)(&VG_(threads)[tid].sig_mask, VKI_SIGSEGV))
2060 VG_(set_default_handler)(VKI_SIGSEGV);
2061
tomadacaf92007-12-21 10:24:24 +00002062 deliver_signal(tid, &info, NULL);
njn9ec0f3e2005-03-13 06:00:47 +00002063}
2064
2065// Synthesize a fault where the address is OK, but the page
2066// permissions are bad.
2067void VG_(synth_fault_perms)(ThreadId tid, Addr addr)
2068{
njn059539d2009-04-28 08:00:23 +00002069 synth_fault_common(tid, addr, VKI_SEGV_ACCERR);
njn9ec0f3e2005-03-13 06:00:47 +00002070}
2071
2072// Synthesize a fault where the address there's nothing mapped at the address.
2073void VG_(synth_fault_mapping)(ThreadId tid, Addr addr)
2074{
njn059539d2009-04-28 08:00:23 +00002075 synth_fault_common(tid, addr, VKI_SEGV_MAPERR);
njn9ec0f3e2005-03-13 06:00:47 +00002076}
2077
2078// Synthesize a misc memory fault.
2079void VG_(synth_fault)(ThreadId tid)
2080{
njn059539d2009-04-28 08:00:23 +00002081 synth_fault_common(tid, 0, VKI_SEGV_MADE_UP_GPF);
njn9ec0f3e2005-03-13 06:00:47 +00002082}
2083
2084// Synthesise a SIGILL.
2085void VG_(synth_sigill)(ThreadId tid, Addr addr)
2086{
2087 vki_siginfo_t info;
2088
2089 vg_assert(VG_(threads)[tid].status == VgTs_Runnable);
2090
njn5a350be2009-04-30 03:05:05 +00002091 VG_(memset)(&info, 0, sizeof(info));
njn9ec0f3e2005-03-13 06:00:47 +00002092 info.si_signo = VKI_SIGILL;
sewardj489bfec2006-10-17 02:00:29 +00002093 info.si_code = VKI_ILL_ILLOPC; /* jrs: no idea what this should be */
2094 info.VKI_SIGINFO_si_addr = (void*)addr;
njn9ec0f3e2005-03-13 06:00:47 +00002095
philippeb3014692015-05-17 13:38:25 +00002096 if (VG_(gdbserver_report_signal) (&info, tid)) {
sewardj3b290482011-05-06 21:02:55 +00002097 resume_scheduler(tid);
2098 deliver_signal(tid, &info, NULL);
2099 }
2100 else
2101 resume_scheduler(tid);
njn9ec0f3e2005-03-13 06:00:47 +00002102}
2103
sewardj1c0ce7a2009-07-01 08:10:49 +00002104// Synthesise a SIGBUS.
2105void VG_(synth_sigbus)(ThreadId tid)
2106{
2107 vki_siginfo_t info;
2108
2109 vg_assert(VG_(threads)[tid].status == VgTs_Runnable);
2110
2111 VG_(memset)(&info, 0, sizeof(info));
2112 info.si_signo = VKI_SIGBUS;
2113 /* There are several meanings to SIGBUS (as per POSIX, presumably),
2114 but the most widely understood is "invalid address alignment",
2115 so let's use that. */
2116 info.si_code = VKI_BUS_ADRALN;
2117 /* If we knew the invalid address in question, we could put it
2118 in .si_addr. Oh well. */
2119 /* info.VKI_SIGINFO_si_addr = (void*)addr; */
2120
philippeb3014692015-05-17 13:38:25 +00002121 if (VG_(gdbserver_report_signal) (&info, tid)) {
sewardj3b290482011-05-06 21:02:55 +00002122 resume_scheduler(tid);
2123 deliver_signal(tid, &info, NULL);
2124 }
2125 else
2126 resume_scheduler(tid);
sewardj1c0ce7a2009-07-01 08:10:49 +00002127}
2128
sewardj86df1552006-02-07 20:56:41 +00002129// Synthesise a SIGTRAP.
2130void VG_(synth_sigtrap)(ThreadId tid)
2131{
2132 vki_siginfo_t info;
tomadacaf92007-12-21 10:24:24 +00002133 struct vki_ucontext uc;
njnf76d27a2009-05-28 01:53:07 +00002134# if defined(VGP_x86_darwin)
2135 struct __darwin_mcontext32 mc;
2136# elif defined(VGP_amd64_darwin)
2137 struct __darwin_mcontext64 mc;
2138# endif
sewardj86df1552006-02-07 20:56:41 +00002139
2140 vg_assert(VG_(threads)[tid].status == VgTs_Runnable);
2141
njn5a350be2009-04-30 03:05:05 +00002142 VG_(memset)(&info, 0, sizeof(info));
2143 VG_(memset)(&uc, 0, sizeof(uc));
sewardj86df1552006-02-07 20:56:41 +00002144 info.si_signo = VKI_SIGTRAP;
tomadacaf92007-12-21 10:24:24 +00002145 info.si_code = VKI_TRAP_BRKPT; /* tjh: only ever called for a brkpt ins */
njncda2f0f2009-05-18 02:12:08 +00002146
2147# if defined(VGP_x86_linux) || defined(VGP_amd64_linux)
tomadacaf92007-12-21 10:24:24 +00002148 uc.uc_mcontext.trapno = 3; /* tjh: this is the x86 trap number
2149 for a breakpoint trap... */
tom8b243022008-06-13 08:37:49 +00002150 uc.uc_mcontext.err = 0; /* tjh: no error code for x86
2151 breakpoint trap... */
njnf76d27a2009-05-28 01:53:07 +00002152# elif defined(VGP_x86_darwin) || defined(VGP_amd64_darwin)
2153 /* the same thing, but using Darwin field/struct names */
2154 VG_(memset)(&mc, 0, sizeof(mc));
2155 uc.uc_mcontext = &mc;
2156 uc.uc_mcontext->__es.__trapno = 3;
2157 uc.uc_mcontext->__es.__err = 0;
sewardj8eb8bab2015-07-21 14:44:28 +00002158# elif defined(VGP_x86_solaris)
2159 uc.uc_mcontext.gregs[VKI_ERR] = 0;
2160 uc.uc_mcontext.gregs[VKI_TRAPNO] = VKI_T_BPTFLT;
njncda2f0f2009-05-18 02:12:08 +00002161# endif
sewardj86df1552006-02-07 20:56:41 +00002162
sewardjb5b87402011-03-07 16:05:35 +00002163 /* fixs390: do we need to do anything here for s390 ? */
philippeb3014692015-05-17 13:38:25 +00002164 if (VG_(gdbserver_report_signal) (&info, tid)) {
sewardj3b290482011-05-06 21:02:55 +00002165 resume_scheduler(tid);
2166 deliver_signal(tid, &info, &uc);
2167 }
2168 else
2169 resume_scheduler(tid);
sewardj86df1552006-02-07 20:56:41 +00002170}
2171
petarj80e5c172012-10-19 14:45:17 +00002172// Synthesise a SIGFPE.
2173void VG_(synth_sigfpe)(ThreadId tid, UInt code)
2174{
petarj4df0bfc2013-02-27 23:17:33 +00002175// Only tested on mips32 and mips64
2176#if !defined(VGA_mips32) && !defined(VGA_mips64)
petarj80e5c172012-10-19 14:45:17 +00002177 vg_assert(0);
2178#else
2179 vki_siginfo_t info;
2180 struct vki_ucontext uc;
2181
2182 vg_assert(VG_(threads)[tid].status == VgTs_Runnable);
2183
2184 VG_(memset)(&info, 0, sizeof(info));
2185 VG_(memset)(&uc, 0, sizeof(uc));
2186 info.si_signo = VKI_SIGFPE;
2187 info.si_code = code;
2188
2189 if (VG_(gdbserver_report_signal) (VKI_SIGFPE, tid)) {
2190 resume_scheduler(tid);
2191 deliver_signal(tid, &info, &uc);
2192 }
2193 else
2194 resume_scheduler(tid);
2195#endif
2196}
2197
sewardjb5f6f512005-03-10 23:59:00 +00002198/* Make a signal pending for a thread, for later delivery.
2199 VG_(poll_signals) will arrange for it to be delivered at the right
2200 time.
2201
2202 tid==0 means add it to the process-wide queue, and not sent it to a
2203 specific thread.
2204*/
sewardj2c5ffbe2005-03-12 13:32:06 +00002205static
sewardjb5f6f512005-03-10 23:59:00 +00002206void queue_signal(ThreadId tid, const vki_siginfo_t *si)
2207{
2208 ThreadState *tst;
2209 SigQueue *sq;
2210 vki_sigset_t savedmask;
2211
2212 tst = VG_(get_ThreadState)(tid);
2213
2214 /* Protect the signal queue against async deliveries */
njn444eba12005-05-12 03:47:31 +00002215 block_all_host_signals(&savedmask);
sewardjb5f6f512005-03-10 23:59:00 +00002216
2217 if (tst->sig_queue == NULL) {
florian77eb20b2014-09-11 21:19:17 +00002218 tst->sig_queue = VG_(malloc)("signals.qs.1", sizeof(*tst->sig_queue));
sewardjb5f6f512005-03-10 23:59:00 +00002219 VG_(memset)(tst->sig_queue, 0, sizeof(*tst->sig_queue));
2220 }
2221 sq = tst->sig_queue;
2222
2223 if (VG_(clo_trace_signals))
floriana5e06c32015-08-05 21:16:09 +00002224 VG_(dmsg)("Queueing signal %d (idx %d) to thread %u\n",
sewardj738856f2009-07-15 14:48:32 +00002225 si->si_signo, sq->next, tid);
sewardjb5f6f512005-03-10 23:59:00 +00002226
2227 /* Add signal to the queue. If the queue gets overrun, then old
2228 queued signals may get lost.
2229
2230 XXX We should also keep a sigset of pending signals, so that at
2231 least a non-siginfo signal gets deliviered.
2232 */
2233 if (sq->sigs[sq->next].si_signo != 0)
floriana5e06c32015-08-05 21:16:09 +00002234 VG_(umsg)("Signal %d being dropped from thread %u's queue\n",
sewardj738856f2009-07-15 14:48:32 +00002235 sq->sigs[sq->next].si_signo, tid);
sewardjb5f6f512005-03-10 23:59:00 +00002236
2237 sq->sigs[sq->next] = *si;
2238 sq->next = (sq->next+1) % N_QUEUED_SIGNALS;
2239
njn444eba12005-05-12 03:47:31 +00002240 restore_all_host_signals(&savedmask);
sewardjb5f6f512005-03-10 23:59:00 +00002241}
2242
2243/*
2244 Returns the next queued signal for thread tid which is in "set".
2245 tid==0 means process-wide signal. Set si_signo to 0 when the
2246 signal has been delivered.
2247
2248 Must be called with all signals blocked, to protect against async
2249 deliveries.
2250*/
2251static vki_siginfo_t *next_queued(ThreadId tid, const vki_sigset_t *set)
2252{
2253 ThreadState *tst = VG_(get_ThreadState)(tid);
2254 SigQueue *sq;
2255 Int idx;
2256 vki_siginfo_t *ret = NULL;
2257
2258 sq = tst->sig_queue;
2259 if (sq == NULL)
2260 goto out;
jsgf855d93d2003-10-13 22:26:55 +00002261
sewardjb5f6f512005-03-10 23:59:00 +00002262 idx = sq->next;
2263 do {
2264 if (0)
2265 VG_(printf)("idx=%d si_signo=%d inset=%d\n", idx,
sewardj738856f2009-07-15 14:48:32 +00002266 sq->sigs[idx].si_signo,
2267 VG_(sigismember)(set, sq->sigs[idx].si_signo));
jsgf855d93d2003-10-13 22:26:55 +00002268
sewardj738856f2009-07-15 14:48:32 +00002269 if (sq->sigs[idx].si_signo != 0
2270 && VG_(sigismember)(set, sq->sigs[idx].si_signo)) {
sewardjb5f6f512005-03-10 23:59:00 +00002271 if (VG_(clo_trace_signals))
floriana5e06c32015-08-05 21:16:09 +00002272 VG_(dmsg)("Returning queued signal %d (idx %d) for thread %u\n",
sewardj738856f2009-07-15 14:48:32 +00002273 sq->sigs[idx].si_signo, idx, tid);
sewardjb5f6f512005-03-10 23:59:00 +00002274 ret = &sq->sigs[idx];
2275 goto out;
jsgf855d93d2003-10-13 22:26:55 +00002276 }
2277
sewardjb5f6f512005-03-10 23:59:00 +00002278 idx = (idx + 1) % N_QUEUED_SIGNALS;
2279 } while(idx != sq->next);
2280 out:
sewardj489bfec2006-10-17 02:00:29 +00002281 return ret;
jsgf855d93d2003-10-13 22:26:55 +00002282}
2283
njn3b6b2aa2009-04-30 03:30:09 +00002284static int sanitize_si_code(int si_code)
2285{
2286#if defined(VGO_linux)
2287 /* The linux kernel uses the top 16 bits of si_code for it's own
2288 use and only exports the bottom 16 bits to user space - at least
2289 that is the theory, but it turns out that there are some kernels
2290 around that forget to mask out the top 16 bits so we do it here.
2291
2292 The kernel treats the bottom 16 bits as signed and (when it does
2293 mask them off) sign extends them when exporting to user space so
2294 we do the same thing here. */
2295 return (Short)si_code;
sewardj8eb8bab2015-07-21 14:44:28 +00002296#elif defined(VGO_darwin) || defined(VGO_solaris)
njn3b6b2aa2009-04-30 03:30:09 +00002297 return si_code;
njn52040a42009-04-30 04:00:13 +00002298#else
2299# error Unknown OS
njn3b6b2aa2009-04-30 03:30:09 +00002300#endif
2301}
2302
sewardj8eb8bab2015-07-21 14:44:28 +00002303#if defined(VGO_solaris)
2304/* Following function is used to switch Valgrind from a client stack back onto
2305 a Valgrind stack. It is used only when the door_return call was invoked by
2306 the client because this is the only syscall which is executed directly on
2307 the client stack (see syscall-{x86,amd64}-solaris.S). The switch onto the
2308 Valgrind stack has to be made as soon as possible because there is no
2309 guarantee that there is enough space on the client stack to run the
2310 complete signal machinery. Also, Valgrind has to be switched back onto its
2311 stack before a simulated signal frame is created because that will
2312 overwrite the real sigframe built by the kernel. */
2313static void async_signalhandler_solaris_preprocess(ThreadId tid, Int *signo,
2314 vki_siginfo_t *info,
2315 struct vki_ucontext *uc)
2316{
2317# define RECURSION_BIT 0x1000
2318 Addr sp;
2319 vki_sigframe_t *frame;
2320 ThreadState *tst = VG_(get_ThreadState)(tid);
2321 Int rec_signo;
2322
2323 /* If not doing door_return then return instantly. */
2324 if (!tst->os_state.in_door_return)
2325 return;
2326
2327 /* Check for the recursion:
2328 v ...
2329 | async_signalhandler - executed on the client stack
2330 v async_signalhandler_solaris_preprocess - first call switches the
2331 | stacks and sets the RECURSION_BIT flag
2332 v async_signalhandler - executed on the Valgrind stack
2333 | async_signalhandler_solaris_preprocess - the RECURSION_BIT flag is
2334 v set, clear it and return
2335 */
2336 if (*signo & RECURSION_BIT) {
2337 *signo &= ~RECURSION_BIT;
2338 return;
2339 }
2340
2341 rec_signo = *signo | RECURSION_BIT;
2342
2343# if defined(VGP_x86_solaris)
2344 /* Register %ebx/%rbx points to the top of the original V stack. */
2345 sp = uc->uc_mcontext.gregs[VKI_EBX];
2346# elif defined(VGP_amd64_solaris)
2347 sp = uc->uc_mcontext.gregs[VKI_REG_RBX];
2348# else
2349# error "Unknown platform"
2350# endif
2351
2352 /* Build a fake signal frame, similarly as in sigframe-solaris.c. */
2353 /* Calculate a new stack pointer. */
2354 sp -= sizeof(vki_sigframe_t);
2355 sp = VG_ROUNDDN(sp, 16) - sizeof(UWord);
2356
2357 /* Fill in the frame. */
2358 frame = (vki_sigframe_t*)sp;
2359 /* Set a bogus return address. */
2360 frame->return_addr = (void*)~0UL;
2361 frame->a1_signo = rec_signo;
2362 /* The first parameter has to be 16-byte aligned, resembling a function
2363 call. */
2364 {
2365 /* Using
2366 vg_assert(VG_IS_16_ALIGNED(&frame->a1_signo));
2367 seems to get miscompiled on amd64 with GCC 4.7.2. */
2368 Addr signo_addr = (Addr)&frame->a1_signo;
2369 vg_assert(VG_IS_16_ALIGNED(signo_addr));
2370 }
2371 frame->a2_siginfo = &frame->siginfo;
2372 frame->siginfo = *info;
2373 frame->ucontext = *uc;
2374
2375# if defined(VGP_x86_solaris)
2376 frame->a3_ucontext = &frame->ucontext;
2377
2378 /* Switch onto the V stack and restart the signal processing. */
2379 __asm__ __volatile__(
2380 "xorl %%ebp, %%ebp\n"
2381 "movl %[sp], %%esp\n"
2382 "jmp async_signalhandler\n"
2383 :
2384 : [sp] "a" (sp)
2385 : /*"ebp"*/);
2386
2387# elif defined(VGP_amd64_solaris)
2388 __asm__ __volatile__(
2389 "xorq %%rbp, %%rbp\n"
2390 "movq %[sp], %%rsp\n"
2391 "jmp async_signalhandler\n"
2392 :
2393 : [sp] "a" (sp), "D" (rec_signo), "S" (&frame->siginfo),
2394 "d" (&frame->ucontext)
2395 : /*"rbp"*/);
2396# else
2397# error "Unknown platform"
2398# endif
2399
2400 /* We should never get here. */
2401 vg_assert(0);
2402
2403# undef RECURSION_BIT
2404}
2405#endif
2406
jsgf855d93d2003-10-13 22:26:55 +00002407/*
sewardjb5f6f512005-03-10 23:59:00 +00002408 Receive an async signal from the kernel.
jsgf855d93d2003-10-13 22:26:55 +00002409
sewardjb5f6f512005-03-10 23:59:00 +00002410 This should only happen when the thread is blocked in a syscall,
2411 since that's the only time this set of signals is unblocked.
jsgf855d93d2003-10-13 22:26:55 +00002412*/
2413static
njn5a350be2009-04-30 03:05:05 +00002414void async_signalhandler ( Int sigNo,
2415 vki_siginfo_t *info, struct vki_ucontext *uc )
jsgf855d93d2003-10-13 22:26:55 +00002416{
njn5a350be2009-04-30 03:05:05 +00002417 ThreadId tid = VG_(lwpid_to_vgtid)(VG_(gettid)());
2418 ThreadState* tst = VG_(get_ThreadState)(tid);
njncda2f0f2009-05-18 02:12:08 +00002419 SysRes sres;
jsgf855d93d2003-10-13 22:26:55 +00002420
njn5a350be2009-04-30 03:05:05 +00002421 vg_assert(tst->status == VgTs_WaitSys);
sewardj8eb8bab2015-07-21 14:44:28 +00002422
2423# if defined(VGO_solaris)
2424 async_signalhandler_solaris_preprocess(tid, &sigNo, info, uc);
2425# endif
2426
2427 /* The thread isn't currently running, make it so before going on */
njn5a350be2009-04-30 03:05:05 +00002428 VG_(acquire_BigLock)(tid, "async_signalhandler");
2429
njn3b6b2aa2009-04-30 03:30:09 +00002430 info->si_code = sanitize_si_code(info->si_code);
tom9f4a7bf2005-11-13 00:01:20 +00002431
sewardjb5f6f512005-03-10 23:59:00 +00002432 if (VG_(clo_trace_signals))
floriana5e06c32015-08-05 21:16:09 +00002433 VG_(dmsg)("async signal handler: signal=%d, tid=%u, si_code=%d\n",
sewardj738856f2009-07-15 14:48:32 +00002434 sigNo, tid, info->si_code);
sewardjb5f6f512005-03-10 23:59:00 +00002435
njncda2f0f2009-05-18 02:12:08 +00002436 /* Update thread state properly. The signal can only have been
2437 delivered whilst we were in
2438 coregrind/m_syswrap/syscall-<PLAT>.S, and only then in the
2439 window between the two sigprocmask calls, since at all other
2440 times, we run with async signals on the host blocked. Hence
2441 make enquiries on the basis that we were in or very close to a
2442 syscall, and attempt to fix up the guest state accordingly.
2443
2444 (normal async signals occurring during computation are blocked,
2445 but periodically polled for using VG_(sigtimedwait_zero), and
2446 delivered at a point convenient for us. Hence this routine only
2447 deals with signals that are delivered to a thread during a
2448 syscall.) */
2449
2450 /* First, extract a SysRes from the ucontext_t* given to this
2451 handler. If it is subsequently established by
2452 VG_(fixup_guest_state_after_syscall_interrupted) that the
2453 syscall was complete but the results had not been committed yet
2454 to the guest state, then it'll have to commit the results itself
2455 "by hand", and so we need to extract the SysRes. Of course if
2456 the thread was not in that particular window then the
2457 SysRes will be meaningless, but that's OK too because
2458 VG_(fixup_guest_state_after_syscall_interrupted) will detect
2459 that the thread was not in said window and ignore the SysRes. */
2460
njnf76d27a2009-05-28 01:53:07 +00002461 /* To make matters more complex still, on Darwin we need to know
2462 the "class" of the syscall under consideration in order to be
2463 able to extract the a correct SysRes. The class will have been
2464 saved just before the syscall, by VG_(client_syscall), into this
2465 thread's tst->arch.vex.guest_SC_CLASS. Hence: */
2466# if defined(VGO_darwin)
2467 sres = VG_UCONTEXT_SYSCALL_SYSRES(uc, tst->arch.vex.guest_SC_CLASS);
2468# else
njncda2f0f2009-05-18 02:12:08 +00002469 sres = VG_UCONTEXT_SYSCALL_SYSRES(uc);
njnf76d27a2009-05-28 01:53:07 +00002470# endif
njncda2f0f2009-05-18 02:12:08 +00002471
2472 /* (1) */
sewardja8d8e232005-06-07 20:04:56 +00002473 VG_(fixup_guest_state_after_syscall_interrupted)(
2474 tid,
njnaf839f52005-06-23 03:27:57 +00002475 VG_UCONTEXT_INSTR_PTR(uc),
njncda2f0f2009-05-18 02:12:08 +00002476 sres,
sewardj8eb8bab2015-07-21 14:44:28 +00002477 !!(scss.scss_per_sig[sigNo].scss_flags & VKI_SA_RESTART),
2478 uc
sewardja8d8e232005-06-07 20:04:56 +00002479 );
sewardjb5f6f512005-03-10 23:59:00 +00002480
njncda2f0f2009-05-18 02:12:08 +00002481 /* (2) */
sewardjb5f6f512005-03-10 23:59:00 +00002482 /* Set up the thread's state to deliver a signal */
philippeb3014692015-05-17 13:38:25 +00002483 if (!is_sig_ign(info, tid))
tomadacaf92007-12-21 10:24:24 +00002484 deliver_signal(tid, info, uc);
sewardjb5f6f512005-03-10 23:59:00 +00002485
njncda2f0f2009-05-18 02:12:08 +00002486 /* It's crucial that (1) and (2) happen in the order (1) then (2)
2487 and not the other way around. (1) fixes up the guest thread
2488 state to reflect the fact that the syscall was interrupted --
2489 either to restart the syscall or to return EINTR. (2) then sets
2490 up the thread state to deliver the signal. Then we resume
2491 execution. First, the signal handler is run, since that's the
2492 second adjustment we made to the thread state. If that returns,
2493 then we resume at the guest state created by (1), viz, either
2494 the syscall returns EINTR or is restarted.
2495
2496 If (2) was done before (1) the outcome would be completely
2497 different, and wrong. */
2498
sewardjb5f6f512005-03-10 23:59:00 +00002499 /* longjmp back to the thread's main loop to start executing the
2500 handler. */
njn06244e72005-06-21 22:27:19 +00002501 resume_scheduler(tid);
sewardjb5f6f512005-03-10 23:59:00 +00002502
njn5a350be2009-04-30 03:05:05 +00002503 VG_(core_panic)("async_signalhandler: got unexpected signal "
2504 "while outside of scheduler");
jsgf855d93d2003-10-13 22:26:55 +00002505}
2506
florian017d8f52015-03-23 17:13:04 +00002507/* Extend the stack of thread #tid to cover addr. It is expected that
2508 addr either points into an already mapped anonymous segment or into a
2509 reservation segment abutting the stack segment. Everything else is a bug.
sewardjb5f6f512005-03-10 23:59:00 +00002510
2511 Returns True on success, False on failure.
2512
2513 Succeeds without doing anything if addr is already within a segment.
2514
2515 Failure could be caused by:
florian017d8f52015-03-23 17:13:04 +00002516 - addr not below a growable segment
florian15fa8a22015-03-03 14:56:17 +00002517 - new stack size would exceed the stack limit for the given thread
sewardjb5f6f512005-03-10 23:59:00 +00002518 - mmap failed for some other reason
florian15fa8a22015-03-03 14:56:17 +00002519*/
2520Bool VG_(extend_stack)(ThreadId tid, Addr addr)
sewardjb5f6f512005-03-10 23:59:00 +00002521{
sewardj45f4e7c2005-09-27 19:20:21 +00002522 SizeT udelta;
Elliott Hughesa0664b92017-04-18 17:46:52 -07002523 Addr new_stack_base;
sewardjb5f6f512005-03-10 23:59:00 +00002524
florian15fa8a22015-03-03 14:56:17 +00002525 /* Get the segment containing addr. */
2526 const NSegment* seg = VG_(am_find_nsegment)(addr);
florian017d8f52015-03-23 17:13:04 +00002527 vg_assert(seg != NULL);
sewardj45f4e7c2005-09-27 19:20:21 +00002528
florianbe38cdd2015-03-02 21:10:46 +00002529 /* TODO: the test "seg->kind == SkAnonC" is really inadequate,
2530 because although it tests whether the segment is mapped
2531 _somehow_, it doesn't check that it has the right permissions
2532 (r,w, maybe x) ? */
florian15fa8a22015-03-03 14:56:17 +00002533 if (seg->kind == SkAnonC)
sewardj45f4e7c2005-09-27 19:20:21 +00002534 /* addr is already mapped. Nothing to do. */
sewardjb5f6f512005-03-10 23:59:00 +00002535 return True;
2536
florian15fa8a22015-03-03 14:56:17 +00002537 const NSegment* seg_next = VG_(am_next_nsegment)( seg, True/*fwds*/ );
florian017d8f52015-03-23 17:13:04 +00002538 vg_assert(seg_next != NULL);
sewardjb5f6f512005-03-10 23:59:00 +00002539
sewardj45f4e7c2005-09-27 19:20:21 +00002540 udelta = VG_PGROUNDUP(seg_next->start - addr);
Elliott Hughesa0664b92017-04-18 17:46:52 -07002541 new_stack_base = seg_next->start - udelta;
florian15fa8a22015-03-03 14:56:17 +00002542
sewardj45f4e7c2005-09-27 19:20:21 +00002543 VG_(debugLog)(1, "signals",
Elliott Hughesa0664b92017-04-18 17:46:52 -07002544 "extending a stack base 0x%lx down by %lu"
2545 " new base 0x%lx to cover 0x%lx\n",
2546 seg_next->start, udelta, new_stack_base, addr);
florian15fa8a22015-03-03 14:56:17 +00002547 Bool overflow;
sewardj45f4e7c2005-09-27 19:20:21 +00002548 if (! VG_(am_extend_into_adjacent_reservation_client)
florian15fa8a22015-03-03 14:56:17 +00002549 ( seg_next->start, -(SSizeT)udelta, &overflow )) {
florian15fa8a22015-03-03 14:56:17 +00002550 if (overflow)
floriana5e06c32015-08-05 21:16:09 +00002551 VG_(umsg)("Stack overflow in thread #%u: can't grow stack to %#lx\n",
florian15fa8a22015-03-03 14:56:17 +00002552 tid, new_stack_base);
2553 else
floriana5e06c32015-08-05 21:16:09 +00002554 VG_(umsg)("Cannot map memory to grow the stack for thread #%u "
florian15fa8a22015-03-03 14:56:17 +00002555 "to %#lx\n", tid, new_stack_base);
sewardjb5f6f512005-03-10 23:59:00 +00002556 return False;
sewardj45f4e7c2005-09-27 19:20:21 +00002557 }
sewardjb5f6f512005-03-10 23:59:00 +00002558
rjwalsh0140af52005-06-04 20:42:33 +00002559 /* When we change the main stack, we have to let the stack handling
2560 code know about it. */
Elliott Hughesa0664b92017-04-18 17:46:52 -07002561 VG_(change_stack)(VG_(clstk_id), new_stack_base, VG_(clstk_end));
sewardjb5f6f512005-03-10 23:59:00 +00002562
2563 if (VG_(clo_sanity_level) > 2)
2564 VG_(sanity_check_general)(False);
2565
2566 return True;
2567}
2568
philippeb5a02e72015-10-22 19:14:30 +00002569static fault_catcher_t fault_catcher = NULL;
sewardjb5f6f512005-03-10 23:59:00 +00002570
philippeb5a02e72015-10-22 19:14:30 +00002571fault_catcher_t VG_(set_fault_catcher)(fault_catcher_t catcher)
sewardjb5f6f512005-03-10 23:59:00 +00002572{
philippeb5a02e72015-10-22 19:14:30 +00002573 fault_catcher_t prev_catcher = fault_catcher;
sewardjb5f6f512005-03-10 23:59:00 +00002574 fault_catcher = catcher;
philippeb5a02e72015-10-22 19:14:30 +00002575 return prev_catcher;
sewardjb5f6f512005-03-10 23:59:00 +00002576}
2577
njn96986052009-04-30 07:41:24 +00002578static
njn2d5ff4f2009-05-03 22:53:19 +00002579void sync_signalhandler_from_user ( ThreadId tid,
njn96986052009-04-30 07:41:24 +00002580 Int sigNo, vki_siginfo_t *info, struct vki_ucontext *uc )
2581{
2582 ThreadId qtid;
2583
njn2d5ff4f2009-05-03 22:53:19 +00002584 /* If some user-process sent us a sync signal (ie. it's not the result
njn96986052009-04-30 07:41:24 +00002585 of a faulting instruction), then how we treat it depends on when it
2586 arrives... */
2587
sewardj8eb8bab2015-07-21 14:44:28 +00002588 if (VG_(threads)[tid].status == VgTs_WaitSys
2589# if defined(VGO_solaris)
2590 /* Check if the signal was really received while doing a blocking
2591 syscall. Only then the async_signalhandler() path can be used. */
2592 && VG_(is_ip_in_blocking_syscall)(tid, VG_UCONTEXT_INSTR_PTR(uc))
2593# endif
2594 ) {
njn96986052009-04-30 07:41:24 +00002595 /* Signal arrived while we're blocked in a syscall. This means that
2596 the client's signal mask was applied. In other words, so we can't
2597 get here unless the client wants this signal right now. This means
2598 we can simply use the async_signalhandler. */
2599 if (VG_(clo_trace_signals))
sewardj738856f2009-07-15 14:48:32 +00002600 VG_(dmsg)("Delivering user-sent sync signal %d as async signal\n",
2601 sigNo);
njn96986052009-04-30 07:41:24 +00002602
2603 async_signalhandler(sigNo, info, uc);
2604 VG_(core_panic)("async_signalhandler returned!?\n");
2605
2606 } else {
2607 /* Signal arrived while in generated client code, or while running
2608 Valgrind core code. That means that every thread has these signals
2609 unblocked, so we can't rely on the kernel to route them properly, so
2610 we need to queue them manually. */
2611 if (VG_(clo_trace_signals))
sewardj738856f2009-07-15 14:48:32 +00002612 VG_(dmsg)("Routing user-sent sync signal %d via queue\n", sigNo);
njn96986052009-04-30 07:41:24 +00002613
2614# if defined(VGO_linux)
2615 /* On Linux, first we have to do a sanity check of the siginfo. */
2616 if (info->VKI_SIGINFO_si_pid == 0) {
2617 /* There's a per-user limit of pending siginfo signals. If
2618 you exceed this, by having more than that number of
2619 pending signals with siginfo, then new signals are
2620 delivered without siginfo. This condition can be caused
2621 by any unrelated program you're running at the same time
2622 as Valgrind, if it has a large number of pending siginfo
2623 signals which it isn't taking delivery of.
2624
2625 Since we depend on siginfo to work out why we were sent a
2626 signal and what we should do about it, we really can't
2627 continue unless we get it. */
sewardj738856f2009-07-15 14:48:32 +00002628 VG_(umsg)("Signal %d (%s) appears to have lost its siginfo; "
philippe886fde32012-03-29 21:56:47 +00002629 "I can't go on.\n", sigNo, VG_(signame)(sigNo));
njn96986052009-04-30 07:41:24 +00002630 VG_(printf)(
2631" This may be because one of your programs has consumed your ration of\n"
2632" siginfo structures. For more information, see:\n"
2633" http://kerneltrap.org/mailarchive/1/message/25599/thread\n"
2634" Basically, some program on your system is building up a large queue of\n"
2635" pending signals, and this causes the siginfo data for other signals to\n"
2636" be dropped because it's exceeding a system limit. However, Valgrind\n"
2637" absolutely needs siginfo for SIGSEGV. A workaround is to track down the\n"
2638" offending program and avoid running it while using Valgrind, but there\n"
2639" is no easy way to do this. Apparently the problem was fixed in kernel\n"
2640" 2.6.12.\n");
2641
2642 /* It's a fatal signal, so we force the default handler. */
2643 VG_(set_default_handler)(sigNo);
2644 deliver_signal(tid, info, uc);
2645 resume_scheduler(tid);
2646 VG_(exit)(99); /* If we can't resume, then just exit */
2647 }
2648# endif
2649
2650 qtid = 0; /* shared pending by default */
2651# if defined(VGO_linux)
2652 if (info->si_code == VKI_SI_TKILL)
2653 qtid = tid; /* directed to us specifically */
2654# endif
2655 queue_signal(qtid, info);
2656 }
2657}
2658
sewardjb5b87402011-03-07 16:05:35 +00002659/* Returns the reported fault address for an exact address */
2660static Addr fault_mask(Addr in)
2661{
2662 /* We have to use VG_PGROUNDDN because faults on s390x only deliver
2663 the page address but not the address within a page.
2664 */
2665# if defined(VGA_s390x)
2666 return VG_PGROUNDDN(in);
2667# else
2668 return in;
2669#endif
2670}
2671
njn96986052009-04-30 07:41:24 +00002672/* Returns True if the sync signal was due to the stack requiring extension
2673 and the extension was successful.
2674*/
2675static Bool extend_stack_if_appropriate(ThreadId tid, vki_siginfo_t* info)
2676{
2677 Addr fault;
2678 Addr esp;
florian4465bd52015-04-14 19:59:21 +00002679 NSegment const *seg, *seg_next;
njn96986052009-04-30 07:41:24 +00002680
2681 if (info->si_signo != VKI_SIGSEGV)
2682 return False;
2683
2684 fault = (Addr)info->VKI_SIGINFO_si_addr;
2685 esp = VG_(get_SP)(tid);
2686 seg = VG_(am_find_nsegment)(fault);
florian4465bd52015-04-14 19:59:21 +00002687 seg_next = seg ? VG_(am_next_nsegment)( seg, True/*fwds*/ )
2688 : NULL;
njn96986052009-04-30 07:41:24 +00002689
2690 if (VG_(clo_trace_signals)) {
2691 if (seg == NULL)
floriana5e06c32015-08-05 21:16:09 +00002692 VG_(dmsg)("SIGSEGV: si_code=%d faultaddr=%#lx tid=%u ESP=%#lx "
sewardj738856f2009-07-15 14:48:32 +00002693 "seg=NULL\n",
2694 info->si_code, fault, tid, esp);
njn96986052009-04-30 07:41:24 +00002695 else
floriana5e06c32015-08-05 21:16:09 +00002696 VG_(dmsg)("SIGSEGV: si_code=%d faultaddr=%#lx tid=%u ESP=%#lx "
sewardj738856f2009-07-15 14:48:32 +00002697 "seg=%#lx-%#lx\n",
2698 info->si_code, fault, tid, esp, seg->start, seg->end);
njn96986052009-04-30 07:41:24 +00002699 }
2700
2701 if (info->si_code == VKI_SEGV_MAPERR
2702 && seg
florian4465bd52015-04-14 19:59:21 +00002703 && seg->kind == SkResvn
2704 && seg->smode == SmUpper
2705 && seg_next
2706 && seg_next->kind == SkAnonC
sewardjb5b87402011-03-07 16:05:35 +00002707 && fault >= fault_mask(esp - VG_STACK_REDZONE_SZB)) {
njn96986052009-04-30 07:41:24 +00002708 /* If the fault address is above esp but below the current known
2709 stack segment base, and it was a fault because there was
2710 nothing mapped there (as opposed to a permissions fault),
2711 then extend the stack segment.
2712 */
2713 Addr base = VG_PGROUNDDN(esp - VG_STACK_REDZONE_SZB);
Elliott Hughesa0664b92017-04-18 17:46:52 -07002714 if (VG_(am_addr_is_in_extensible_client_stack)(base)
2715 && VG_(extend_stack)(tid, base)) {
njn96986052009-04-30 07:41:24 +00002716 if (VG_(clo_trace_signals))
sewardj738856f2009-07-15 14:48:32 +00002717 VG_(dmsg)(" -> extended stack base to %#lx\n",
2718 VG_PGROUNDDN(fault));
njn96986052009-04-30 07:41:24 +00002719 return True;
2720 } else {
njn96986052009-04-30 07:41:24 +00002721 return False;
2722 }
2723 } else {
2724 return False;
2725 }
2726}
2727
2728static
njn2d5ff4f2009-05-03 22:53:19 +00002729void sync_signalhandler_from_kernel ( ThreadId tid,
njn96986052009-04-30 07:41:24 +00002730 Int sigNo, vki_siginfo_t *info, struct vki_ucontext *uc )
2731{
2732 /* Check to see if some part of Valgrind itself is interested in faults.
2733 The fault catcher should never be set whilst we're in generated code, so
2734 check for that. AFAIK the only use of the catcher right now is
2735 memcheck's leak detector. */
2736 if (fault_catcher) {
2737 vg_assert(VG_(in_generated_code) == False);
2738
2739 (*fault_catcher)(sigNo, (Addr)info->VKI_SIGINFO_si_addr);
2740 /* If the catcher returns, then it didn't handle the fault,
2741 so carry on panicking. */
2742 }
2743
2744 if (extend_stack_if_appropriate(tid, info)) {
2745 /* Stack extension occurred, so we don't need to do anything else; upon
2746 returning from this function, we'll restart the host (hence guest)
2747 instruction. */
2748 } else {
2749 /* OK, this is a signal we really have to deal with. If it came
2750 from the client's code, then we can jump back into the scheduler
2751 and have it delivered. Otherwise it's a Valgrind bug. */
2752 ThreadState *tst = VG_(get_ThreadState)(tid);
2753
2754 if (VG_(sigismember)(&tst->sig_mask, sigNo)) {
2755 /* signal is blocked, but they're not allowed to block faults */
2756 VG_(set_default_handler)(sigNo);
2757 }
2758
2759 if (VG_(in_generated_code)) {
philippeb3014692015-05-17 13:38:25 +00002760 if (VG_(gdbserver_report_signal) (info, tid)
sewardj3b290482011-05-06 21:02:55 +00002761 || VG_(sigismember)(&tst->sig_mask, sigNo)) {
2762 /* Can't continue; must longjmp back to the scheduler and thus
2763 enter the sighandler immediately. */
2764 deliver_signal(tid, info, uc);
2765 resume_scheduler(tid);
2766 }
2767 else
2768 resume_scheduler(tid);
njn96986052009-04-30 07:41:24 +00002769 }
2770
2771 /* If resume_scheduler returns or its our fault, it means we
2772 don't have longjmp set up, implying that we weren't running
2773 client code, and therefore it was actually generated by
2774 Valgrind internally.
2775 */
sewardj738856f2009-07-15 14:48:32 +00002776 VG_(dmsg)("VALGRIND INTERNAL ERROR: Valgrind received "
2777 "a signal %d (%s) - exiting\n",
philippe886fde32012-03-29 21:56:47 +00002778 sigNo, VG_(signame)(sigNo));
njn96986052009-04-30 07:41:24 +00002779
floriana5e06c32015-08-05 21:16:09 +00002780 VG_(dmsg)("si_code=%d; Faulting address: %p; sp: %#lx\n",
sewardj738856f2009-07-15 14:48:32 +00002781 info->si_code, info->VKI_SIGINFO_si_addr,
2782 VG_UCONTEXT_STACK_PTR(uc));
njn96986052009-04-30 07:41:24 +00002783
2784 if (0)
2785 VG_(kill_self)(sigNo); /* generate a core dump */
2786
2787 //if (tid == 0) /* could happen after everyone has exited */
2788 // tid = VG_(master_tid);
2789 vg_assert(tid != 0);
2790
sewardj59570ff2010-01-01 11:59:33 +00002791 UnwindStartRegs startRegs;
2792 VG_(memset)(&startRegs, 0, sizeof(startRegs));
2793
2794 VG_UCONTEXT_TO_UnwindStartRegs(&startRegs, uc);
2795 VG_(core_panic_at)("Killed by fatal signal", &startRegs);
njn96986052009-04-30 07:41:24 +00002796 }
2797}
sewardjb5f6f512005-03-10 23:59:00 +00002798
jsgf855d93d2003-10-13 22:26:55 +00002799/*
sewardj2a99cf62004-11-24 10:44:19 +00002800 Receive a sync signal from the host.
jsgf855d93d2003-10-13 22:26:55 +00002801*/
2802static
njn5a350be2009-04-30 03:05:05 +00002803void sync_signalhandler ( Int sigNo,
2804 vki_siginfo_t *info, struct vki_ucontext *uc )
jsgf855d93d2003-10-13 22:26:55 +00002805{
sewardj42781722006-12-17 19:36:06 +00002806 ThreadId tid = VG_(lwpid_to_vgtid)(VG_(gettid)());
njn2d5ff4f2009-05-03 22:53:19 +00002807 Bool from_user;
jsgf855d93d2003-10-13 22:26:55 +00002808
njn5a350be2009-04-30 03:05:05 +00002809 if (0)
2810 VG_(printf)("sync_sighandler(%d, %p, %p)\n", sigNo, info, uc);
2811
jsgf855d93d2003-10-13 22:26:55 +00002812 vg_assert(info != NULL);
2813 vg_assert(info->si_signo == sigNo);
Elliott Hughesa0664b92017-04-18 17:46:52 -07002814 vg_assert(sigNo == VKI_SIGSEGV
2815 || sigNo == VKI_SIGBUS
2816 || sigNo == VKI_SIGFPE
2817 || sigNo == VKI_SIGILL
2818 || sigNo == VKI_SIGTRAP);
jsgf855d93d2003-10-13 22:26:55 +00002819
njn3b6b2aa2009-04-30 03:30:09 +00002820 info->si_code = sanitize_si_code(info->si_code);
tom9f4a7bf2005-11-13 00:01:20 +00002821
njnf76d27a2009-05-28 01:53:07 +00002822 from_user = !is_signal_from_kernel(tid, sigNo, info->si_code);
njn81f52d12009-04-30 03:49:06 +00002823
2824 if (VG_(clo_trace_signals)) {
sewardj738856f2009-07-15 14:48:32 +00002825 VG_(dmsg)("sync signal handler: "
2826 "signal=%d, si_code=%d, EIP=%#lx, eip=%#lx, from %s\n",
2827 sigNo, info->si_code, VG_(get_IP)(tid),
2828 VG_UCONTEXT_INSTR_PTR(uc),
2829 ( from_user ? "user" : "kernel" ));
njn81f52d12009-04-30 03:49:06 +00002830 }
2831 vg_assert(sigNo >= 1 && sigNo <= VG_(max_signal));
2832
njn36fce1b2009-04-28 01:55:01 +00002833 /* // debug code:
2834 if (0) {
2835 VG_(printf)("info->si_signo %d\n", info->si_signo);
2836 VG_(printf)("info->si_errno %d\n", info->si_errno);
2837 VG_(printf)("info->si_code %d\n", info->si_code);
2838 VG_(printf)("info->si_pid %d\n", info->si_pid);
2839 VG_(printf)("info->si_uid %d\n", info->si_uid);
2840 VG_(printf)("info->si_status %d\n", info->si_status);
2841 VG_(printf)("info->si_addr %p\n", info->si_addr);
2842 }
2843 */
2844
2845 /* Figure out if the signal is being sent from outside the process.
2846 (Why do we care?) If the signal is from the user rather than the
njn96986052009-04-30 07:41:24 +00002847 kernel, then treat it more like an async signal than a sync signal --
njn36fce1b2009-04-28 01:55:01 +00002848 that is, merely queue it for later delivery. */
njn2d5ff4f2009-05-03 22:53:19 +00002849 if (from_user) {
njnc8a91072009-05-20 06:51:11 +00002850 sync_signalhandler_from_user( tid, sigNo, info, uc);
njn96986052009-04-30 07:41:24 +00002851 } else {
njnc8a91072009-05-20 06:51:11 +00002852 sync_signalhandler_from_kernel(tid, sigNo, info, uc);
jsgf855d93d2003-10-13 22:26:55 +00002853 }
sewardj8eb8bab2015-07-21 14:44:28 +00002854
2855# if defined(VGO_solaris)
2856 /* On Solaris we have to return from signal handler manually. */
2857 VG_(do_syscall2)(__NR_context, VKI_SETCONTEXT, (UWord)uc);
2858# endif
jsgf855d93d2003-10-13 22:26:55 +00002859}
2860
2861
2862/*
sewardjb5f6f512005-03-10 23:59:00 +00002863 Kill this thread. Makes it leave any syscall it might be currently
2864 blocked in, and return to the scheduler. This doesn't mark the thread
2865 as exiting; that's the caller's job.
jsgf855d93d2003-10-13 22:26:55 +00002866 */
njn5a350be2009-04-30 03:05:05 +00002867static void sigvgkill_handler(int signo, vki_siginfo_t *si,
2868 struct vki_ucontext *uc)
jsgf855d93d2003-10-13 22:26:55 +00002869{
sewardj42781722006-12-17 19:36:06 +00002870 ThreadId tid = VG_(lwpid_to_vgtid)(VG_(gettid)());
sewardj489bfec2006-10-17 02:00:29 +00002871 ThreadStatus at_signal = VG_(threads)[tid].status;
sewardjb5f6f512005-03-10 23:59:00 +00002872
2873 if (VG_(clo_trace_signals))
floriana5e06c32015-08-05 21:16:09 +00002874 VG_(dmsg)("sigvgkill for lwp %d tid %u\n", VG_(gettid)(), tid);
sewardj489bfec2006-10-17 02:00:29 +00002875
sewardjad0a3a82006-12-17 18:58:55 +00002876 VG_(acquire_BigLock)(tid, "sigvgkill_handler");
sewardjb5f6f512005-03-10 23:59:00 +00002877
njn351d0062005-06-21 22:23:59 +00002878 vg_assert(signo == VG_SIGVGKILL);
jsgf855d93d2003-10-13 22:26:55 +00002879 vg_assert(si->si_signo == signo);
2880
sewardj489bfec2006-10-17 02:00:29 +00002881 /* jrs 2006 August 3: the following assertion seems incorrect to
2882 me, and fails on AIX. sigvgkill could be sent to a thread which
2883 is runnable - see VG_(nuke_all_threads_except) in the scheduler.
2884 Hence comment these out ..
2885
2886 vg_assert(VG_(threads)[tid].status == VgTs_WaitSys);
2887 VG_(post_syscall)(tid);
2888
2889 and instead do:
2890 */
2891 if (at_signal == VgTs_WaitSys)
2892 VG_(post_syscall)(tid);
2893 /* jrs 2006 August 3 ends */
sewardjb5f6f512005-03-10 23:59:00 +00002894
njn06244e72005-06-21 22:27:19 +00002895 resume_scheduler(tid);
sewardjb5f6f512005-03-10 23:59:00 +00002896
2897 VG_(core_panic)("sigvgkill_handler couldn't return to the scheduler\n");
sewardjde4a1d02002-03-22 01:27:54 +00002898}
2899
sewardjde4a1d02002-03-22 01:27:54 +00002900static __attribute((unused))
njncda2f0f2009-05-18 02:12:08 +00002901void pp_ksigaction ( vki_sigaction_toK_t* sa )
sewardjde4a1d02002-03-22 01:27:54 +00002902{
2903 Int i;
njn695c16e2005-03-27 03:40:28 +00002904 VG_(printf)("pp_ksigaction: handler %p, flags 0x%x, restorer %p\n",
sewardj489bfec2006-10-17 02:00:29 +00002905 sa->ksa_handler,
2906 (UInt)sa->sa_flags,
sewardj8eb8bab2015-07-21 14:44:28 +00002907# if !defined(VGP_x86_darwin) && !defined(VGP_amd64_darwin) && \
2908 !defined(VGO_solaris)
sewardj489bfec2006-10-17 02:00:29 +00002909 sa->sa_restorer
2910# else
sewardja8ffda62008-07-18 18:23:24 +00002911 (void*)0
sewardj489bfec2006-10-17 02:00:29 +00002912# endif
2913 );
njn695c16e2005-03-27 03:40:28 +00002914 VG_(printf)("pp_ksigaction: { ");
sewardjb5f6f512005-03-10 23:59:00 +00002915 for (i = 1; i <= VG_(max_signal); i++)
Elliott Hughesa0664b92017-04-18 17:46:52 -07002916 if (VG_(sigismember)(&(sa->sa_mask),i))
sewardjde4a1d02002-03-22 01:27:54 +00002917 VG_(printf)("%d ", i);
2918 VG_(printf)("}\n");
2919}
2920
jsgf855d93d2003-10-13 22:26:55 +00002921/*
sewardjb5f6f512005-03-10 23:59:00 +00002922 Force signal handler to default
jsgf855d93d2003-10-13 22:26:55 +00002923 */
sewardjb5f6f512005-03-10 23:59:00 +00002924void VG_(set_default_handler)(Int signo)
2925{
njncda2f0f2009-05-18 02:12:08 +00002926 vki_sigaction_toK_t sa;
sewardjb5f6f512005-03-10 23:59:00 +00002927
2928 sa.ksa_handler = VKI_SIG_DFL;
2929 sa.sa_flags = 0;
sewardj8eb8bab2015-07-21 14:44:28 +00002930# if !defined(VGP_x86_darwin) && !defined(VGP_amd64_darwin) && \
2931 !defined(VGO_solaris)
sewardjb5f6f512005-03-10 23:59:00 +00002932 sa.sa_restorer = 0;
sewardj489bfec2006-10-17 02:00:29 +00002933# endif
sewardjb5f6f512005-03-10 23:59:00 +00002934 VG_(sigemptyset)(&sa.sa_mask);
2935
2936 VG_(do_sys_sigaction)(signo, &sa, NULL);
2937}
2938
2939/*
2940 Poll for pending signals, and set the next one up for delivery.
2941 */
2942void VG_(poll_signals)(ThreadId tid)
jsgf855d93d2003-10-13 22:26:55 +00002943{
sewardjb5f6f512005-03-10 23:59:00 +00002944 vki_siginfo_t si, *sip;
2945 vki_sigset_t pollset;
2946 ThreadState *tst = VG_(get_ThreadState)(tid);
sewardjb5f6f512005-03-10 23:59:00 +00002947 vki_sigset_t saved_mask;
jsgf855d93d2003-10-13 22:26:55 +00002948
sewardjb5f6f512005-03-10 23:59:00 +00002949 /* look for all the signals this thread isn't blocking */
njncda2f0f2009-05-18 02:12:08 +00002950 /* pollset = ~tst->sig_mask */
2951 VG_(sigcomplementset)( &pollset, &tst->sig_mask );
jsgf855d93d2003-10-13 22:26:55 +00002952
njn444eba12005-05-12 03:47:31 +00002953 block_all_host_signals(&saved_mask); // protect signal queue
sewardjb5f6f512005-03-10 23:59:00 +00002954
2955 /* First look for any queued pending signals */
2956 sip = next_queued(tid, &pollset); /* this thread */
2957
2958 if (sip == NULL)
2959 sip = next_queued(0, &pollset); /* process-wide */
2960
2961 /* If there was nothing queued, ask the kernel for a pending signal */
sewardj489bfec2006-10-17 02:00:29 +00002962 if (sip == NULL && VG_(sigtimedwait_zero)(&pollset, &si) > 0) {
sewardjb5f6f512005-03-10 23:59:00 +00002963 if (VG_(clo_trace_signals))
floriana5e06c32015-08-05 21:16:09 +00002964 VG_(dmsg)("poll_signals: got signal %d for thread %u\n",
sewardj738856f2009-07-15 14:48:32 +00002965 si.si_signo, tid);
sewardjb5f6f512005-03-10 23:59:00 +00002966 sip = &si;
thughes8abf3922004-10-16 10:59:49 +00002967 }
fitzhardingee1c06d82003-10-30 07:21:44 +00002968
sewardjb5f6f512005-03-10 23:59:00 +00002969 if (sip != NULL) {
2970 /* OK, something to do; deliver it */
2971 if (VG_(clo_trace_signals))
floriana5e06c32015-08-05 21:16:09 +00002972 VG_(dmsg)("Polling found signal %d for tid %u\n", sip->si_signo, tid);
philippeb3014692015-05-17 13:38:25 +00002973 if (!is_sig_ign(sip, tid))
tomadacaf92007-12-21 10:24:24 +00002974 deliver_signal(tid, sip, NULL);
sewardjb5f6f512005-03-10 23:59:00 +00002975 else if (VG_(clo_trace_signals))
sewardj738856f2009-07-15 14:48:32 +00002976 VG_(dmsg)(" signal %d ignored\n", sip->si_signo);
sewardjb5f6f512005-03-10 23:59:00 +00002977
2978 sip->si_signo = 0; /* remove from signal queue, if that's
2979 where it came from */
jsgf855d93d2003-10-13 22:26:55 +00002980 }
2981
njn444eba12005-05-12 03:47:31 +00002982 restore_all_host_signals(&saved_mask);
sewardjb5f6f512005-03-10 23:59:00 +00002983}
2984
sewardj018f7622002-05-15 21:13:39 +00002985/* At startup, copy the process' real signal state to the SCSS.
2986 Whilst doing this, block all real signals. Then calculate SKSS and
2987 set the kernel to that. Also initialise DCSS.
sewardjde4a1d02002-03-22 01:27:54 +00002988*/
2989void VG_(sigstartup_actions) ( void )
2990{
njncda2f0f2009-05-18 02:12:08 +00002991 Int i, ret, vKI_SIGRTMIN;
nethercote73b526f2004-10-31 18:48:21 +00002992 vki_sigset_t saved_procmask;
njncda2f0f2009-05-18 02:12:08 +00002993 vki_sigaction_fromK_t sa;
2994
2995 VG_(memset)(&scss, 0, sizeof(scss));
2996 VG_(memset)(&skss, 0, sizeof(skss));
2997
2998# if defined(VKI_SIGRTMIN)
2999 vKI_SIGRTMIN = VKI_SIGRTMIN;
3000# else
3001 vKI_SIGRTMIN = 0; /* eg Darwin */
3002# endif
sewardjde4a1d02002-03-22 01:27:54 +00003003
sewardj2e93c502002-04-12 11:12:52 +00003004 /* VG_(printf)("SIGSTARTUP\n"); */
jsgf855d93d2003-10-13 22:26:55 +00003005 /* Block all signals. saved_procmask remembers the previous mask,
3006 which the first thread inherits.
3007 */
njn444eba12005-05-12 03:47:31 +00003008 block_all_host_signals( &saved_procmask );
sewardjde4a1d02002-03-22 01:27:54 +00003009
sewardj018f7622002-05-15 21:13:39 +00003010 /* Copy per-signal settings to SCSS. */
nethercote73b526f2004-10-31 18:48:21 +00003011 for (i = 1; i <= _VKI_NSIG; i++) {
sewardj018f7622002-05-15 21:13:39 +00003012 /* Get the old host action */
nethercote73b526f2004-10-31 18:48:21 +00003013 ret = VG_(sigaction)(i, NULL, &sa);
sewardj018f7622002-05-15 21:13:39 +00003014
njnea2d6fd2010-07-01 00:20:20 +00003015# if defined(VGP_x86_darwin) || defined(VGP_amd64_darwin)
njnf76d27a2009-05-28 01:53:07 +00003016 /* apparently we may not even ask about the disposition of these
3017 signals, let alone change them */
3018 if (ret != 0 && (i == VKI_SIGKILL || i == VKI_SIGSTOP))
3019 continue;
3020# endif
3021
sewardjb5f6f512005-03-10 23:59:00 +00003022 if (ret != 0)
3023 break;
3024
3025 /* Try setting it back to see if this signal is really
3026 available */
njncda2f0f2009-05-18 02:12:08 +00003027 if (vKI_SIGRTMIN > 0 /* it actually exists on this platform */
3028 && i >= vKI_SIGRTMIN) {
3029 vki_sigaction_toK_t tsa, sa2;
sewardjb5f6f512005-03-10 23:59:00 +00003030
njn695c16e2005-03-27 03:40:28 +00003031 tsa.ksa_handler = (void *)sync_signalhandler;
sewardjb5f6f512005-03-10 23:59:00 +00003032 tsa.sa_flags = VKI_SA_SIGINFO;
sewardj8eb8bab2015-07-21 14:44:28 +00003033# if !defined(VGP_x86_darwin) && !defined(VGP_amd64_darwin) && \
3034 !defined(VGO_solaris)
sewardjb5f6f512005-03-10 23:59:00 +00003035 tsa.sa_restorer = 0;
sewardj489bfec2006-10-17 02:00:29 +00003036# endif
sewardjb5f6f512005-03-10 23:59:00 +00003037 VG_(sigfillset)(&tsa.sa_mask);
3038
3039 /* try setting it to some arbitrary handler */
3040 if (VG_(sigaction)(i, &tsa, NULL) != 0) {
3041 /* failed - not really usable */
3042 break;
3043 }
3044
njncda2f0f2009-05-18 02:12:08 +00003045 VG_(convert_sigaction_fromK_to_toK)( &sa, &sa2 );
3046 ret = VG_(sigaction)(i, &sa2, NULL);
sewardjb5f6f512005-03-10 23:59:00 +00003047 vg_assert(ret == 0);
3048 }
3049
3050 VG_(max_signal) = i;
3051
3052 if (VG_(clo_trace_signals) && VG_(clo_verbosity) > 2)
njn8a7b41b2007-09-23 00:51:24 +00003053 VG_(printf)("snaffling handler 0x%lx for signal %d\n",
sewardj018f7622002-05-15 21:13:39 +00003054 (Addr)(sa.ksa_handler), i );
3055
njn695c16e2005-03-27 03:40:28 +00003056 scss.scss_per_sig[i].scss_handler = sa.ksa_handler;
3057 scss.scss_per_sig[i].scss_flags = sa.sa_flags;
3058 scss.scss_per_sig[i].scss_mask = sa.sa_mask;
njncda2f0f2009-05-18 02:12:08 +00003059
3060 scss.scss_per_sig[i].scss_restorer = NULL;
sewardj8eb8bab2015-07-21 14:44:28 +00003061# if !defined(VGP_x86_darwin) && !defined(VGP_amd64_darwin) && \
3062 !defined(VGO_solaris)
njn695c16e2005-03-27 03:40:28 +00003063 scss.scss_per_sig[i].scss_restorer = sa.sa_restorer;
sewardj489bfec2006-10-17 02:00:29 +00003064# endif
njncda2f0f2009-05-18 02:12:08 +00003065
3066 scss.scss_per_sig[i].scss_sa_tramp = NULL;
njnf76d27a2009-05-28 01:53:07 +00003067# if defined(VGP_x86_darwin) || defined(VGP_amd64_darwin)
3068 scss.scss_per_sig[i].scss_sa_tramp = NULL;
3069 /*sa.sa_tramp;*/
3070 /* We can't know what it was, because Darwin's sys_sigaction
3071 doesn't tell us. */
3072# endif
sewardj018f7622002-05-15 21:13:39 +00003073 }
3074
sewardjb5f6f512005-03-10 23:59:00 +00003075 if (VG_(clo_trace_signals))
sewardj738856f2009-07-15 14:48:32 +00003076 VG_(dmsg)("Max kernel-supported signal is %d\n", VG_(max_signal));
sewardjb5f6f512005-03-10 23:59:00 +00003077
jsgf855d93d2003-10-13 22:26:55 +00003078 /* Our private internal signals are treated as ignored */
njn351d0062005-06-21 22:23:59 +00003079 scss.scss_per_sig[VG_SIGVGKILL].scss_handler = VKI_SIG_IGN;
3080 scss.scss_per_sig[VG_SIGVGKILL].scss_flags = VKI_SA_SIGINFO;
3081 VG_(sigfillset)(&scss.scss_per_sig[VG_SIGVGKILL].scss_mask);
jsgf855d93d2003-10-13 22:26:55 +00003082
sewardj018f7622002-05-15 21:13:39 +00003083 /* Copy the process' signal mask into the root thread. */
sewardj1d887112005-05-30 21:44:08 +00003084 vg_assert(VG_(threads)[1].status == VgTs_Init);
3085 for (i = 2; i < VG_N_THREADS; i++)
3086 vg_assert(VG_(threads)[i].status == VgTs_Empty);
3087
3088 VG_(threads)[1].sig_mask = saved_procmask;
3089 VG_(threads)[1].tmp_sig_mask = saved_procmask;
sewardj7a61d912002-04-25 01:27:35 +00003090
sewardj018f7622002-05-15 21:13:39 +00003091 /* Calculate SKSS and apply it. This also sets the initial kernel
3092 mask we need to run with. */
nethercote9dd11512004-08-04 15:31:30 +00003093 handle_SCSS_change( True /* forced update */ );
jsgf855d93d2003-10-13 22:26:55 +00003094
sewardjb5f6f512005-03-10 23:59:00 +00003095 /* Leave with all signals still blocked; the thread scheduler loop
3096 will set the appropriate mask at the appropriate time. */
sewardjde4a1d02002-03-22 01:27:54 +00003097}
3098
sewardjde4a1d02002-03-22 01:27:54 +00003099/*--------------------------------------------------------------------*/
njned6b8242005-06-01 00:03:17 +00003100/*--- end ---*/
sewardjde4a1d02002-03-22 01:27:54 +00003101/*--------------------------------------------------------------------*/