blob: 74fa5bc3107e181b548e6a3722a8b4d8f346212a [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
55unsigned long GetPid();
56
57size_t GetPeakRSSMb();
58
morehousea80f6452017-12-04 19:25:59 +000059int ExecuteCommand(const Command &Cmd);
george.karpenkov29efa6d2017-08-21 23:25:50 +000060
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
george.karpenkovfbfa45c2017-08-27 23:20:09 +000066std::string CloneArgsWithoutX(const Vector<std::string> &Args,
george.karpenkov29efa6d2017-08-21 23:25:50 +000067 const char *X1, const char *X2);
68
george.karpenkovfbfa45c2017-08-27 23:20:09 +000069inline std::string CloneArgsWithoutX(const Vector<std::string> &Args,
george.karpenkov29efa6d2017-08-21 23:25:50 +000070 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
82std::string DisassembleCmd(const std::string &FileName);
83
84std::string SearchRegexCmd(const std::string &Regex);
85
86size_t SimpleFastHash(const uint8_t *Data, size_t Size);
87
metzman40132972019-01-09 21:46:09 +000088inline uint32_t Log(uint32_t X) { return 32 - Clz(X) - 1; }
kcce29d7e32017-12-12 23:11:28 +000089
george.karpenkov29efa6d2017-08-21 23:25:50 +000090} // namespace fuzzer
91
92#endif // LLVM_FUZZER_UTIL_H