FuzzerUtil.h revision 360784
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
53void SleepSeconds(int Seconds);
54
55unsigned long GetPid();
56
57size_t GetPeakRSSMb();
58
59int ExecuteCommand(const Command &Cmd);
60
61FILE *OpenProcessPipe(const char *Command, const char *Mode);
62
63const void *SearchMemory(const void *haystack, size_t haystacklen,
64                         const void *needle, size_t needlelen);
65
66std::string CloneArgsWithoutX(const Vector<std::string> &Args,
67                              const char *X1, const char *X2);
68
69inline std::string CloneArgsWithoutX(const Vector<std::string> &Args,
70                                     const char *X) {
71  return CloneArgsWithoutX(Args, X, X);
72}
73
74inline std::pair<std::string, std::string> SplitBefore(std::string X,
75                                                       std::string S) {
76  auto Pos = S.find(X);
77  if (Pos == std::string::npos)
78    return std::make_pair(S, "");
79  return std::make_pair(S.substr(0, Pos), S.substr(Pos));
80}
81
82void DiscardOutput(int Fd);
83
84std::string DisassembleCmd(const std::string &FileName);
85
86std::string SearchRegexCmd(const std::string &Regex);
87
88size_t SimpleFastHash(const uint8_t *Data, size_t Size);
89
90inline uint32_t Log(uint32_t X) { return 32 - Clz(X) - 1; }
91
92inline size_t PageSize() { return 4096; }
93inline uint8_t *RoundUpByPage(uint8_t *P) {
94  uintptr_t X = reinterpret_cast<uintptr_t>(P);
95  size_t Mask = PageSize() - 1;
96  X = (X + Mask) & ~Mask;
97  return reinterpret_cast<uint8_t *>(X);
98}
99inline uint8_t *RoundDownByPage(uint8_t *P) {
100  uintptr_t X = reinterpret_cast<uintptr_t>(P);
101  size_t Mask = PageSize() - 1;
102  X = X & ~Mask;
103  return reinterpret_cast<uint8_t *>(X);
104}
105
106}  // namespace fuzzer
107
108#endif  // LLVM_FUZZER_UTIL_H
109