23 #include <sys/resource.h>
24 #include <sys/syscall.h>
26 #include <sys/types.h>
32 static void AlarmHandler(
int, siginfo_t *,
void *) {
40 static void InterruptHandler(
int, siginfo_t *,
void *) {
44 static void FileSizeExceedHandler(
int, siginfo_t *,
void *) {
49 void (*callback)(
int, siginfo_t *,
void *)) {
50 struct sigaction sigact = {};
51 if (sigaction(signum,
nullptr, &sigact)) {
52 Printf(
"libFuzzer: sigaction failed with %d\n", errno);
55 if (sigact.sa_flags & SA_SIGINFO) {
56 if (sigact.sa_sigaction)
59 if (sigact.sa_handler != SIG_DFL && sigact.sa_handler != SIG_IGN &&
60 sigact.sa_handler != SIG_ERR)
65 sigact.sa_sigaction = callback;
66 if (sigaction(signum, &sigact, 0)) {
67 Printf(
"libFuzzer: sigaction failed with %d\n", errno);
72 void SetTimer(
int Seconds) {
74 {Seconds, 0}, { Seconds, 0 }
76 if (setitimer(ITIMER_REAL, &
T,
nullptr)) {
77 Printf(
"libFuzzer: setitimer failed with %d\n", errno);
84 if (Options.UnitTimeoutSec > 0)
85 SetTimer(Options.UnitTimeoutSec / 2 + 1);
86 if (Options.HandleInt)
88 if (Options.HandleTerm)
90 if (Options.HandleSegv)
92 if (Options.HandleBus)
94 if (Options.HandleAbrt)
96 if (Options.HandleIll)
98 if (Options.HandleFpe)
100 if (Options.HandleXfsz)
108 unsigned long GetPid() {
return (
unsigned long)getpid(); }
112 if (getrusage(RUSAGE_SELF, &usage))
114 if (LIBFUZZER_LINUX) {
116 return usage.ru_maxrss >> 10;
117 }
else if (LIBFUZZER_APPLE) {
119 return usage.ru_maxrss >> 20;
121 assert(0 &&
"GetPeakRSSMb() is not implemented for your platform");
126 return popen(Command, Mode);
129 const void *
SearchMemory(
const void *
Data,
size_t DataLen,
const void *Patt,
131 return memmem(Data, DataLen, Patt, PattLen);
135 return "objdump -d " + FileName;
139 return "grep '" + Regex +
"'";
144 #endif // LIBFUZZER_POSIX
static void SetSigaction(int signum, void(*callback)(int, siginfo_t *, void *))
void SleepSeconds(int Seconds)
static void StaticFileSizeExceedCallback()
std::string SearchRegexCmd(const std::string &Regex)
FILE * OpenProcessPipe(const char *Command, const char *Mode)
void SetSignalHandler(const FuzzingOptions &Options)
std::string DisassembleCmd(const std::string &FileName)
const void * SearchMemory(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen)
static void CrashHandler(void *)
CrashHandler - This callback is run if a fatal signal is delivered to the process, it prints the pretty stack trace.
static void StaticAlarmCallback()
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static void StaticInterruptCallback()
Printf(" # Score: %d, Used: %d\n", Scores[i], Usages[i])
static void StaticCrashSignalCallback()