1/*
2 * Copyright (C) 2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#if USE(PTHREADS) && HAVE(MACHINE_CONTEXT)
29
30#include <signal.h>
31#include <tuple>
32#include <wtf/Function.h>
33#include <wtf/Optional.h>
34#include <wtf/PlatformRegisters.h>
35
36namespace WTF {
37
38// Note that SIGUSR1 is used in Pthread-based ports except for Darwin to suspend and resume threads.
39enum class Signal {
40 // Usr will always chain to any non-default handler install before us. Since there is no way to know
41 // if a signal was intended exclusively for us.
42 Usr,
43
44 // These signals will only chain if we don't have a handler that can process them. If there is nothing
45 // to chain to we restore the default handler and crash.
46 Ill,
47 BadAccess, // For posix this is both SIGSEGV and SIGBUS
48 NumberOfSignals = BadAccess + 2, // BadAccess is really two signals.
49 Unknown = NumberOfSignals
50};
51
52inline std::tuple<int, Optional<int>> toSystemSignal(Signal signal)
53{
54 switch (signal) {
55 case Signal::BadAccess: return std::make_tuple(SIGSEGV, SIGBUS);
56 case Signal::Ill: return std::make_tuple(SIGILL, WTF::nullopt);
57 case Signal::Usr: return std::make_tuple(SIGILL, WTF::nullopt);
58 default: break;
59 }
60 RELEASE_ASSERT_NOT_REACHED();
61}
62
63inline Signal fromSystemSignal(int signal)
64{
65 switch (signal) {
66 case SIGSEGV: return Signal::BadAccess;
67 case SIGBUS: return Signal::BadAccess;
68 case SIGILL: return Signal::Ill;
69 case SIGUSR2: return Signal::Usr;
70 default: return Signal::Unknown;
71 }
72}
73
74enum class SignalAction {
75 Handled,
76 NotHandled,
77 ForceDefault
78};
79
80struct SigInfo {
81 void* faultingAddress { 0 };
82};
83
84using SignalHandler = Function<SignalAction(Signal, SigInfo&, PlatformRegisters&)>;
85
86// Call this method whenever you want to install a signal handler. It's ok to call this function lazily.
87// Note: Your signal handler will be called every time the handler for the desired signal is called.
88// Thus it is your responsibility to discern if the signal fired was yours.
89// This function is currently a one way street i.e. once installed, a signal handler cannot be uninstalled.
90WTF_EXPORT_PRIVATE void installSignalHandler(Signal, SignalHandler&&);
91
92
93#if HAVE(MACH_EXCEPTIONS)
94class Thread;
95void registerThreadForMachExceptionHandling(Thread&);
96
97void handleSignalsWithMach();
98#endif // HAVE(MACH_EXCEPTIONS)
99
100} // namespace WTF
101
102#if HAVE(MACH_EXCEPTIONS)
103using WTF::registerThreadForMachExceptionHandling;
104using WTF::handleSignalsWithMach;
105#endif // HAVE(MACH_EXCEPTIONS)
106
107using WTF::Signal;
108using WTF::SigInfo;
109using WTF::toSystemSignal;
110using WTF::fromSystemSignal;
111using WTF::SignalAction;
112using WTF::installSignalHandler;
113
114#endif // USE(PTHREADS) && HAVE(MACHINE_CONTEXT)
115