Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / lib / fuzzer / FuzzerUtil.h
blob554567e1b8fcb3677b64583480fe3e2e974b102b
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 //===----------------------------------------------------------------------===//
11 #ifndef LLVM_FUZZER_UTIL_H
12 #define LLVM_FUZZER_UTIL_H
14 #include "FuzzerBuiltins.h"
15 #include "FuzzerBuiltinsMsvc.h"
16 #include "FuzzerCommand.h"
17 #include "FuzzerDefs.h"
19 namespace fuzzer {
21 void PrintHexArray(const Unit &U, const char *PrintAfter = "");
23 void PrintHexArray(const uint8_t *Data, size_t Size,
24 const char *PrintAfter = "");
26 void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");
28 void PrintASCII(const Unit &U, const char *PrintAfter = "");
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);
34 bool IsASCII(const Unit &U);
36 bool IsASCII(const uint8_t *Data, size_t Size);
38 std::string Base64(const Unit &U);
40 void PrintPC(const char *SymbolizedFMT, const char *FallbackFMT, uintptr_t PC);
42 std::string DescribePC(const char *SymbolizedFMT, uintptr_t PC);
44 void PrintStackTrace();
46 void PrintMemoryProfile();
48 unsigned NumberOfCpuCores();
50 // Platform specific functions.
51 void SetSignalHandler(const FuzzingOptions& Options);
53 void SleepSeconds(int Seconds);
55 unsigned long GetPid();
57 size_t GetPeakRSSMb();
59 int ExecuteCommand(const Command &Cmd);
60 bool ExecuteCommand(const Command &Cmd, std::string *CmdOutput);
62 void SetThreadName(std::thread &thread, const std::string &name);
64 // Fuchsia does not have popen/pclose.
65 FILE *OpenProcessPipe(const char *Command, const char *Mode);
66 int CloseProcessPipe(FILE *F);
68 const void *SearchMemory(const void *haystack, size_t haystacklen,
69 const void *needle, size_t needlelen);
71 std::string CloneArgsWithoutX(const std::vector<std::string> &Args,
72 const char *X1, const char *X2);
74 inline std::string CloneArgsWithoutX(const std::vector<std::string> &Args,
75 const char *X) {
76 return CloneArgsWithoutX(Args, X, X);
79 inline std::pair<std::string, std::string> SplitBefore(std::string X,
80 std::string S) {
81 auto Pos = S.find(X);
82 if (Pos == std::string::npos)
83 return std::make_pair(S, "");
84 return std::make_pair(S.substr(0, Pos), S.substr(Pos));
87 void DiscardOutput(int Fd);
89 std::string DisassembleCmd(const std::string &FileName);
91 std::string SearchRegexCmd(const std::string &Regex);
93 uint64_t SimpleFastHash(const void *Data, size_t Size, uint64_t Initial = 0);
95 inline size_t Log(size_t X) {
96 return static_cast<size_t>((sizeof(unsigned long long) * 8) - Clzll(X) - 1);
99 size_t PageSize();
101 inline uint8_t *RoundUpByPage(uint8_t *P) {
102 uintptr_t X = reinterpret_cast<uintptr_t>(P);
103 size_t Mask = PageSize() - 1;
104 X = (X + Mask) & ~Mask;
105 return reinterpret_cast<uint8_t *>(X);
107 inline uint8_t *RoundDownByPage(uint8_t *P) {
108 uintptr_t X = reinterpret_cast<uintptr_t>(P);
109 size_t Mask = PageSize() - 1;
110 X = X & ~Mask;
111 return reinterpret_cast<uint8_t *>(X);
114 #if __BYTE_ORDER == __LITTLE_ENDIAN
115 template <typename T> T HostToLE(T X) { return X; }
116 #else
117 template <typename T> T HostToLE(T X) { return Bswap(X); }
118 #endif
120 } // namespace fuzzer
122 #endif // LLVM_FUZZER_UTIL_H