Zachary Turner | 6fa57ad | 2016-12-02 23:02:01 +0000 | [diff] [blame] | 1 | //===- 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 | //===----------------------------------------------------------------------===// |
Zachary Turner | 6fa57ad | 2016-12-02 23:02:01 +0000 | [diff] [blame] | 11 | #include "FuzzerDefs.h" |
| 12 | #if LIBFUZZER_POSIX |
| 13 | #include "FuzzerIO.h" |
| 14 | #include "FuzzerInternal.h" |
| 15 | #include <cassert> |
| 16 | #include <chrono> |
| 17 | #include <cstring> |
| 18 | #include <errno.h> |
| 19 | #include <iomanip> |
| 20 | #include <signal.h> |
| 21 | #include <sstream> |
| 22 | #include <stdio.h> |
| 23 | #include <sys/resource.h> |
| 24 | #include <sys/syscall.h> |
| 25 | #include <sys/time.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <thread> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | namespace fuzzer { |
| 31 | |
| 32 | static void AlarmHandler(int, siginfo_t *, void *) { |
| 33 | Fuzzer::StaticAlarmCallback(); |
| 34 | } |
| 35 | |
| 36 | static void CrashHandler(int, siginfo_t *, void *) { |
| 37 | Fuzzer::StaticCrashSignalCallback(); |
| 38 | } |
| 39 | |
| 40 | static void InterruptHandler(int, siginfo_t *, void *) { |
| 41 | Fuzzer::StaticInterruptCallback(); |
| 42 | } |
| 43 | |
Kostya Serebryany | 4aa0590 | 2017-01-05 22:05:47 +0000 | [diff] [blame^] | 44 | static void FileSizeExceedHandler(int, siginfo_t *, void *) { |
| 45 | Fuzzer::StaticFileSizeExceedCallback(); |
| 46 | } |
| 47 | |
Zachary Turner | 6fa57ad | 2016-12-02 23:02:01 +0000 | [diff] [blame] | 48 | static void SetSigaction(int signum, |
| 49 | void (*callback)(int, siginfo_t *, void *)) { |
| 50 | struct sigaction sigact; |
| 51 | memset(&sigact, 0, sizeof(sigact)); |
| 52 | sigact.sa_sigaction = callback; |
| 53 | if (sigaction(signum, &sigact, 0)) { |
| 54 | Printf("libFuzzer: sigaction failed with %d\n", errno); |
| 55 | exit(1); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | void SetTimer(int Seconds) { |
| 60 | struct itimerval T { |
| 61 | {Seconds, 0}, { Seconds, 0 } |
| 62 | }; |
| 63 | if (setitimer(ITIMER_REAL, &T, nullptr)) { |
| 64 | Printf("libFuzzer: setitimer failed with %d\n", errno); |
| 65 | exit(1); |
| 66 | } |
| 67 | SetSigaction(SIGALRM, AlarmHandler); |
| 68 | } |
| 69 | |
Marcos Pividori | c59b692 | 2016-12-13 17:45:20 +0000 | [diff] [blame] | 70 | void SetSignalHandler(const FuzzingOptions& Options) { |
| 71 | if (Options.UnitTimeoutSec > 0) |
| 72 | SetTimer(Options.UnitTimeoutSec / 2 + 1); |
| 73 | if (Options.HandleInt) |
| 74 | SetSigaction(SIGINT, InterruptHandler); |
| 75 | if (Options.HandleTerm) |
| 76 | SetSigaction(SIGTERM, InterruptHandler); |
| 77 | if (Options.HandleSegv) |
| 78 | SetSigaction(SIGSEGV, CrashHandler); |
| 79 | if (Options.HandleBus) |
| 80 | SetSigaction(SIGBUS, CrashHandler); |
| 81 | if (Options.HandleAbrt) |
| 82 | SetSigaction(SIGABRT, CrashHandler); |
| 83 | if (Options.HandleIll) |
| 84 | SetSigaction(SIGILL, CrashHandler); |
| 85 | if (Options.HandleFpe) |
| 86 | SetSigaction(SIGFPE, CrashHandler); |
Kostya Serebryany | 4aa0590 | 2017-01-05 22:05:47 +0000 | [diff] [blame^] | 87 | if (Options.HandleXfsz) |
| 88 | SetSigaction(SIGXFSZ, FileSizeExceedHandler); |
Marcos Pividori | c59b692 | 2016-12-13 17:45:20 +0000 | [diff] [blame] | 89 | } |
Zachary Turner | 6fa57ad | 2016-12-02 23:02:01 +0000 | [diff] [blame] | 90 | |
| 91 | void SleepSeconds(int Seconds) { |
| 92 | sleep(Seconds); // Use C API to avoid coverage from instrumented libc++. |
| 93 | } |
| 94 | |
Marcos Pividori | 463f8bd | 2016-12-13 17:45:44 +0000 | [diff] [blame] | 95 | unsigned long GetPid() { return (unsigned long)getpid(); } |
Zachary Turner | 6fa57ad | 2016-12-02 23:02:01 +0000 | [diff] [blame] | 96 | |
| 97 | size_t GetPeakRSSMb() { |
| 98 | struct rusage usage; |
| 99 | if (getrusage(RUSAGE_SELF, &usage)) |
| 100 | return 0; |
| 101 | if (LIBFUZZER_LINUX) { |
| 102 | // ru_maxrss is in KiB |
| 103 | return usage.ru_maxrss >> 10; |
| 104 | } else if (LIBFUZZER_APPLE) { |
| 105 | // ru_maxrss is in bytes |
| 106 | return usage.ru_maxrss >> 20; |
| 107 | } |
| 108 | assert(0 && "GetPeakRSSMb() is not implemented for your platform"); |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | FILE *OpenProcessPipe(const char *Command, const char *Mode) { |
| 113 | return popen(Command, Mode); |
| 114 | } |
| 115 | |
| 116 | const void *SearchMemory(const void *Data, size_t DataLen, const void *Patt, |
| 117 | size_t PattLen) { |
| 118 | return memmem(Data, DataLen, Patt, PattLen); |
| 119 | } |
| 120 | |
Marcos Pividori | 178fe58 | 2016-12-13 17:46:11 +0000 | [diff] [blame] | 121 | } // namespace fuzzer |
| 122 | |
Zachary Turner | 6fa57ad | 2016-12-02 23:02:01 +0000 | [diff] [blame] | 123 | #endif // LIBFUZZER_POSIX |