1 //===- FuzzerUtilPosix.cpp - Misc utils for Posix. ------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 // Misc utils implementation using Posix API.
9 //===----------------------------------------------------------------------===//
10 #include "FuzzerPlatform.h"
11 #if LIBFUZZER_POSIX
12   #include "FuzzerIO.h"
13   #include "FuzzerInternal.h"
14   #include "FuzzerTracePC.h"
15   #include <cassert>
16   #include <chrono>
17   #include <cstring>
18   #include <errno.h>
19   #include <iomanip>
20   #include <signal.h>
21   #include <stdio.h>
22   #include <sys/mman.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 
AlarmHandler(int,siginfo_t *,void *)32 static void AlarmHandler(int, siginfo_t *, void *) {
33 
34   Fuzzer::StaticAlarmCallback();
35 
36 }
37 
38 static void (*upstream_segv_handler)(int, siginfo_t *, void *);
39 
SegvHandler(int sig,siginfo_t * si,void * ucontext)40 static void SegvHandler(int sig, siginfo_t *si, void *ucontext) {
41 
42   assert(si->si_signo == SIGSEGV);
43   if (upstream_segv_handler) return upstream_segv_handler(sig, si, ucontext);
44   Fuzzer::StaticCrashSignalCallback();
45 
46 }
47 
CrashHandler(int,siginfo_t *,void *)48 static void CrashHandler(int, siginfo_t *, void *) {
49 
50   Fuzzer::StaticCrashSignalCallback();
51 
52 }
53 
InterruptHandler(int,siginfo_t *,void *)54 static void InterruptHandler(int, siginfo_t *, void *) {
55 
56   Fuzzer::StaticInterruptCallback();
57 
58 }
59 
GracefulExitHandler(int,siginfo_t *,void *)60 static void GracefulExitHandler(int, siginfo_t *, void *) {
61 
62   Fuzzer::StaticGracefulExitCallback();
63 
64 }
65 
FileSizeExceedHandler(int,siginfo_t *,void *)66 static void FileSizeExceedHandler(int, siginfo_t *, void *) {
67 
68   Fuzzer::StaticFileSizeExceedCallback();
69 
70 }
71 
SetSigaction(int signum,void (* callback)(int,siginfo_t *,void *))72 static void SetSigaction(int signum,
73                          void (*callback)(int, siginfo_t *, void *)) {
74 
75   struct sigaction sigact = {};
76   if (sigaction(signum, nullptr, &sigact)) {
77 
78     Printf("libFuzzer: sigaction failed with %d\n", errno);
79     exit(1);
80 
81   }
82 
83   if (sigact.sa_flags & SA_SIGINFO) {
84 
85     if (sigact.sa_sigaction) {
86 
87       if (signum != SIGSEGV) return;
88       upstream_segv_handler = sigact.sa_sigaction;
89 
90     }
91 
92   } else {
93 
94     if (sigact.sa_handler != SIG_DFL && sigact.sa_handler != SIG_IGN &&
95         sigact.sa_handler != SIG_ERR)
96       return;
97 
98   }
99 
100   sigact = {};
101   sigact.sa_flags = SA_SIGINFO;
102   sigact.sa_sigaction = callback;
103   if (sigaction(signum, &sigact, 0)) {
104 
105     Printf("libFuzzer: sigaction failed with %d\n", errno);
106     exit(1);
107 
108   }
109 
110 }
111 
112 // Return true on success, false otherwise.
ExecuteCommand(const Command & Cmd,std::string * CmdOutput)113 bool ExecuteCommand(const Command &Cmd, std::string *CmdOutput) {
114 
115   FILE *Pipe = popen(Cmd.toString().c_str(), "r");
116   if (!Pipe) return false;
117 
118   if (CmdOutput) {
119 
120     char TmpBuffer[128];
121     while (fgets(TmpBuffer, sizeof(TmpBuffer), Pipe))
122       CmdOutput->append(TmpBuffer);
123 
124   }
125 
126   return pclose(Pipe) == 0;
127 
128 }
129 
SetTimer(int Seconds)130 void SetTimer(int Seconds) {
131 
132   struct itimerval T {
133 
134     {Seconds, 0}, {
135 
136       Seconds, 0
137 
138     }
139 
140   };
141 
142   if (setitimer(ITIMER_REAL, &T, nullptr)) {
143 
144     Printf("libFuzzer: setitimer failed with %d\n", errno);
145     exit(1);
146 
147   }
148 
149   SetSigaction(SIGALRM, AlarmHandler);
150 
151 }
152 
SetSignalHandler(const FuzzingOptions & Options)153 void SetSignalHandler(const FuzzingOptions &Options) {
154 
155   // setitimer is not implemented in emscripten.
156   if (Options.HandleAlrm && Options.UnitTimeoutSec > 0 && !LIBFUZZER_EMSCRIPTEN)
157     SetTimer(Options.UnitTimeoutSec / 2 + 1);
158   if (Options.HandleInt) SetSigaction(SIGINT, InterruptHandler);
159   if (Options.HandleTerm) SetSigaction(SIGTERM, InterruptHandler);
160   if (Options.HandleSegv) SetSigaction(SIGSEGV, SegvHandler);
161   if (Options.HandleBus) SetSigaction(SIGBUS, CrashHandler);
162   if (Options.HandleAbrt) SetSigaction(SIGABRT, CrashHandler);
163   if (Options.HandleIll) SetSigaction(SIGILL, CrashHandler);
164   if (Options.HandleFpe) SetSigaction(SIGFPE, CrashHandler);
165   if (Options.HandleXfsz) SetSigaction(SIGXFSZ, FileSizeExceedHandler);
166   if (Options.HandleUsr1) SetSigaction(SIGUSR1, GracefulExitHandler);
167   if (Options.HandleUsr2) SetSigaction(SIGUSR2, GracefulExitHandler);
168 
169 }
170 
SleepSeconds(int Seconds)171 void SleepSeconds(int Seconds) {
172 
173   sleep(Seconds);  // Use C API to avoid coverage from instrumented libc++.
174 
175 }
176 
GetPid()177 unsigned long GetPid() {
178 
179   return (unsigned long)getpid();
180 
181 }
182 
GetPeakRSSMb()183 size_t GetPeakRSSMb() {
184 
185   struct rusage usage;
186   if (getrusage(RUSAGE_SELF, &usage)) return 0;
187   if (LIBFUZZER_LINUX || LIBFUZZER_FREEBSD || LIBFUZZER_NETBSD ||
188       LIBFUZZER_OPENBSD || LIBFUZZER_EMSCRIPTEN) {
189 
190     // ru_maxrss is in KiB
191     return usage.ru_maxrss >> 10;
192 
193   } else if (LIBFUZZER_APPLE) {
194 
195     // ru_maxrss is in bytes
196     return usage.ru_maxrss >> 20;
197 
198   }
199 
200   assert(0 && "GetPeakRSSMb() is not implemented for your platform");
201   return 0;
202 
203 }
204 
OpenProcessPipe(const char * Command,const char * Mode)205 FILE *OpenProcessPipe(const char *Command, const char *Mode) {
206 
207   return popen(Command, Mode);
208 
209 }
210 
CloseProcessPipe(FILE * F)211 int CloseProcessPipe(FILE *F) {
212 
213   return pclose(F);
214 
215 }
216 
SearchMemory(const void * Data,size_t DataLen,const void * Patt,size_t PattLen)217 const void *SearchMemory(const void *Data, size_t DataLen, const void *Patt,
218                          size_t PattLen) {
219 
220   return memmem(Data, DataLen, Patt, PattLen);
221 
222 }
223 
DisassembleCmd(const std::string & FileName)224 std::string DisassembleCmd(const std::string &FileName) {
225 
226   return "objdump -d " + FileName;
227 
228 }
229 
SearchRegexCmd(const std::string & Regex)230 std::string SearchRegexCmd(const std::string &Regex) {
231 
232   return "grep '" + Regex + "'";
233 
234 }
235 
236 }  // namespace fuzzer
237 
238 #endif  // LIBFUZZER_POSIX
239 
240