| 1 | //===-- allocator_config_test.cpp -------------------------------*- C++ -*-===// |
|---|---|
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "tests/scudo_unit_test.h" |
| 10 | |
| 11 | #include "allocator_config.h" |
| 12 | #include "allocator_config_wrapper.h" |
| 13 | #include "common.h" |
| 14 | #include "secondary.h" |
| 15 | |
| 16 | #include <type_traits> |
| 17 | |
| 18 | struct TestBaseConfig { |
| 19 | template <typename> using TSDRegistryT = void; |
| 20 | template <typename> using PrimaryT = void; |
| 21 | template <typename> using SecondaryT = void; |
| 22 | }; |
| 23 | |
| 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; } |
| 29 | }; |
| 30 | |
| 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; |
| 40 | }; |
| 41 | |
| 42 | struct TestPrimaryConfig : public TestBaseConfig { |
| 43 | struct Primary : TestBasePrimaryConfig {}; |
| 44 | }; |
| 45 | |
| 46 | struct TestPrimaryConfigEnableOptionalFlag : public TestBaseConfig { |
| 47 | struct Primary : TestBasePrimaryConfig { |
| 48 | static const bool EnableRandomOffset = true; |
| 49 | static bool getEnableRandomOffset() { return EnableRandomOffset; } |
| 50 | }; |
| 51 | }; |
| 52 | |
| 53 | struct TestPrimaryConfigEnableOptionalType : public TestBaseConfig { |
| 54 | struct DummyConditionVariable {}; |
| 55 | |
| 56 | struct Primary : TestBasePrimaryConfig { |
| 57 | using ConditionVariableT = DummyConditionVariable; |
| 58 | }; |
| 59 | }; |
| 60 | |
| 61 | struct TestSecondaryConfig : public TestPrimaryConfig { |
| 62 | struct Secondary { |
| 63 | template <typename Config> |
| 64 | using CacheT = scudo::MapAllocatorNoCache<Config>; |
| 65 | }; |
| 66 | }; |
| 67 | |
| 68 | struct TestSecondaryCacheConfigEnableOptionalFlag : public TestPrimaryConfig { |
| 69 | struct Secondary { |
| 70 | struct Cache { |
| 71 | static const scudo::u32 EntriesArraySize = 256U; |
| 72 | static scudo::u32 getEntriesArraySize() { return EntriesArraySize; } |
| 73 | }; |
| 74 | template <typename T> using CacheT = scudo::MapAllocatorCache<T>; |
| 75 | }; |
| 76 | }; |
| 77 | |
| 78 | TEST(ScudoAllocatorConfigTest, VerifyOptionalFlags) { |
| 79 | // Test the top level allocator optional config. |
| 80 | // |
| 81 | // `MaySupportMemoryTagging` is default off. |
| 82 | EXPECT_FALSE(scudo::BaseConfig<TestBaseConfig>::getMaySupportMemoryTagging()); |
| 83 | EXPECT_EQ(scudo::BaseConfig< |
| 84 | TestBaseConfigEnableOptionalFlag>::getMaySupportMemoryTagging(), |
| 85 | TestBaseConfigEnableOptionalFlag::getMaySupportMemoryTagging()); |
| 86 | |
| 87 | // Test primary optional config. |
| 88 | // |
| 89 | // `EnableRandomeOffset` is default off. |
| 90 | EXPECT_FALSE( |
| 91 | scudo::PrimaryConfig<TestPrimaryConfig>::getEnableRandomOffset()); |
| 92 | EXPECT_EQ( |
| 93 | scudo::PrimaryConfig< |
| 94 | TestPrimaryConfigEnableOptionalFlag>::getEnableRandomOffset(), |
| 95 | TestPrimaryConfigEnableOptionalFlag::Primary::getEnableRandomOffset()); |
| 96 | |
| 97 | // `ConditionVariableT` is default off. |
| 98 | EXPECT_FALSE( |
| 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>)); |
| 107 | |
| 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); |
| 113 | |
| 114 | using CacheConfig = scudo::SecondaryConfig< |
| 115 | TestSecondaryCacheConfigEnableOptionalFlag>::CacheConfig; |
| 116 | EXPECT_EQ(CacheConfig::getEntriesArraySize(), |
| 117 | TestSecondaryCacheConfigEnableOptionalFlag::Secondary::Cache:: |
| 118 | getEntriesArraySize()); |
| 119 | } |
| 120 |
