1 //===-- allocator_config_test.cpp -------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "tests/scudo_unit_test.h"
11 #include "allocator_config.h"
12 #include "allocator_config_wrapper.h"
14 #include "secondary.h"
16 #include <type_traits>
18 struct TestBaseConfig
{
19 template <typename
> using TSDRegistryT
= void;
20 template <typename
> using PrimaryT
= void;
21 template <typename
> using SecondaryT
= void;
24 struct TestBaseConfigEnableOptionalFlag
: public TestBaseConfig
{
25 static const bool MaySupportMemoryTagging
= true;
26 // Use the getter to avoid the test to `use` the address of static const
27 // variable (which requires additional explicit definition).
28 static bool getMaySupportMemoryTagging() { return MaySupportMemoryTagging
; }
31 struct TestBasePrimaryConfig
{
32 using SizeClassMap
= void;
33 static const scudo::uptr RegionSizeLog
= 18U;
34 static const scudo::uptr GroupSizeLog
= 18U;
35 static const scudo::s32 MinReleaseToOsIntervalMs
= INT32_MIN
;
36 static const scudo::s32 MaxReleaseToOsIntervalMs
= INT32_MAX
;
37 typedef scudo::uptr CompactPtrT
;
38 static const scudo::uptr CompactPtrScale
= 0;
39 static const scudo::uptr MapSizeIncrement
= 1UL << 18;
42 struct TestPrimaryConfig
: public TestBaseConfig
{
43 struct Primary
: TestBasePrimaryConfig
{};
46 struct TestPrimaryConfigEnableOptionalFlag
: public TestBaseConfig
{
47 struct Primary
: TestBasePrimaryConfig
{
48 static const bool EnableRandomOffset
= true;
49 static bool getEnableRandomOffset() { return EnableRandomOffset
; }
53 struct TestPrimaryConfigEnableOptionalType
: public TestBaseConfig
{
54 struct DummyConditionVariable
{};
56 struct Primary
: TestBasePrimaryConfig
{
57 using ConditionVariableT
= DummyConditionVariable
;
61 struct TestSecondaryConfig
: public TestPrimaryConfig
{
63 template <typename Config
>
64 using CacheT
= scudo::MapAllocatorNoCache
<Config
>;
68 struct TestSecondaryCacheConfigEnableOptionalFlag
: public TestPrimaryConfig
{
71 static const scudo::u32 EntriesArraySize
= 256U;
72 static scudo::u32
getEntriesArraySize() { return EntriesArraySize
; }
74 template <typename T
> using CacheT
= scudo::MapAllocatorCache
<T
>;
78 TEST(ScudoAllocatorConfigTest
, VerifyOptionalFlags
) {
79 // Test the top level allocator optional config.
81 // `MaySupportMemoryTagging` is default off.
82 EXPECT_FALSE(scudo::BaseConfig
<TestBaseConfig
>::getMaySupportMemoryTagging());
83 EXPECT_EQ(scudo::BaseConfig
<
84 TestBaseConfigEnableOptionalFlag
>::getMaySupportMemoryTagging(),
85 TestBaseConfigEnableOptionalFlag::getMaySupportMemoryTagging());
87 // Test primary optional config.
89 // `EnableRandomeOffset` is default off.
91 scudo::PrimaryConfig
<TestPrimaryConfig
>::getEnableRandomOffset());
94 TestPrimaryConfigEnableOptionalFlag
>::getEnableRandomOffset(),
95 TestPrimaryConfigEnableOptionalFlag::Primary::getEnableRandomOffset());
97 // `ConditionVariableT` is default off.
99 scudo::PrimaryConfig
<TestPrimaryConfig
>::hasConditionVariableT());
100 EXPECT_TRUE(scudo::PrimaryConfig
<
101 TestPrimaryConfigEnableOptionalType
>::hasConditionVariableT());
102 EXPECT_TRUE((std::is_same_v
<
103 typename
scudo::PrimaryConfig
<
104 TestPrimaryConfigEnableOptionalType
>::ConditionVariableT
,
105 typename
TestPrimaryConfigEnableOptionalType::Primary::
106 ConditionVariableT
>));
108 // Test secondary cache optional config.
109 using NoCacheConfig
=
110 scudo::SecondaryConfig
<TestSecondaryConfig
>::CacheConfig
;
111 // `EntriesArraySize` is default 0.
112 EXPECT_EQ(NoCacheConfig::getEntriesArraySize(), 0U);
114 using CacheConfig
= scudo::SecondaryConfig
<
115 TestSecondaryCacheConfigEnableOptionalFlag
>::CacheConfig
;
116 EXPECT_EQ(CacheConfig::getEntriesArraySize(),
117 TestSecondaryCacheConfigEnableOptionalFlag::Secondary::Cache::
118 getEntriesArraySize());