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