Skip to content

Commit b686d8f

Browse files
neildharfacebook-github-bot
authored andcommitted
Allow stripping of file cleanup code
Summary: The linker currently cannot strip the file cleanup code because it is called on fatal errors and signals (when a the LLVM signal handler is installed). To allow it to be stripped, add a layer of indirection through a function pointer, which is only populated by `RemoveFileOnSignal`. This way, the cleanup code is only considered reachable when `RemoveFileOnSignal` is actually used, and it can be stripped otherwise. Reviewed By: tmikov Differential Revision: D53535800 fbshipit-source-id: ae26197a66bc154dd83eb304b4c731eabac10873
1 parent 09eab96 commit b686d8f

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

external/llvh/lib/Support/Unix/Signals.inc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ public:
184184
};
185185
static std::atomic<FileToRemoveList *> FilesToRemove = ATOMIC_VAR_INIT(nullptr);
186186

187+
/// Provide the interrupt function to clean up FilesToRemove if it is populated.
188+
/// We use a nullable static function pointer for this to allow the linker to
189+
/// strip the file removal code if RemoveFileOnSignal is never called.
190+
static std::atomic<InterruptFunctionType> FilesToRemoveInterruptFunc = ATOMIC_VAR_INIT(nullptr);
191+
187192
/// Clean up the list in a signal-friendly manner.
188193
/// Recall that signals can fire during llvm_shutdown. If this occurs we should
189194
/// either clean something up or nothing at all, but we shouldn't crash!
@@ -327,7 +332,8 @@ static RETSIGTYPE SignalHandler(int Sig) {
327332
sigprocmask(SIG_UNBLOCK, &SigMask, nullptr);
328333

329334
{
330-
RemoveFilesToRemove();
335+
if(auto *fn = FilesToRemoveInterruptFunc.load())
336+
fn();
331337

332338
if (std::find(std::begin(IntSigs), std::end(IntSigs), Sig)
333339
!= std::end(IntSigs)) {
@@ -353,7 +359,8 @@ static RETSIGTYPE SignalHandler(int Sig) {
353359
}
354360

355361
void llvh::sys::RunInterruptHandlers() {
356-
RemoveFilesToRemove();
362+
if(auto *fn = FilesToRemoveInterruptFunc.load())
363+
fn();
357364
}
358365

359366
void llvh::sys::SetInterruptFunction(void (*IF)()) {
@@ -367,6 +374,7 @@ bool llvh::sys::RemoveFileOnSignal(StringRef Filename,
367374
// Ensure that cleanup will occur as soon as one file is added.
368375
static ManagedStatic<FilesToRemoveCleanup> FilesToRemoveCleanup;
369376
*FilesToRemoveCleanup;
377+
FilesToRemoveInterruptFunc = RemoveFilesToRemove;
370378
FileToRemoveList::insert(FilesToRemove, Filename.str());
371379
RegisterHandlers();
372380
return false;

0 commit comments

Comments
 (0)