llvm/compiler-rt/lib/scudo/standalone/common.h

//===-- common.h ------------------------------------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef SCUDO_COMMON_H_
#define SCUDO_COMMON_H_

#include "internal_defs.h"

#include "fuchsia.h"
#include "linux.h"
#include "trusty.h"

#include <stddef.h>
#include <string.h>
#include <unistd.h>

namespace scudo {

template <class Dest, class Source> inline Dest bit_cast(const Source &S) {}

inline constexpr bool isPowerOfTwo(uptr X) {}

inline constexpr uptr roundUp(uptr X, uptr Boundary) {}
inline constexpr uptr roundUpSlow(uptr X, uptr Boundary) {}

inline constexpr uptr roundDown(uptr X, uptr Boundary) {}
inline constexpr uptr roundDownSlow(uptr X, uptr Boundary) {}

inline constexpr bool isAligned(uptr X, uptr Alignment) {}
inline constexpr bool isAlignedSlow(uptr X, uptr Alignment) {}

template <class T> constexpr T Min(T A, T B) {}

template <class T> constexpr T Max(T A, T B) {}

template <class T> void Swap(T &A, T &B) {}

inline uptr getMostSignificantSetBitIndex(uptr X) {}

inline uptr roundUpPowerOfTwo(uptr Size) {}

inline uptr getLeastSignificantSetBitIndex(uptr X) {}

inline uptr getLog2(uptr X) {}

inline u32 getRandomU32(u32 *State) {}

inline u32 getRandomModN(u32 *State, u32 N) {}

template <typename T> inline void shuffle(T *A, u32 N, u32 *RandState) {}

inline void computePercentage(uptr Numerator, uptr Denominator, uptr *Integral,
                              uptr *Fractional) {}

// Platform specific functions.

#if defined(SCUDO_PAGE_SIZE)

inline constexpr uptr getPageSizeCached() { return SCUDO_PAGE_SIZE; }

inline constexpr uptr getPageSizeSlow() { return getPageSizeCached(); }

inline constexpr uptr getPageSizeLogCached() {
  return static_cast<uptr>(__builtin_ctzl(SCUDO_PAGE_SIZE));
}

#else

extern uptr PageSizeCached;
extern uptr PageSizeLogCached;

uptr getPageSizeSlow();

inline uptr getPageSizeCached() {}

inline uptr getPageSizeLogCached() {}

#endif

// Returns 0 if the number of CPUs could not be determined.
u32 getNumberOfCPUs();

const char *getEnv(const char *Name);

u64 getMonotonicTime();
// Gets the time faster but with less accuracy. Can call getMonotonicTime
// if no fast version is available.
u64 getMonotonicTimeFast();

u32 getThreadID();

// Our randomness gathering function is limited to 256 bytes to ensure we get
// as many bytes as requested, and avoid interruptions (on Linux).
constexpr uptr MaxRandomLength =;
bool getRandom(void *Buffer, uptr Length, bool Blocking = false);

// Platform memory mapping functions.

#define MAP_ALLOWNOMEM
#define MAP_NOACCESS
#define MAP_RESIZABLE
#define MAP_MEMTAG
#define MAP_PRECOMMIT

// Our platform memory mapping use is restricted to 3 scenarios:
// - reserve memory at a random address (MAP_NOACCESS);
// - commit memory in a previously reserved space;
// - commit memory at a random address.
// As such, only a subset of parameters combinations is valid, which is checked
// by the function implementation. The Data parameter allows to pass opaque
// platform specific data to the function.
// Returns nullptr on error or dies if MAP_ALLOWNOMEM is not specified.
void *map(void *Addr, uptr Size, const char *Name, uptr Flags = 0,
          MapPlatformData *Data = nullptr);

// Indicates that we are getting rid of the whole mapping, which might have
// further consequences on Data, depending on the platform.
#define UNMAP_ALL

void unmap(void *Addr, uptr Size, uptr Flags = 0,
           MapPlatformData *Data = nullptr);

void setMemoryPermission(uptr Addr, uptr Size, uptr Flags,
                         MapPlatformData *Data = nullptr);

void releasePagesToOS(uptr BaseAddress, uptr Offset, uptr Size,
                      MapPlatformData *Data = nullptr);

// Logging related functions.

void setAbortMessage(const char *Message);

struct BlockInfo {};

enum class Option : u8 {};

enum class ReleaseToOS : u8 {};

constexpr unsigned char PatternFillByte =;

enum FillContentsMode {};

} // namespace scudo

#endif // SCUDO_COMMON_H_