| 1 |
<?xml version="1.0" encoding="utf-8"?> |
| 2 |
<!DOCTYPE book [ |
| 3 |
<!ENTITY % eclent SYSTEM "ecl.ent"> |
| 4 |
%eclent; |
| 5 |
]> |
| 6 |
<book xmlns="http://docbook.org/ns/docbook" version="5.0" xml:lang="en"> |
| 7 |
<chapter xml:id="ext.signals"> |
| 8 |
<title>Signals and interrupts</title> |
| 9 |
|
| 10 |
<section xml:id="ext.signals.intro"> |
| 11 |
<title>Problems associated to signals</title> |
| 12 |
|
| 13 |
<para>POSIX contemplates the notion of "signals", which are events that |
| 14 |
cause a process or a thread to be interrupted. Windows uses the term |
| 15 |
"exception", which includes also a more general kind of errors.</para> |
| 16 |
|
| 17 |
<para>In both cases the consequence is that a thread or process may be |
| 18 |
interrupted at any time, either by causes which are intrinsic to them |
| 19 |
(synchronous signals), such as floating point exceptions, or extrinsic |
| 20 |
(asynchronous signals), such as the process being aborted by the |
| 21 |
user.</para> |
| 22 |
|
| 23 |
<para>Of course, those interruptions are not always welcome. When the |
| 24 |
interrupt is delivered and a handler is invoked, the thread or even the |
| 25 |
whole program may be in an inconsistent state. For instance the thread may |
| 26 |
have acquired a lock, or it may be in the process of filling the fields of |
| 27 |
a structure. Furthermore, sometimes the signal that a process receives may |
| 28 |
not even be related to it, as in the case when a user presses Cltr-C |
| 29 |
and a SIGINT signal is delivered to an arbitrary thread, or when the |
| 30 |
process receives the Windows exception CTRL_CLOSE_EVENT denoting |
| 31 |
that the terminal window is being closed.</para> |
| 32 |
|
| 33 |
<para>Understanding this, POSIX restricts severely what functions can be |
| 34 |
called from a signal handler, thereby limiting its usefulness. However, |
| 35 |
Common Lisp users expect to be able to handle floating point exceptions and |
| 36 |
to gracefully manage user interrupts, program exits, etc. In an attempt to |
| 37 |
solve this seemingly impossible problem, &ECL; has taken a pragmatic |
| 38 |
approach that works, it is rather safe, but involves some work on the &ECL; |
| 39 |
maintainers and also on users that want to embed &ECL; as a library.</para> |
| 40 |
</section> |
| 41 |
|
| 42 |
<section xml:id="ext.signals.kinds"> |
| 43 |
<title>Kinds of signals</title> |
| 44 |
|
| 45 |
<section xml:id="ext.signals.synchronous"> |
| 46 |
<title>Synchronous signals</title> |
| 47 |
|
| 48 |
<para>The name derives from POSIX and it denotes interrupts that occur due |
| 49 |
to the code that a particular thread executes. They are largely equivalent |
| 50 |
to C++ and Java exceptions, and in Windows they are called "unchecked |
| 51 |
exceptions."</para> |
| 52 |
|
| 53 |
<para>Common Lisp programs may generate mostly three kinds of synchronous |
| 54 |
signals:</para> |
| 55 |
<itemizedlist> |
| 56 |
<listitem> |
| 57 |
<para>Floating point exceptions, that result from overflows in |
| 58 |
computations, division by zero, and so on.</para> |
| 59 |
</listitem> |
| 60 |
<listitem> |
| 61 |
<para>Access violations, such as dereferencing NULL pointers, |
| 62 |
writing into regions of memory that are protected, etc.</para> |
| 63 |
</listitem> |
| 64 |
<listitem> |
| 65 |
<para>Process interrupts.</para> |
| 66 |
</listitem> |
| 67 |
</itemizedlist> |
| 68 |
|
| 69 |
<para>The first family of signals are generated by the floating point |
| 70 |
processing hardware in the computer, and they typically happen when code |
| 71 |
is compiled with low security settings, performing mathematical operations |
| 72 |
without checks.</para> |
| 73 |
|
| 74 |
<para>The second family of signals may seem rare, but unfortunately they |
| 75 |
still happen quite often. One scenario is wrong code that handles memory |
| 76 |
directly via FFI. Another one is undetected stack overflows, which typically |
| 77 |
result in access to protected memory regions. Finally, a very common cause |
| 78 |
of these kind of exceptions is invoking a function that has been compiled |
| 79 |
with very low security settings with arguments that are not of the expected |
| 80 |
type -- for instance, passing a float when a structure is expected.</para> |
| 81 |
|
| 82 |
<para>The third family is related to the multiprocessing capabilities in |
| 83 |
Common Lisp systems and more precisely to the <xref |
| 84 |
linkend="ref.mp.interrupt-process"/> function which is used to kill, |
| 85 |
interrupt and inspect arbitrary threads. In POSIX systems &ECL; informs a |
| 86 |
given thread about the need to interrupt its execution by sending a |
| 87 |
particular signal from the set which is available to the user.</para> |
| 88 |
|
| 89 |
<para>Note that in neither of these cases we should let the signal pass |
| 90 |
unnoticed. Access violations and floating point exceptions may propagate |
| 91 |
through the program causing more harm than expected, and without |
| 92 |
process interrupts we will not be able to stop and cancel different |
| 93 |
threads. The only question that remains, though, is whether such signals can |
| 94 |
be handled by the thread in which they were generated and how.</para> |
| 95 |
</section> |
| 96 |
|
| 97 |
<section xml:id="ext.signals.asynchronous"> |
| 98 |
<title>Asynchronous signals</title> |
| 99 |
|
| 100 |
<para>In addition to the set of synchronous signals or "exceptions", we |
| 101 |
have a set of signals that denote "events", things that happen while the |
| 102 |
program is being executed, and "requests". Some typical examples are:</para> |
| 103 |
<itemizedlist> |
| 104 |
<listitem> |
| 105 |
<para>Request for program termination (SIGKILL, SIGTERM).</para> |
| 106 |
</listitem> |
| 107 |
<listitem> |
| 108 |
<para>Indication that a child process has finished.</para> |
| 109 |
</listitem> |
| 110 |
<listitem> |
| 111 |
<para>Request for program interruption (SIGINT), typically as a |
| 112 |
consecuence of pressing a key combination, Ctrl-C.</para> |
| 113 |
</listitem> |
| 114 |
</itemizedlist> |
| 115 |
|
| 116 |
<para>The important difference with synchronous signals is that we have no |
| 117 |
thread that causes the interrupt and thus there is no preferred way of |
| 118 |
handling them. Moreover, the operating system will typically dispatch these |
| 119 |
signals to an arbitrary thread, unless we set up mechanisms to prevent |
| 120 |
it. This can have nasty consequences if the incoming signal interrupt a |
| 121 |
system call, or leaves the interrupted thread in an inconsistent |
| 122 |
state.</para> |
| 123 |
</section> |
| 124 |
</section> |
| 125 |
|
| 126 |
<section xml:id="ext.signals.implementation"> |
| 127 |
<title>Signals and interrupts in &ECL;</title> |
| 128 |
|
| 129 |
<para>The signal handling facilities in &ECL; are constrained by two |
| 130 |
needs. First of all, we can not ignore the synchronous signals mentioned in |
| 131 |
<xref linkend="ext.signals.synchronous"/>. Second, all other signals should |
| 132 |
cause the least harm to the running threads. Third, when a signal is handled |
| 133 |
synchronously using a signal handler, the handler should do almost nothing |
| 134 |
unless we are completely sure that we are in an interruptible region, that is |
| 135 |
outside system calls, in code that &ECL; knows and controls.</para> |
| 136 |
|
| 137 |
<para>The way in which this is solved is based on the existence of both |
| 138 |
synchronous and asynchronous signal handling code, as explained in the |
| 139 |
following two sections.</para> |
| 140 |
|
| 141 |
<section xml:id="ext.signals.asynchronous-handler"> |
| 142 |
<title>Handling of asynchronous signals</title> |
| 143 |
|
| 144 |
<para>In systems in which this is possible, &ECL; creates a signal handling |
| 145 |
thread to detect and process asynchronous signals (See <xref |
| 146 |
linkend="ext.signals.asynchronous"/>). This thread is a trivial one and does |
| 147 |
not process the signals itself: it communicates with, or launches new signal |
| 148 |
handling threads to act accordingly to the denoted events.</para> |
| 149 |
|
| 150 |
<para>The use of a separate thread has some nice consequences. The first |
| 151 |
one is that those signals will not interrupt any sensitive code. The |
| 152 |
second one is that the signal handling thread will be able to execute |
| 153 |
arbitrary lisp or C code, since it is not being executed in a sensitive |
| 154 |
context. Most important, this style of signal handling is the recommended |
| 155 |
one by the POSIX standards, and it is the one that Windows uses.</para> |
| 156 |
|
| 157 |
<para>The installation of the signal handling thread is dictated by a boot |
| 158 |
time option, <varname>ECL_OPT_SIGNAL_HANDLING_THREAD</varname>, and it will |
| 159 |
only be possible in systems that support either POSIX or Windows |
| 160 |
threads.</para> |
| 161 |
|
| 162 |
<para>Systems which embed &ECL; as an extension language may wish to |
| 163 |
deactivate the signal handling thread using the previously mentioned |
| 164 |
option. If this is the case, then they should take appropriate measures to |
| 165 |
avoid interrupting the code in &ECL; when such signals are delivered.</para> |
| 166 |
|
| 167 |
<para>Systems which embed &ECL; and do not mind having a separate signal |
| 168 |
handling thread can control the set of asynchronous signals which is handled |
| 169 |
by this thread. This is done again using the appropriate boot options such |
| 170 |
as <varname>ECL_OPT_TRAP_SIGINT</varname>, |
| 171 |
<varname>ECL_OPT_TRAP_SIGTERM</varname>, etc. Note that in order to detect |
| 172 |
and handle those signals, &ECL; must block them from delivery to any other |
| 173 |
thread. This means changing the <function>sigprocmask()</function> in POSIX |
| 174 |
systems or setting up a custom <function>SetConsoleCtrlHandler()</function> |
| 175 |
in Windows.</para> |
| 176 |
</section> |
| 177 |
|
| 178 |
<section xml:id="ext.signals.synchronous-handler"> |
| 179 |
<title>Handling of synchronous signals</title> |
| 180 |
|
| 181 |
<para>We have already mentioned that certain synchronous signals and |
| 182 |
exceptions can not be ignored and yet the corresponding signal handlers are |
| 183 |
not able to execute arbitrary code. To solve this seemingly impossible |
| 184 |
contradiction, &ECL; uses a simple solution, which is to mark the sections of |
| 185 |
code which are interruptible, and in which it is safe for the handler to run |
| 186 |
arbitrary code. All other regions would be considered "unsafe" and would be |
| 187 |
protected from signals and exceptions.</para> |
| 188 |
|
| 189 |
<para>In principle this "marking" of safe areas can be done using POSIX |
| 190 |
functions such as <function>pthread_sigmask()</function> or |
| 191 |
<function>sigprocmask()</function>. However in practice this is slow, as it |
| 192 |
involves at least a function call, resolving thread-local variables, etc, |
| 193 |
etc, and it will not work in Windows.</para> |
| 194 |
|
| 195 |
<para>Furthermore, sometimes we want signals to be detected but not to be |
| 196 |
immediately processed. For instance, when reading from the terminal we want |
| 197 |
to be able to interrupt the process, but we can not execute the code from |
| 198 |
the handler, since the C function which is used to read from the terminal, |
| 199 |
<function>read()</function>, may have left the input stream in an |
| 200 |
inconsistent, or even locked state.</para> |
| 201 |
|
| 202 |
<para>The approach in &ECL; is more lightweight: we install our own signal |
| 203 |
handler and use a thread-local variable as a flag that determines whether |
| 204 |
the thread is executing interrupt safe code or not. More precisely, if the |
| 205 |
variable <code>ecl_process_env()->disable_interrupts</code> is set, signals |
| 206 |
and exceptions will be postponed and then the information about the signal |
| 207 |
is queued. Otherwise the appropriate code is executed: for instance invoking |
| 208 |
the debugger, jumping to a condition handler, quitting, etc.</para> |
| 209 |
|
| 210 |
<para>Systems that embed &ECL; may wish to deactivate completely these |
| 211 |
signal handlers. This is done using the boot options, |
| 212 |
<varname>ECL_OPT_TRAP_SIGFPE</varname>, |
| 213 |
<varname>ECL_OPT_TRAP_SIGSEGV</varname>, |
| 214 |
<varname>ECL_OPT_TRAP_SIGBUS</varname>, |
| 215 |
<varname>ECL_OPT_TRAP_INTERRUPT_SIGNAL</varname>.</para> |
| 216 |
|
| 217 |
<para>Systems that embed &ECL; and want to allow handling of synchronous |
| 218 |
signals should take care to also trap the associated lisp conditions that |
| 219 |
may arise. This is automatically taken care of by functions such as |
| 220 |
<function>si_safe_eval()</function>, and in all other cases it can be solved |
| 221 |
by enclosing the unsafe code in a <function>CL_CATCH_ALL_BEGIN</function> |
| 222 |
frame (See <xref linkend="ref.embed.cl_catch_all"/>).</para> |
| 223 |
</section> |
| 224 |
</section> |
| 225 |
|
| 226 |
<section xml:id="ext.signals.embedding"> |
| 227 |
<title>Considerations when embedding &ECL;</title> |
| 228 |
|
| 229 |
<para>There are several approaches when handling signals and interrupts in |
| 230 |
a program that uses &ECL;. One is to install your own signal handlers. This |
| 231 |
is perfectly fine, but you should respect the same restrictions as &ECL;. |
| 232 |
Namely, you may not execute arbitrary code from those signal handlers, and |
| 233 |
in particular it will not always be safe to execute Common Lisp code from |
| 234 |
there.</para> |
| 235 |
|
| 236 |
<para>If you want to use your own signal handlers then you should set the |
| 237 |
appropriate options before invoking <function>cl_boot()</function>, as |
| 238 |
explained in <xref linkend="ref.embed.ecl_set_option"/>. Note that in this |
| 239 |
case &ECL; will not always be able to detect floating point exceptions, |
| 240 |
specially if your compiler does not support C99 and the corresponding |
| 241 |
floating point flags.</para> |
| 242 |
|
| 243 |
<para>The other option is to let &ECL; handle signals itself. This would be |
| 244 |
safer when the dominant part of the code is Common Lisp, but you may need |
| 245 |
to protect the code that embeds &ECL; from being interrupted using either |
| 246 |
the macros <xref linkend="ref.embed.ecl_disable_interrupts"/> and <xref |
| 247 |
linkend="ref.embed.ecl_enable_interrupts"/> or the POSIX functions |
| 248 |
<function>pthread_sigmaks</function> and |
| 249 |
<function>sigprocmask</function>.</para> |
| 250 |
</section> |
| 251 |
|
| 252 |
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="ref_signals.xmlf" xpointer="ext.signals.dict"/> |
| 253 |
|
| 254 |
</chapter> |
| 255 |
</book> |
| 256 |
<!-- Keep this comment at the end of the file |
| 257 |
Local variables: |
| 258 |
mode: nxml |
| 259 |
sgml-parent-document: "ecl.xml" |
| 260 |
sgml-indent-step: 1 |
| 261 |
nxml-child-indent: 1 |
| 262 |
nxml-outline-child-indent: 1 |
| 263 |
fill-column: 79 |
| 264 |
End: |
| 265 |
--> |