Skip to content

[scudo] Refactor allocator config to support optional flags #81805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler-rt/lib/scudo/standalone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ endif()
set(SCUDO_HEADERS
allocator_common.h
allocator_config.h
allocator_config_wrapper.h
atomic_helpers.h
bytemap.h
checksum.h
Expand Down
124 changes: 124 additions & 0 deletions compiler-rt/lib/scudo/standalone/allocator_config.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
//===-- allocator_config.def ------------------------------------*- 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
//
//===----------------------------------------------------------------------===//
//
// This file defines all the flags and types supported in Scudo. For optional
// flags and types, only explicitly define them when interested (i.e., unused
// optional flags or types can be skipped).

#ifndef BASE_REQUIRED_TEMPLATE_TYPE
#define BASE_REQUIRED_TEMPLATE_TYPE(...)
#endif
#ifndef BASE_OPTIONAL
#define BASE_OPTIONAL(...)
#endif
#ifndef PRIMARY_REQUIRED_TYPE
#define PRIMARY_REQUIRED_TYPE(...)
#endif
#ifndef PRIMARY_REQUIRED
#define PRIMARY_REQUIRED(...)
#endif
#ifndef PRIMARY_OPTIONAL
#define PRIMARY_OPTIONAL(...)
#endif
#ifndef PRIMARY_OPTIONAL_TYPE
#define PRIMARY_OPTIONAL_TYPE(...)
#endif
#ifndef SECONDARY_REQUIRED_TEMPLATE_TYPE
#define SECONDARY_REQUIRED_TEMPLATE_TYPE(...)
#endif
#ifndef SECONDARY_CACHE_OPTIONAL
#define SECONDARY_CACHE_OPTIONAL(...)
#endif

// BASE_REQUIRED_TEMPLATE_TYPE(NAME)
//
// Thread-Specific Data Registry used, shared or exclusive.
BASE_REQUIRED_TEMPLATE_TYPE(TSDRegistryT)

// Defines the type of Primary allocator to use.
BASE_REQUIRED_TEMPLATE_TYPE(PrimaryT)

// Defines the type of Secondary allocator to use.
BASE_REQUIRED_TEMPLATE_TYPE(SecondaryT)

// BASE_OPTIONAL(TYPE, NAME, DEFAULT)
//
// Indicates possible support for Memory Tagging.
BASE_OPTIONAL(const bool, MaySupportMemoryTagging, false)

// PRIMARY_REQUIRED_TYPE(NAME)
//
// SizeClassMap to use with the Primary.
PRIMARY_REQUIRED_TYPE(SizeClassMap)

// Defines the type and scale of a compact pointer. A compact pointer can
// be understood as the offset of a pointer within the region it belongs
// to, in increments of a power-of-2 scale. See `CompactPtrScale` also.
PRIMARY_REQUIRED_TYPE(CompactPtrT)

// PRIMARY_REQUIRED(TYPE, NAME)
//
// The scale of a compact pointer. E.g., Ptr = Base + (CompactPtr << Scale).
PRIMARY_REQUIRED(const uptr, CompactPtrScale)

// Log2 of the size of a size class region, as used by the Primary.
PRIMARY_REQUIRED(const uptr, RegionSizeLog)

// Conceptually, a region will be divided into groups based on the address
// range. Each allocation consumes blocks in the same group until exhaustion
// then it pops out blocks in a new group. Therefore, `GroupSizeLog` is always
// smaller or equal to `RegionSizeLog`. Note that `GroupSizeLog` needs to be
// equal to `RegionSizeLog` for SizeClassAllocator32 because of certain
// constraints.
PRIMARY_REQUIRED(const uptr, GroupSizeLog)

// Call map for user memory with at least this size. Only used with primary64.
PRIMARY_REQUIRED(const uptr, MapSizeIncrement)

// Defines the minimal & maximal release interval that can be set.
PRIMARY_REQUIRED(const s32, MinReleaseToOsIntervalMs)
PRIMARY_REQUIRED(const s32, MaxReleaseToOsIntervalMs)

// PRIMARY_OPTIONAL(TYPE, NAME, DEFAULT)
//
// Indicates support for offsetting the start of a region by a random number of
// pages. Only used with primary64.
PRIMARY_OPTIONAL(const bool, EnableRandomOffset, false)

// PRIMARY_OPTIONAL_TYPE(NAME, DEFAULT)
//
// Use condition variable to shorten the waiting time of refillment of
// freelist. Note that this depends on the implementation of condition
// variable on each platform and the performance may vary so that it does not
// guarantee a performance benefit.
PRIMARY_OPTIONAL_TYPE(ConditionVariableT, ConditionVariableDummy)

// SECONDARY_REQUIRED_TEMPLATE_TYPE(NAME)
//
// Defines the type of Secondary Cache to use.
SECONDARY_REQUIRED_TEMPLATE_TYPE(CacheT)

// SECONDARY_CACHE_OPTIONAL(TYPE, NAME, DEFAULT)
//
// Defines the type of cache used by the Secondary. Some additional
// configuration entries can be necessary depending on the Cache.
SECONDARY_CACHE_OPTIONAL(const u32, EntriesArraySize, 0)
SECONDARY_CACHE_OPTIONAL(const u32, QuarantineSize, 0)
SECONDARY_CACHE_OPTIONAL(const u32, DefaultMaxEntriesCount, 0)
SECONDARY_CACHE_OPTIONAL(const u32, DefaultMaxEntrySize, 0)
SECONDARY_CACHE_OPTIONAL(const s32, MinReleaseToOsIntervalMs, INT32_MIN)
SECONDARY_CACHE_OPTIONAL(const s32, MaxReleaseToOsIntervalMs, INT32_MAX)

#undef SECONDARY_CACHE_OPTIONAL
#undef SECONDARY_REQUIRED_TEMPLATE_TYPE
#undef PRIMARY_OPTIONAL_TYPE
#undef PRIMARY_OPTIONAL
#undef PRIMARY_REQUIRED
#undef PRIMARY_REQUIRED_TYPE
#undef BASE_OPTIONAL
#undef BASE_REQUIRED_TEMPLATE_TYPE
78 changes: 4 additions & 74 deletions compiler-rt/lib/scudo/standalone/allocator_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,80 +38,10 @@

namespace scudo {

// The combined allocator uses a structure as a template argument that
// specifies the configuration options for the various subcomponents of the
// allocator.
//
// struct ExampleConfig {
// // Indicates possible support for Memory Tagging.
// static const bool MaySupportMemoryTagging = false;
//
// // Thread-Specific Data Registry used, shared or exclusive.
// template <class A> using TSDRegistryT = TSDRegistrySharedT<A, 8U, 4U>;
//
// struct Primary {
// // SizeClassMap to use with the Primary.
// using SizeClassMap = DefaultSizeClassMap;
//
// // Log2 of the size of a size class region, as used by the Primary.
// static const uptr RegionSizeLog = 30U;
//
// // Log2 of the size of block group, as used by the Primary. Each group
// // contains a range of memory addresses, blocks in the range will belong
// // to the same group. In general, single region may have 1 or 2MB group
// // size. Multiple regions will have the group size equal to the region
// // size because the region size is usually smaller than 1 MB.
// // Smaller value gives fine-grained control of memory usage but the
// // trade-off is that it may take longer time of deallocation.
// static const uptr GroupSizeLog = 20U;
//
// // Defines the type and scale of a compact pointer. A compact pointer can
// // be understood as the offset of a pointer within the region it belongs
// // to, in increments of a power-of-2 scale.
// // eg: Ptr = Base + (CompactPtr << Scale).
// typedef u32 CompactPtrT;
// static const uptr CompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
//
// // Indicates support for offsetting the start of a region by
// // a random number of pages. Only used with primary64.
// static const bool EnableRandomOffset = true;
//
// // Call map for user memory with at least this size. Only used with
// // primary64.
// static const uptr MapSizeIncrement = 1UL << 18;
//
// // Defines the minimal & maximal release interval that can be set.
// static const s32 MinReleaseToOsIntervalMs = INT32_MIN;
// static const s32 MaxReleaseToOsIntervalMs = INT32_MAX;
//
// // Use condition variable to shorten the waiting time of refillment of
// // freelist. Note that this depends on the implementation of condition
// // variable on each platform and the performance may vary so that it
// // doesn't guarantee a performance benefit.
// // Note that both variables have to be defined to enable it.
// static const bool UseConditionVariable = true;
// using ConditionVariableT = ConditionVariableLinux;
// };
// // Defines the type of Primary allocator to use.
// template <typename Config> using PrimaryT = SizeClassAllocator64<Config>;
//
// // Defines the type of cache used by the Secondary. Some additional
// // configuration entries can be necessary depending on the Cache.
// struct Secondary {
// struct Cache {
// static const u32 EntriesArraySize = 32U;
// static const u32 QuarantineSize = 0U;
// static const u32 DefaultMaxEntriesCount = 32U;
// static const uptr DefaultMaxEntrySize = 1UL << 19;
// static const s32 MinReleaseToOsIntervalMs = INT32_MIN;
// static const s32 MaxReleaseToOsIntervalMs = INT32_MAX;
// };
// // Defines the type of Secondary Cache to use.
// template <typename Config> using CacheT = MapAllocatorCache<Config>;
// };
// // Defines the type of Secondary allocator to use.
// template <typename Config> using SecondaryT = MapAllocator<Config>;
// };
// Scudo uses a structure as a template argument that specifies the
// configuration options for the various subcomponents of the allocator. See the
// following configs as examples and check `allocator_config.def` for all the
// available options.

#ifndef SCUDO_USE_CUSTOM_CONFIG

Expand Down
135 changes: 135 additions & 0 deletions compiler-rt/lib/scudo/standalone/allocator_config_wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
//===-- allocator_config_wrapper.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_ALLOCATOR_CONFIG_WRAPPER_H_
#define SCUDO_ALLOCATOR_CONFIG_WRAPPER_H_

#include "condition_variable.h"
#include "internal_defs.h"
#include "secondary.h"

namespace {

template <typename T> struct removeConst {
using type = T;
};
template <typename T> struct removeConst<const T> {
using type = T;
};

// This is only used for SFINAE when detecting if a type is defined.
template <typename T> struct voidAdaptor {
using type = void;
};

} // namespace

namespace scudo {

#define OPTIONAL_TEMPLATE(TYPE, NAME, DEFAULT, MEMBER) \
template <typename Config, typename = TYPE> struct NAME##State { \
static constexpr removeConst<TYPE>::type getValue() { return DEFAULT; } \
}; \
template <typename Config> \
struct NAME##State<Config, decltype(Config::MEMBER)> { \
static constexpr removeConst<TYPE>::type getValue() { \
return Config::MEMBER; \
} \
};

#define OPTIONAL_TYPE_TEMPLATE(NAME, DEFAULT, MEMBER) \
template <typename Config, typename Void = void> struct NAME##Type { \
static constexpr bool enabled() { return false; } \
using NAME = DEFAULT; \
}; \
template <typename Config> \
struct NAME##Type<Config, \
typename voidAdaptor<typename Config::MEMBER>::type> { \
static constexpr bool enabled() { return true; } \
using NAME = typename Config::MEMBER; \
};

