Skip to content

Commit 33cc58f

Browse files
authored
[compiler-rt][libFuzzer] Add support for capturing SIGTRAP exits. (#149120)
Swift's FatalError raises a SIGTRAP, which currently causes the fuzzer to exit without writing out the crashing input. rdar://142975522
1 parent a22d010 commit 33cc58f

File tree

7 files changed

+42
-1
lines changed

7 files changed

+42
-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)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2+
// See https://llvm.org/LICENSE.txt for license information.
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
// Simple test for a fuzzer. The fuzzer must find the string "Hi!".
6+
#include <assert.h>
7+
#include <cstddef>
8+
#include <cstdint>
9+
#include <cstdlib>
10+
#include <iostream>
11+
#include <ostream>
12+
#include <signal.h>
13+
14+
static volatile int Sink;
15+
16+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
17+
assert(Data);
18+
if (Size > 0 && Data[0] == 'H') {
19+
Sink = 1;
20+
if (Size > 1 && Data[1] == 'i') {
21+
Sink = 2;
22+
if (Size > 2 && Data[2] == '!') {
23+
std::cout << "BINGO; Found the target, exiting\n" << std::flush;
24+
raise(SIGTRAP);
25+
}
26+
}
27+
}
28+
return 0;
29+
}

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/SigTrapTest.cpp -o %t
2+
3+
RUN: not %run %t 2>&1 | FileCheck %s
4+
CHECK: BINGO
5+
CHECK: ERROR: libFuzzer: deadly signal
6+
7+
RUN: trap "%run %t -handle_trap=0" TRAP

0 commit comments

Comments
 (0)