1 //===- FuzzerUtil.h - Internal header for the Fuzzer Utils ------*- C++ -* ===//
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 // Util functions.
10 //===----------------------------------------------------------------------===//
11
12 #ifndef LLVM_FUZZER_UTIL_H
13 #define LLVM_FUZZER_UTIL_H
14
15 #include "FuzzerDefs.h"
16 #include "FuzzerCommand.h"
17
18 namespace fuzzer {
19
20 void PrintHexArray(const Unit &U, const char *PrintAfter = "");
21
22 void PrintHexArray(const uint8_t *Data, size_t Size,
23 const char *PrintAfter = "");
24
25 void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");
26
27 void PrintASCII(const Unit &U, const char *PrintAfter = "");
28
29 // Changes U to contain only ASCII (isprint+isspace) characters.
30 // Returns true iff U has been changed.
31 bool ToASCII(uint8_t *Data, size_t Size);
32
33 bool IsASCII(const Unit &U);
34
35 bool IsASCII(const uint8_t *Data, size_t Size);
36
37 std::string Base64(const Unit &U);
38
39 void PrintPC(const char *SymbolizedFMT, const char *FallbackFMT, uintptr_t PC);
40
41 std::string DescribePC(const char *SymbolizedFMT, uintptr_t PC);
42
43 void PrintStackTrace();
44
45 void PrintMemoryProfile();
46
47 unsigned NumberOfCpuCores();
48
49 // Platform specific functions.
50 void SetSignalHandler(const FuzzingOptions& Options);
51
52 void SleepSeconds(int Seconds);
53
54 unsigned long GetPid();
55
56 size_t GetPeakRSSMb();
57
58 int ExecuteCommand(const Command &Cmd);
59
60 FILE *OpenProcessPipe(const char *Command, const char *Mode);
61
62 const void *SearchMemory(const void *haystack, size_t haystacklen,
63 const void *needle, size_t needlelen);
64
65 std::string CloneArgsWithoutX(const Vector<std::string> &Args,
66 const char *X1, const char *X2);
67
CloneArgsWithoutX(const Vector<std::string> & Args,const char * X)68 inline std::string CloneArgsWithoutX(const Vector<std::string> &Args,
69 const char *X) {
70 return CloneArgsWithoutX(Args, X, X);
71 }
72
SplitBefore(std::string X,std::string S)73 inline std::pair<std::string, std::string> SplitBefore(std::string X,
74 std::string S) {
75 auto Pos = S.find(X);
76 if (Pos == std::string::npos)
77 return std::make_pair(S, "");
78 return std::make_pair(S.substr(0, Pos), S.substr(Pos));
79 }
80
81 std::string DisassembleCmd(const std::string &FileName);
82
83 std::string SearchRegexCmd(const std::string &Regex);
84
85 size_t SimpleFastHash(const uint8_t *Data, size_t Size);
86
Log(uint32_t X)87 inline uint32_t Log(uint32_t X) { return 32 - __builtin_clz(X) - 1; }
88
89 } // namespace fuzzer
90
91 #endif // LLVM_FUZZER_UTIL_H
92