Path: blob/main/contrib/llvm-project/compiler-rt/lib/fuzzer/FuzzerUtil.h
35262 views
//===- FuzzerUtil.h - Internal header for the Fuzzer Utils ------*- C++ -* ===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7// Util functions.8//===----------------------------------------------------------------------===//910#ifndef LLVM_FUZZER_UTIL_H11#define LLVM_FUZZER_UTIL_H1213#include "FuzzerBuiltins.h"14#include "FuzzerBuiltinsMsvc.h"15#include "FuzzerCommand.h"16#include "FuzzerDefs.h"1718namespace fuzzer {1920void PrintHexArray(const Unit &U, const char *PrintAfter = "");2122void PrintHexArray(const uint8_t *Data, size_t Size,23const char *PrintAfter = "");2425void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");2627void PrintASCII(const Unit &U, const char *PrintAfter = "");2829// Changes U to contain only ASCII (isprint+isspace) characters.30// Returns true iff U has been changed.31bool ToASCII(uint8_t *Data, size_t Size);3233bool IsASCII(const Unit &U);3435bool IsASCII(const uint8_t *Data, size_t Size);3637std::string Base64(const Unit &U);3839void PrintPC(const char *SymbolizedFMT, const char *FallbackFMT, uintptr_t PC);4041std::string DescribePC(const char *SymbolizedFMT, uintptr_t PC);4243void PrintStackTrace();4445void PrintMemoryProfile();4647unsigned NumberOfCpuCores();4849// Platform specific functions.50void SetSignalHandler(const FuzzingOptions& Options);5152void SleepSeconds(int Seconds);5354unsigned long GetPid();5556size_t GetPeakRSSMb();5758int ExecuteCommand(const Command &Cmd);59bool ExecuteCommand(const Command &Cmd, std::string *CmdOutput);6061void SetThreadName(std::thread &thread, const std::string &name);6263// Fuchsia does not have popen/pclose.64FILE *OpenProcessPipe(const char *Command, const char *Mode);65int CloseProcessPipe(FILE *F);6667const void *SearchMemory(const void *haystack, size_t haystacklen,68const void *needle, size_t needlelen);6970std::string CloneArgsWithoutX(const std::vector<std::string> &Args,71const char *X1, const char *X2);7273inline std::string CloneArgsWithoutX(const std::vector<std::string> &Args,74const char *X) {75return CloneArgsWithoutX(Args, X, X);76}7778inline std::pair<std::string, std::string> SplitBefore(std::string X,79std::string S) {80auto Pos = S.find(X);81if (Pos == std::string::npos)82return std::make_pair(S, "");83return std::make_pair(S.substr(0, Pos), S.substr(Pos));84}8586void DiscardOutput(int Fd);8788std::string DisassembleCmd(const std::string &FileName);8990std::string SearchRegexCmd(const std::string &Regex);9192uint64_t SimpleFastHash(const void *Data, size_t Size, uint64_t Initial = 0);9394inline size_t Log(size_t X) {95return static_cast<size_t>((sizeof(unsigned long long) * 8) - Clzll(X) - 1);96}9798size_t PageSize();99100inline uint8_t *RoundUpByPage(uint8_t *P) {101uintptr_t X = reinterpret_cast<uintptr_t>(P);102size_t Mask = PageSize() - 1;103X = (X + Mask) & ~Mask;104return reinterpret_cast<uint8_t *>(X);105}106inline uint8_t *RoundDownByPage(uint8_t *P) {107uintptr_t X = reinterpret_cast<uintptr_t>(P);108size_t Mask = PageSize() - 1;109X = X & ~Mask;110return reinterpret_cast<uint8_t *>(X);111}112113#if __BYTE_ORDER == __LITTLE_ENDIAN114template <typename T> T HostToLE(T X) { return X; }115#else116template <typename T> T HostToLE(T X) { return Bswap(X); }117#endif118119} // namespace fuzzer120121#endif // LLVM_FUZZER_UTIL_H122123124