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
19namespace fuzzer {
20
21void PrintHexArray(const Unit &U, const char *PrintAfter = "");
22
23void PrintHexArray(const uint8_t *Data, size_t Size,
24 const char *PrintAfter = "");
25
26void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");
27
28void 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.
32bool ToASCII(uint8_t *Data, size_t Size);
33
34bool IsASCII(const Unit &U);
35
36bool IsASCII(const uint8_t *Data, size_t Size);
37
38std::string Base64(const Unit &U);
39
40void PrintPC(const char *SymbolizedFMT, const char *FallbackFMT, uintptr_t PC);
41
42std::string DescribePC(const char *SymbolizedFMT, uintptr_t PC);
43
44void PrintStackTrace();
45
46void PrintMemoryProfile();
47
48unsigned NumberOfCpuCores();
49
50// Platform specific functions.
51void SetSignalHandler(const FuzzingOptions& Options);
52
53// Platform-specific one-time initialization, called early in FuzzerDriver.
54void PlatformInit();
55
56void SleepSeconds(int Seconds);
57
58unsigned long GetPid();
59
60size_t GetPeakRSSMb();
61
62int ExecuteCommand(const Command &Cmd);
63bool ExecuteCommand(const Command &Cmd, std::string *CmdOutput);
64
65void SetThreadName(std::thread &thread, const std::string &name);
66
67// Fuchsia does not have popen/pclose.
68FILE *OpenProcessPipe(const char *Command, const char *Mode);
69int CloseProcessPipe(FILE *F);
70
71const void *SearchMemory(const void *haystack, size_t haystacklen,
72 const void *needle, size_t needlelen);
73
74std::string CloneArgsWithoutX(const std::vector<std::string> &Args,
75 const char *X1, const char *X2);
76
77inline std::string CloneArgsWithoutX(const std::vector<std::string> &Args,
78 const char *X) {
79 return CloneArgsWithoutX(Args, X1: X, X2: X);
80}
81
82inline std::pair<std::string, std::string> SplitBefore(std::string X,
83 std::string S) {
84 auto Pos = S.find(str: X);
85 if (Pos == std::string::npos)
86 return std::make_pair(t1&: S, t2: "");
87 return std::make_pair(t1: S.substr(pos: 0, n: Pos), t2: S.substr(pos: Pos));
88}
89
90void DiscardOutput(int Fd);
91
92std::string DisassembleCmd(const std::string &FileName);
93
94std::string SearchRegexCmd(const std::string &Regex);
95
96uint64_t SimpleFastHash(const void *Data, size_t Size, uint64_t Initial = 0);
97
98inline size_t Log(size_t X) {
99 return static_cast<size_t>((sizeof(unsigned long long) * 8) - Clzll(X) - 1);
100}
101
102size_t PageSize();
103
104inline uint8_t *RoundUpByPage(uint8_t *P) {
105 uintptr_t X = reinterpret_cast<uintptr_t>(P);
106 size_t Mask = PageSize() - 1;
107 X = (X + Mask) & ~Mask;
108 return reinterpret_cast<uint8_t *>(X);
109}
110inline uint8_t *RoundDownByPage(uint8_t *P) {
111 uintptr_t X = reinterpret_cast<uintptr_t>(P);
112 size_t Mask = PageSize() - 1;
113 X = X & ~Mask;
114 return reinterpret_cast<uint8_t *>(X);
115}
116
117#if __BYTE_ORDER == __LITTLE_ENDIAN
118template <typename T> T HostToLE(T X) { return X; }
119#else
120template <typename T> T HostToLE(T X) { return Bswap(X); }
121#endif
122
123} // namespace fuzzer
124
125#endif // LLVM_FUZZER_UTIL_H
126