[PATCH] D46858: Signal handling should be signal-safe

JF Bastien via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 14 17:06:28 PDT 2018


jfb created this revision.
jfb added a reviewer: dexonsmith.
Herald added subscribers: llvm-commits, aheejin.

Before this patch, signal handling wasn't signal safe. This leads to real-world
crashes. It used ManagedStatic inside of signals, this can allocate and can lead
to unexpected state when a signal occurs during llvm_shutdown (because
llvm_shutdown destroys the ManagedStatic). It also used cl::opt without custom
backing storage. Some de-allocation was performed as well. Acquiring a lock in a
signal handler is also a great way to deadlock.

We can't just disable signals on llvm_shutdown because the signals might do
useful work during that shutdown. We also can't just disable llvm_shutdown for
programs (instead of library uses of clang) because we'd have to then mark the
pointers as not leaked and make sure all the ManagedStatic uses are OK to leak
and remain so.

Move all of the code to lock-free datastructures instead, and avoid having any
of them in an inconsistent state. I'm not trying to be fancy, I'm not using any
explicit memory order because this code isn't hot. The only purpose of the
atomics is to guarantee that a signal firing on the same or a different thread
doesn't see an inconsistent state and crash. In some cases we might miss some
state (for example, we might fail to delete a temporary file), but that's fine.

Note that I haven't touched any of the backtrace support despite it not
technically being totally signal-safe. When that code is called we know
something bad is up and we don't expect to continue execution, so calling
something that e.g. sets errno is the least of our problems.

A similar patch should be applied to lib/Support/Windows/Signals.inc, but that
can be done separately.

rdar://problem/28010281


Repository:
  rL LLVM

https://reviews.llvm.org/D46858

Files:
  include/llvm/Support/Signals.h
  lib/Support/Signals.cpp
  lib/Support/Unix/Signals.inc
  lib/Support/Windows/Signals.inc

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D46858.146722.patch
Type: text/x-patch
Size: 16863 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180515/94bf40ee/attachment.bin>


More information about the llvm-commits mailing list