template <typename AllocatorConfig> struct BaseConfig {
#define BASE_REQUIRED_TEMPLATE_TYPE(NAME) \
template <typename T> using NAME = typename AllocatorConfig::template NAME<T>;

#define BASE_OPTIONAL(TYPE, NAME, DEFAULT) \
OPTIONAL_TEMPLATE(TYPE, NAME, DEFAULT, NAME) \
static constexpr removeConst<TYPE>::type get##NAME() { \
return NAME##State<AllocatorConfig>::getValue(); \
}

#include "allocator_config.def"
}; // BaseConfig

template <typename AllocatorConfig> struct PrimaryConfig {
// TODO: Pass this flag through template argument to remove this hard-coded
// function.
static constexpr bool getMaySupportMemoryTagging() {
return BaseConfig<AllocatorConfig>::getMaySupportMemoryTagging();
}

#define PRIMARY_REQUIRED_TYPE(NAME) \
using NAME = typename AllocatorConfig::Primary::NAME;

#define PRIMARY_REQUIRED(TYPE, NAME) \
static constexpr removeConst<TYPE>::type get##NAME() { \
return AllocatorConfig::Primary::NAME; \
}

#define PRIMARY_OPTIONAL(TYPE, NAME, DEFAULT) \
OPTIONAL_TEMPLATE(TYPE, NAME, DEFAULT, NAME) \
static constexpr removeConst<TYPE>::type get##NAME() { \
return NAME##State<typename AllocatorConfig::Primary>::getValue(); \
}

