1 //===- FuzzerUtil.h - Internal header for the Fuzzer Utils ------*- C++ -* ===//
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 // Util functions.
9 //===----------------------------------------------------------------------===//
10
11 #ifndef LLVM_FUZZER_UTIL_H
12 #define LLVM_FUZZER_UTIL_H
13
14 #include "FuzzerBuiltins.h"
15 #include "FuzzerBuiltinsMsvc.h"
16 #include "FuzzerCommand.h"
17 #include "FuzzerDefs.h"
18
19 namespace fuzzer {
20
21 void PrintHexArray(const Unit &U, const char *PrintAfter = "");
22
23 void PrintHexArray(const uint8_t *Data, size_t Size,
24 const char *PrintAfter = "");
25
26 void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");
27
28 void PrintASCII(const Unit &U, const char *PrintAfter = "");
29
30 // Changes U to contain only ASCII (isprint+isspace) characters.
31 // Returns true iff U has been changed.
32 bool ToASCII(uint8_t *Data, size_t Size);
33
34 bool IsASCII(const Unit &U);
35
36 bool IsASCII(const uint8_t *Data, size_t Size);
37
38 std::string Base64(const Unit &U);
39
40 void PrintPC(const char *SymbolizedFMT, const char *FallbackFMT, uintptr_t PC);
41
42 std::string DescribePC(const char *SymbolizedFMT, uintptr_t PC);
43
44 void PrintStackTrace();
45
46 void PrintMemoryProfile();
47
48 unsigned NumberOfCpuCores();
49
50 // Platform specific functions.
51 void SetSignalHandler(const FuzzingOptions& Options);
52
53 void SleepSeconds(int Seconds);
54
55 unsigned long GetPid();
56
57 size_t GetPeakRSSMb();
58
59 int ExecuteCommand(const Command &Cmd);
60 bool ExecuteCommand(const Command &Cmd, std::string *CmdOutput);
61
62 // Fuchsia does not have popen/pclose.
63 FILE *OpenProcessPipe(const char *Command, const char *Mode);
64 int CloseProcessPipe(FILE *F);
65
66 const void *SearchMemory(const void *haystack, size_t haystacklen,
67 const void *needle, size_t needlelen);
68
69 std::string CloneArgsWithoutX(const std::vector<std::string> &Args,
70 const char *X1, const char *X2);
71
CloneArgsWithoutX(const std::vector<std::string> & Args,const char * X)72 inline std::string CloneArgsWithoutX(const std::vector<std::string> &Args,
73 const char *X) {
74 return CloneArgsWithoutX(Args, X, X);
75 }
76
SplitBefore(std::string X,std::string S)77 inline std::pair<std::string, std::string> SplitBefore(std::string X,
78 std::string S) {
79 auto Pos = S.find(X);
80 if (Pos == std::string::npos)
81 return std::make_pair(S, "");
82 return std::make_pair(S.substr(0, Pos), S.substr(Pos));
83 }
84
85 void DiscardOutput(int Fd);
86
87 std::string DisassembleCmd(const std::string &FileName);
88
89 std::string SearchRegexCmd(const std::string &Regex);
90
91 uint64_t SimpleFastHash(const void *Data, size_t Size, uint64_t Initial = 0);
92
Log(size_t X)93 inline size_t Log(size_t X) {
94 return static_cast<size_t>((sizeof(unsigned long long) * 8) - Clzll(X) - 1);
95 }
96
PageSize()97 inline size_t PageSize() { return 4096; }
RoundUpByPage(uint8_t * P)98 inline uint8_t *RoundUpByPage(uint8_t *P) {
99 uintptr_t X = reinterpret_cast<uintptr_t>(P);
100 size_t Mask = PageSize() - 1;
101 X = (X + Mask) & ~Mask;
102 return reinterpret_cast<uint8_t *>(X);
103 }
RoundDownByPage(uint8_t * P)104 inline uint8_t *RoundDownByPage(uint8_t *P) {
105 uintptr_t X = reinterpret_cast<uintptr_t>(P);
106 size_t Mask = PageSize() - 1;
107 X = X & ~Mask;
108 return reinterpret_cast<uint8_t *>(X);
109 }
110
111 #if __BYTE_ORDER == __LITTLE_ENDIAN
HostToLE(T X)112 template <typename T> T HostToLE(T X) { return X; }
113 #else
HostToLE(T X)114 template <typename T> T HostToLE(T X) { return Bswap(X); }
115 #endif
116
117 } // namespace fuzzer
118
119 #endif // LLVM_FUZZER_UTIL_H
120