Skip to content

Commit a8d0dd0

Browse files
committed
[compiler-rt][libFuzzer] Add support for capturing SIGTRAP exits.
Swift's FatalError raises a SIGTRAP, which currently causes the fuzzer to exit without writing out the crashing input.
1 parent bd4e7f5 commit a8d0dd0

File tree

7 files changed

+20
-1
lines changed

7 files changed

+20
-1
lines changed

compiler-rt/lib/fuzzer/FuzzerDriver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,7 @@ int FuzzerDriver(int *argc, char ***argv, UserCallback Callback) {
834834
Options.HandleInt = Flags.handle_int;
835835
Options.HandleSegv = Flags.handle_segv;
836836
Options.HandleTerm = Flags.handle_term;
837+
Options.HandleTrap = Flags.handle_trap;
837838
Options.HandleXfsz = Flags.handle_xfsz;
838839
Options.HandleUsr1 = Flags.handle_usr1;
839840
Options.HandleUsr2 = Flags.handle_usr2;

compiler-rt/lib/fuzzer/FuzzerFlags.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ FUZZER_FLAG_INT(handle_ill, 1, "If 1, try to intercept SIGILL.")
152152
FUZZER_FLAG_INT(handle_fpe, 1, "If 1, try to intercept SIGFPE.")
153153
FUZZER_FLAG_INT(handle_int, 1, "If 1, try to intercept SIGINT.")
154154
FUZZER_FLAG_INT(handle_term, 1, "If 1, try to intercept SIGTERM.")
155+
FUZZER_FLAG_INT(handle_trap, 1, "If 1, try to intercept SIGTRAP.")
155156
FUZZER_FLAG_INT(handle_xfsz, 1, "If 1, try to intercept SIGXFSZ.")
156157
FUZZER_FLAG_INT(handle_usr1, 1, "If 1, try to intercept SIGUSR1.")
157158
FUZZER_FLAG_INT(handle_usr2, 1, "If 1, try to intercept SIGUSR2.")

compiler-rt/lib/fuzzer/FuzzerOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ struct FuzzingOptions {
8282
bool HandleInt = false;
8383
bool HandleSegv = false;
8484
bool HandleTerm = false;
85+
bool HandleTrap = false;
8586
bool HandleXfsz = false;
8687
bool HandleUsr1 = false;
8788
bool HandleUsr2 = false;

compiler-rt/lib/fuzzer/FuzzerUtilFuchsia.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ void SetSignalHandler(const FuzzingOptions &Options) {
410410

411411
// Early exit if no crash handler needed.
412412
if (!Options.HandleSegv && !Options.HandleBus && !Options.HandleIll &&
413-
!Options.HandleFpe && !Options.HandleAbrt)
413+
!Options.HandleFpe && !Options.HandleAbrt && !Options.HandleTrap)
414414
return;
415415

416416
// Set up the crash handler and wait until it is ready before proceeding.

compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ void SetSignalHandler(const FuzzingOptions& Options) {
132132
SetSigaction(SIGILL, CrashHandler);
133133
if (Options.HandleFpe)
134134
SetSigaction(SIGFPE, CrashHandler);
135+
if (Options.HandleTrap)
136+
SetSigaction(SIGTRAP, CrashHandler);
135137
if (Options.HandleXfsz)
136138
SetSigaction(SIGXFSZ, FileSizeExceedHandler);
137139
if (Options.HandleUsr1)

compiler-rt/test/fuzzer/SimpleTest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#include <cstdlib>
1010
#include <iostream>
1111
#include <ostream>
12+
#ifdef SIGTRAP_TEST
13+
# include <signal.h>
14+
#endif
1215

1316
static volatile int Sink;
1417

@@ -20,7 +23,11 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
2023
Sink = 2;
2124
if (Size > 2 && Data[2] == '!') {
2225
std::cout << "BINGO; Found the target, exiting\n" << std::flush;
26+
#ifdef SIGTRAP_TEST
27+
raise(SIGTRAP);
28+
#else
2329
exit(0);
30+
#endif
2431
}
2532
}
2633
}

compiler-rt/test/fuzzer/sig-trap.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
RUN: %cpp_compiler %S/SimpleTest.cpp -DSIGTRAP_TEST -o %t-SigTrapTest
2+
3+
RUN: not %run %t-SigTrapTest 2>&1 | FileCheck %s
4+
CHECK: BINGO
5+
CHECK: ERROR: libFuzzer: deadly signal
6+
7+
RUN: trap "%run %t-SigTrapTest -handle_trap=0" TRAP

0 commit comments

Comments
 (0)