#define PRIMARY_OPTIONAL_TYPE(NAME, DEFAULT) \
OPTIONAL_TYPE_TEMPLATE(NAME, DEFAULT, NAME) \
static constexpr bool has##NAME() { \
return NAME##Type<typename AllocatorConfig::Primary>::enabled(); \
} \
using NAME = typename NAME##Type<typename AllocatorConfig::Primary>::NAME;

#include "allocator_config.def"

}; // PrimaryConfig

template <typename AllocatorConfig> struct SecondaryConfig {
// TODO: Pass this flag through template argument to remove this hard-coded
// function.
static constexpr bool getMaySupportMemoryTagging() {
return BaseConfig<AllocatorConfig>::getMaySupportMemoryTagging();
}

#define SECONDARY_REQUIRED_TEMPLATE_TYPE(NAME) \
template <typename T> \
using NAME = typename AllocatorConfig::Secondary::template NAME<T>;
#include "allocator_config.def"

struct CacheConfig {
// TODO: Pass this flag through template argument to remove this hard-coded
// function.
static constexpr bool getMaySupportMemoryTagging() {
return BaseConfig<AllocatorConfig>::getMaySupportMemoryTagging();
}

#define SECONDARY_CACHE_OPTIONAL(TYPE, NAME, DEFAULT) \
OPTIONAL_TEMPLATE(TYPE, NAME, DEFAULT, Cache::NAME) \
static constexpr removeConst<TYPE>::type get##NAME() { \
return NAME##State<typename AllocatorConfig::Secondary>::getValue(); \
}
#include "allocator_config.def"
}; // CacheConfig
}; // SecondaryConfig

#undef OPTIONAL_TEMPLATE
#undef OPTIONAL_TEMPLATE_TYPE

} // namespace scudo

#endif // SCUDO_ALLOCATOR_CONFIG_WRAPPER_H_
Loading