2017-05-18 20:23:05 +03:00
|
|
|
//===- FuzzerUtil.h - Internal header for the Fuzzer Utils ------*- C++ -* ===//
|
|
|
|
//
|
2019-04-18 11:28:29 +03:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-05-18 20:23:05 +03:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Util functions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_FUZZER_UTIL_H
|
|
|
|
#define LLVM_FUZZER_UTIL_H
|
|
|
|
|
2019-04-18 11:28:29 +03:00
|
|
|
#include "FuzzerBuiltins.h"
|
|
|
|
#include "FuzzerBuiltinsMsvc.h"
|
2018-03-29 21:18:36 +03:00
|
|
|
#include "FuzzerCommand.h"
|
2019-04-18 11:28:29 +03:00
|
|
|
#include "FuzzerDefs.h"
|
2017-05-18 20:23:05 +03:00
|
|
|
|
|
|
|
namespace fuzzer {
|
|
|
|
|
|
|
|
void PrintHexArray(const Unit &U, const char *PrintAfter = "");
|
|
|
|
|
|
|
|
void PrintHexArray(const uint8_t *Data, size_t Size,
|
|
|
|
const char *PrintAfter = "");
|
|
|
|
|
|
|
|
void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");
|
|
|
|
|
|
|
|
void PrintASCII(const Unit &U, const char *PrintAfter = "");
|
|
|
|
|
|
|
|
// Changes U to contain only ASCII (isprint+isspace) characters.
|
|
|
|
// Returns true iff U has been changed.
|
|
|
|
bool ToASCII(uint8_t *Data, size_t Size);
|
|
|
|
|
|
|
|
bool IsASCII(const Unit &U);
|
|
|
|
|
|
|
|
bool IsASCII(const uint8_t *Data, size_t Size);
|
|
|
|
|
|
|
|
std::string Base64(const Unit &U);
|
|
|
|
|
|
|
|
void PrintPC(const char *SymbolizedFMT, const char *FallbackFMT, uintptr_t PC);
|
|
|
|
|
|
|
|
std::string DescribePC(const char *SymbolizedFMT, uintptr_t PC);
|
|
|
|
|
2019-04-18 11:28:29 +03:00
|
|
|
void PrintStackTrace();
|
|
|
|
|
|
|
|
void PrintMemoryProfile();
|
|
|
|
|
2017-05-18 20:23:05 +03:00
|
|
|
unsigned NumberOfCpuCores();
|
|
|
|
|
|
|
|
// Platform specific functions.
|
|
|
|
void SetSignalHandler(const FuzzingOptions& Options);
|
|
|
|
|
|
|
|
void SleepSeconds(int Seconds);
|
|
|
|
|
2019-04-18 11:28:29 +03:00
|
|
|
bool Mprotect(void *Ptr, size_t Size, bool AllowReadWrite);
|
|
|
|
|
2017-05-18 20:23:05 +03:00
|
|
|
unsigned long GetPid();
|
|
|
|
|
|
|
|
size_t GetPeakRSSMb();
|
|
|
|
|
2018-03-29 21:18:36 +03:00
|
|
|
int ExecuteCommand(const Command &Cmd);
|
2017-05-18 20:23:05 +03:00
|
|
|
|
|
|
|
FILE *OpenProcessPipe(const char *Command, const char *Mode);
|
|
|
|
|
|
|
|
const void *SearchMemory(const void *haystack, size_t haystacklen,
|
|
|
|
const void *needle, size_t needlelen);
|
|
|
|
|
2018-03-29 21:18:36 +03:00
|
|
|
std::string CloneArgsWithoutX(const Vector<std::string> &Args,
|
2017-05-18 20:23:05 +03:00
|
|
|
const char *X1, const char *X2);
|
|
|
|
|
2018-03-29 21:18:36 +03:00
|
|
|
inline std::string CloneArgsWithoutX(const Vector<std::string> &Args,
|
2017-05-18 20:23:05 +03:00
|
|
|
const char *X) {
|
|
|
|
return CloneArgsWithoutX(Args, X, X);
|
|
|
|
}
|
|
|
|
|
2018-03-29 21:18:36 +03:00
|
|
|
inline std::pair<std::string, std::string> SplitBefore(std::string X,
|
|
|
|
std::string S) {
|
|
|
|
auto Pos = S.find(X);
|
|
|
|
if (Pos == std::string::npos)
|
|
|
|
return std::make_pair(S, "");
|
|
|
|
return std::make_pair(S.substr(0, Pos), S.substr(Pos));
|
|
|
|
}
|
|
|
|
|
2017-05-18 20:23:05 +03:00
|
|
|
std::string DisassembleCmd(const std::string &FileName);
|
|
|
|
|
|
|
|
std::string SearchRegexCmd(const std::string &Regex);
|
|
|
|
|
2018-03-29 21:18:36 +03:00
|
|
|
size_t SimpleFastHash(const uint8_t *Data, size_t Size);
|
|
|
|
|
2019-04-18 11:28:29 +03:00
|
|
|
inline uint32_t Log(uint32_t X) { return 32 - Clz(X) - 1; }
|
|
|
|
|
|
|
|
inline size_t PageSize() { return 4096; }
|
|
|
|
inline uint8_t *RoundUpByPage(uint8_t *P) {
|
|
|
|
uintptr_t X = reinterpret_cast<uintptr_t>(P);
|
|
|
|
size_t Mask = PageSize() - 1;
|
|
|
|
X = (X + Mask) & ~Mask;
|
|
|
|
return reinterpret_cast<uint8_t *>(X);
|
|
|
|
}
|
|
|
|
inline uint8_t *RoundDownByPage(uint8_t *P) {
|
|
|
|
uintptr_t X = reinterpret_cast<uintptr_t>(P);
|
|
|
|
size_t Mask = PageSize() - 1;
|
|
|
|
X = X & ~Mask;
|
|
|
|
return reinterpret_cast<uint8_t *>(X);
|
|
|
|
}
|
2018-03-29 21:18:36 +03:00
|
|
|
|
2017-05-18 20:23:05 +03:00
|
|
|
} // namespace fuzzer
|
|
|
|
|
|
|
|
#endif // LLVM_FUZZER_UTIL_H
|