blob: ad1e3044477a537f35eeacf4ac353d473fdd2cf0 [file] [log] [blame]
Zachary Turner6fa57ad2016-12-02 23:02:01 +00001//===- FuzzerUtilPosix.cpp - Misc utils for Posix. ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9// Misc utils implementation using Posix API.
10//===----------------------------------------------------------------------===//
11
12#include "FuzzerDefs.h"
13#if LIBFUZZER_POSIX
14#include "FuzzerIO.h"
15#include "FuzzerInternal.h"
16#include <cassert>
17#include <chrono>
18#include <cstring>
19#include <errno.h>
20#include <iomanip>
21#include <signal.h>
22#include <sstream>
23#include <stdio.h>
24#include <sys/resource.h>
25#include <sys/syscall.h>
26#include <sys/time.h>
27#include <sys/types.h>
28#include <thread>
29#include <unistd.h>
30
31namespace fuzzer {
32
33static void AlarmHandler(int, siginfo_t *, void *) {
34 Fuzzer::StaticAlarmCallback();
35}
36
37static void CrashHandler(int, siginfo_t *, void *) {
38 Fuzzer::StaticCrashSignalCallback();
39}
40
41static void InterruptHandler(int, siginfo_t *, void *) {
42 Fuzzer::StaticInterruptCallback();
43}
44
45static void SetSigaction(int signum,
46 void (*callback)(int, siginfo_t *, void *)) {
47 struct sigaction sigact;
48 memset(&sigact, 0, sizeof(sigact));
49 sigact.sa_sigaction = callback;
50 if (sigaction(signum, &sigact, 0)) {
51 Printf("libFuzzer: sigaction failed with %d\n", errno);
52 exit(1);
53 }
54}
55
56void SetTimer(int Seconds) {
57 struct itimerval T {
58 {Seconds, 0}, { Seconds, 0 }
59 };
60 if (setitimer(ITIMER_REAL, &T, nullptr)) {
61 Printf("libFuzzer: setitimer failed with %d\n", errno);
62 exit(1);
63 }
64 SetSigaction(SIGALRM, AlarmHandler);
65}
66
67void SetSigSegvHandler() { SetSigaction(SIGSEGV, CrashHandler); }
68void SetSigBusHandler() { SetSigaction(SIGBUS, CrashHandler); }
69void SetSigAbrtHandler() { SetSigaction(SIGABRT, CrashHandler); }
70void SetSigIllHandler() { SetSigaction(SIGILL, CrashHandler); }
71void SetSigFpeHandler() { SetSigaction(SIGFPE, CrashHandler); }
72void SetSigIntHandler() { SetSigaction(SIGINT, InterruptHandler); }
73void SetSigTermHandler() { SetSigaction(SIGTERM, InterruptHandler); }
74
75void SleepSeconds(int Seconds) {
76 sleep(Seconds); // Use C API to avoid coverage from instrumented libc++.
77}
78
79int GetPid() { return getpid(); }
80
81size_t GetPeakRSSMb() {
82 struct rusage usage;
83 if (getrusage(RUSAGE_SELF, &usage))
84 return 0;
85 if (LIBFUZZER_LINUX) {
86 // ru_maxrss is in KiB
87 return usage.ru_maxrss >> 10;
88 } else if (LIBFUZZER_APPLE) {
89 // ru_maxrss is in bytes
90 return usage.ru_maxrss >> 20;
91 }
92 assert(0 && "GetPeakRSSMb() is not implemented for your platform");
93 return 0;
94}
95
96FILE *OpenProcessPipe(const char *Command, const char *Mode) {
97 return popen(Command, Mode);
98}
99
100const void *SearchMemory(const void *Data, size_t DataLen, const void *Patt,
101 size_t PattLen) {
102 return memmem(Data, DataLen, Patt, PattLen);
103}
104
105} // namespace fuzzer
106#endif // LIBFUZZER_POSIX