Skip to content

[scudo] Add primary option to zero block on dealloc. #142394

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions compiler-rt/lib/scudo/standalone/allocator_config.def
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ PRIMARY_OPTIONAL_TYPE(ConditionVariableT, ConditionVariableDummy)
// to, in increments of a power-of-2 scale. See `CompactPtrScale` also.
PRIMARY_OPTIONAL_TYPE(CompactPtrT, uptr)

// Clears the memory slot when an allocation is returned to the allocator.
// Operating systems that detects pages filled with zeroes will decommit
// memory.
PRIMARY_OPTIONAL(const bool, ZeroOnDealloc, false)

// SECONDARY_REQUIRED_TEMPLATE_TYPE(NAME)
//
// Defines the type of Secondary Cache to use.
Expand Down
4 changes: 4 additions & 0 deletions compiler-rt/lib/scudo/standalone/flags.inc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ SCUDO_FLAG(bool, delete_size_mismatch, true,

SCUDO_FLAG(bool, zero_contents, false, "Zero chunk contents on allocation.")

SCUDO_FLAG(bool, zero_on_dealloc, false,
"Clears the memory slot when an allocation is returned to the "
"allocator.")

SCUDO_FLAG(bool, pattern_fill_contents, false,
"Pattern fill chunk contents on allocation.")

Expand Down
1 change: 1 addition & 0 deletions compiler-rt/lib/scudo/standalone/primary64.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ template <typename Config> class SizeClassAllocator64 {
static const uptr CompactPtrScale = Config::getCompactPtrScale();
static const uptr RegionSizeLog = Config::getRegionSizeLog();
static const uptr GroupSizeLog = Config::getGroupSizeLog();
static const bool ZeroOnDealloc = Config::getZeroOnDealloc();
static_assert(RegionSizeLog >= GroupSizeLog,
"Group size shouldn't be greater than the region size");
static const uptr GroupScale = GroupSizeLog - CompactPtrScale;
Expand Down
14 changes: 14 additions & 0 deletions compiler-rt/lib/scudo/standalone/size_class_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef SCUDO_SIZE_CLASS_ALLOCATOR_H_
#define SCUDO_SIZE_CLASS_ALLOCATOR_H_

#include "flags.h"
#include "internal_defs.h"
#include "list.h"
#include "platform.h"
Expand All @@ -28,6 +29,7 @@ template <class SizeClassAllocator> struct SizeClassAllocatorLocalCache {
if (LIKELY(S))
S->link(&Stats);
Allocator = A;
ZeroOnDealloc = getFlags()->zero_on_dealloc;
initAllocator();
}

Expand Down Expand Up @@ -59,6 +61,11 @@ template <class SizeClassAllocator> struct SizeClassAllocatorLocalCache {

bool deallocate(uptr ClassId, void *P) {
CHECK_LT(ClassId, NumClasses);

if (SizeClassAllocator::ZeroOnDealloc || ZeroOnDealloc) {
memset(P, 0, SizeClassAllocator::getSizeByClassId(ClassId));
}

PerClass *C = &PerClassArray[ClassId];

// If the cache is full, drain half of blocks back to the main allocator.
Expand Down Expand Up @@ -145,6 +152,7 @@ template <class SizeClassAllocator> struct SizeClassAllocatorLocalCache {
PerClass PerClassArray[NumClasses] = {};
LocalStats Stats;
SizeClassAllocator *Allocator = nullptr;
bool ZeroOnDealloc = false;

NOINLINE void initAllocator() {
for (uptr I = 0; I < NumClasses; I++) {
Expand Down Expand Up @@ -188,6 +196,7 @@ template <class SizeClassAllocator> struct SizeClassAllocatorNoCache {
if (LIKELY(S))
S->link(&Stats);
Allocator = A;
ZeroOnDealloc = getFlags()->zero_on_dealloc;
initAllocator();
}

Expand All @@ -211,6 +220,10 @@ template <class SizeClassAllocator> struct SizeClassAllocatorNoCache {
bool deallocate(uptr ClassId, void *P) {
CHECK_LT(ClassId, NumClasses);

if (SizeClassAllocator::ZeroOnDealloc || ZeroOnDealloc) {
memset(P, 0, SizeClassAllocator::getSizeByClassId(ClassId));
}

if (ClassId == BatchClassId)
return deallocateBatchClassBlock(P);

Expand Down Expand Up @@ -288,6 +301,7 @@ template <class SizeClassAllocator> struct SizeClassAllocatorNoCache {
CompactPtrT BatchClassStorage[SizeClassMap::MaxNumCachedHint] = {};
LocalStats Stats;
SizeClassAllocator *Allocator = nullptr;
bool ZeroOnDealloc = false;

bool deallocateBatchClassBlock(void *P) {
PerClass *C = &PerClassArray[BatchClassId];
Expand Down
24 changes: 23 additions & 1 deletion compiler-rt/lib/scudo/standalone/tests/primary_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,27 @@ template <typename SizeClassMapT> struct TestConfig5 {
};
};

// Enable `ZeroOnDealloc`
template <typename SizeClassMapT> struct TestConfig6 {
static const bool MaySupportMemoryTagging = false;
template <typename> using TSDRegistryT = void;
template <typename> using PrimaryT = void;
template <typename> using SecondaryT = void;

struct Primary {
using SizeClassMap = SizeClassMapT;
static const scudo::uptr RegionSizeLog = 18U;
static const scudo::uptr GroupSizeLog = 18U;
static const scudo::s32 MinReleaseToOsIntervalMs = INT32_MIN;
static const scudo::s32 MaxReleaseToOsIntervalMs = INT32_MAX;
typedef scudo::uptr CompactPtrT;
static const scudo::uptr CompactPtrScale = 0;
static const bool EnableRandomOffset = true;
static const scudo::uptr MapSizeIncrement = 1UL << 18;
static const bool ZeroOnDealloc = true;
};
};

template <template <typename> class BaseConfig, typename SizeClassMapT>
struct Config : public BaseConfig<SizeClassMapT> {};

Expand Down Expand Up @@ -191,7 +212,8 @@ struct ScudoPrimaryTest : public Test {};
SCUDO_TYPED_TEST_TYPE(FIXTURE, NAME, TestConfig2) \
SCUDO_TYPED_TEST_TYPE(FIXTURE, NAME, TestConfig3) \
SCUDO_TYPED_TEST_TYPE(FIXTURE, NAME, TestConfig4) \
SCUDO_TYPED_TEST_TYPE(FIXTURE, NAME, TestConfig5)
SCUDO_TYPED_TEST_TYPE(FIXTURE, NAME, TestConfig5) \
SCUDO_TYPED_TEST_TYPE(FIXTURE, NAME, TestConfig6)
#endif

#define SCUDO_TYPED_TEST_TYPE(FIXTURE, NAME, TYPE) \
Expand Down