blob: 0a127911df3cca2defb750d147c22a904a3a1311 [file] [log] [blame]
george.karpenkov29efa6d2017-08-21 23:25:50 +00001//===- FuzzerUtil.h - Internal header for the Fuzzer Utils ------*- C++ -* ===//
2//
chandlerc40284492019-01-19 08:50:56 +00003// 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
george.karpenkov29efa6d2017-08-21 23:25:50 +00006//
7//===----------------------------------------------------------------------===//
8// Util functions.
9//===----------------------------------------------------------------------===//
10
11#ifndef LLVM_FUZZER_UTIL_H
12#define LLVM_FUZZER_UTIL_H
13
metzman40132972019-01-09 21:46:09 +000014#include "FuzzerBuiltins.h"
15#include "FuzzerBuiltinsMsvc.h"
morehousea80f6452017-12-04 19:25:59 +000016#include "FuzzerCommand.h"
metzman40132972019-01-09 21:46:09 +000017#include "FuzzerDefs.h"
george.karpenkov29efa6d2017-08-21 23:25:50 +000018
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
morehousebd67cc22018-05-08 23:45:05 +000044void PrintStackTrace();
45
46void PrintMemoryProfile();
47
george.karpenkov29efa6d2017-08-21 23:25:50 +000048unsigned NumberOfCpuCores();
49
50// Platform specific functions.
51void SetSignalHandler(const FuzzingOptions& Options);
52
53void SleepSeconds(int Seconds);
54
kccda168932019-01-31 00:09:43 +000055bool Mprotect(void *Ptr, size_t Size, bool AllowReadWrite);
56
george.karpenkov29efa6d2017-08-21 23:25:50 +000057unsigned long GetPid();
58
59size_t GetPeakRSSMb();
60
morehousea80f6452017-12-04 19:25:59 +000061int ExecuteCommand(const Command &Cmd);
george.karpenkov29efa6d2017-08-21 23:25:50 +000062
63FILE *OpenProcessPipe(const char *Command, const char *Mode);
64
65const void *SearchMemory(const void *haystack, size_t haystacklen,
66 const void *needle, size_t needlelen);
67
george.karpenkovfbfa45c2017-08-27 23:20:09 +000068std::string CloneArgsWithoutX(const Vector<std::string> &Args,
george.karpenkov29efa6d2017-08-21 23:25:50 +000069 const char *X1, const char *X2);
70
george.karpenkovfbfa45c2017-08-27 23:20:09 +000071inline std::string CloneArgsWithoutX(const Vector<std::string> &Args,
george.karpenkov29efa6d2017-08-21 23:25:50 +000072 const char *X) {
73 return CloneArgsWithoutX(Args, X, X);
74}
75
76inline std::pair<std::string, std::string> SplitBefore(std::string X,
77 std::string S) {
78 auto Pos = S.find(X);
79 if (Pos == std::string::npos)
80 return std::make_pair(S, "");
81 return std::make_pair(S.substr(0, Pos), S.substr(Pos));
82}
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
metzman40132972019-01-09 21:46:09 +000090inline uint32_t Log(uint32_t X) { return 32 - Clz(X) - 1; }
kcce29d7e32017-12-12 23:11:28 +000091
kcce2469202019-01-30 06:15:52 +000092inline 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
george.karpenkov29efa6d2017-08-21 23:25:50 +0000106} // namespace fuzzer
107
108#endif // LLVM_FUZZER_UTIL_H