| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | // |
| 4 | // SPDX-License-Identifier: Apache-2.0 |
| 5 | // |
| 6 | |
| 7 | #include "test/common/buffer.hpp" |
| 8 | |
| 9 | #include <gtest/gtest.h> |
| 10 | #include <sys/mman.h> |
| 11 | #include <sys/signal.h> |
| 12 | |
| 13 | #include <csignal> |
| 14 | #include <cstddef> |
| 15 | #include <cstdint> |
| 16 | #include <cstdlib> |
| 17 | #include <limits> |
| 18 | #include <optional> |
| 19 | #include <random> |
| 20 | #include <string> |
| 21 | |
| 22 | #include "kai/kai_common.h" |
| 23 | #include "test/common/seed.hpp" |
| 24 | |
| 25 | namespace kai::test { |
| 26 | |
| 27 | namespace { |
| 28 | constexpr size_t g_num_runs = 100; |
| 29 | bool protected_access_was_trapped(const int exit_status) { |
| 30 | return testing::KilledBySignal(SIGBUS)(exit_status) || // |
| 31 | testing::KilledBySignal(SIGSEGV)(exit_status) || // |
| 32 | testing::KilledBySignal(SIGABRT)(exit_status); // |
| 33 | } |
| 34 | |
| 35 | std::string invalid_policy_error_matcher(const char* invalid_policy) { |
| 36 | #ifdef NDEBUG |
| 37 | KAI_UNUSED(invalid_policy); |
| 38 | return ""; |
| 39 | #else |
| 40 | return std::string("Unrecognized buffer protection policy provided by ") + Buffer::buffer_policy_env_name + ": " + |
| 41 | invalid_policy; |
| 42 | #endif // NDEBUG |
| 43 | } |
| 44 | } // namespace |
| 45 | |
| 46 | // Suppress -Wswitch-default around death-test macros; EXPECT_EXIT/EXPECT_DEATH expand to |
| 47 | // framework code that switches over DeathTest::AssumeRole() without a default case. |
| 48 | #define KAI_TEST_DISABLE_SWITCH_DEFAULT_WARNING \ |
| 49 | _Pragma("GCC diagnostic push") _Pragma("GCC diagnostic ignored \"-Wswitch-default\"") |
| 50 | |
| 51 | #define KAI_TEST_RESTORE_SWITCH_DEFAULT_WARNING _Pragma("GCC diagnostic pop") |
| 52 | |
| 53 | class BufferTest : public testing::Test { |
| 54 | protected: |
| 55 | void SetUp() override { |
| 56 | if (const char* buffer_policy_env = getenv(Buffer::buffer_policy_env_name)) { |
| 57 | original_buffer_policy = std::string(buffer_policy_env); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void TearDown() override { |
| 62 | if (original_buffer_policy.has_value()) { |
| 63 | ASSERT_EQ(setenv(Buffer::buffer_policy_env_name, original_buffer_policy->c_str(), 1), 0); |
| 64 | } else { |
| 65 | ASSERT_EQ(unsetenv(Buffer::buffer_policy_env_name), 0); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void set_buffer_policy(const char* policy) { |
| 70 | if (policy == nullptr) { |
| 71 | ASSERT_EQ(unsetenv(Buffer::buffer_policy_env_name), 0); |
| 72 | } else { |
| 73 | ASSERT_EQ(setenv(Buffer::buffer_policy_env_name, policy, 1), 0); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | std::optional<std::string> original_buffer_policy; |
| 79 | }; |
| 80 | |
| 81 | class BufferDeathTest : public BufferTest {}; |
| 82 | |
| 83 | TEST_F(BufferTest, NonePolicy) { |
| 84 | std::mt19937 rng(seed_stream(current_test_key())()); |
| 85 | std::uniform_int_distribution<size_t> dist(1, std::numeric_limits<uint16_t>::max()); |
| 86 | |
| 87 | // Overwrite the buffer policy for purpose of the test |
| 88 | set_buffer_policy("NONE"); |
| 89 | |
| 90 | for (size_t i = 0; i < g_num_runs; ++i) { |
| 91 | const size_t buffer_size = dist(rng); |
| 92 | |
| 93 | const auto buffer = Buffer(buffer_size); |
| 94 | |
| 95 | const auto* data = reinterpret_cast<uint8_t*>(buffer.data()); |
| 96 | ASSERT_NE(data, nullptr); |
| 97 | ASSERT_TRUE(buffer.has_no_protection()); |
| 98 | ASSERT_FALSE(buffer.protects_underflow()); |
| 99 | ASSERT_FALSE(buffer.protects_overflow()); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | TEST_F(BufferDeathTest, InvalidPolicy) { |
| 104 | std::mt19937 rng(seed_stream(current_test_key())()); |
| 105 | std::uniform_int_distribution<size_t> dist(1, std::numeric_limits<uint16_t>::max()); |
| 106 | |
| 107 | // Overwrite the buffer policy for purpose of the test |
| 108 | constexpr const char* invalid_policy = "INVALID_POLICY_TEST"; |
| 109 | set_buffer_policy(invalid_policy); |
| 110 | |
| 111 | for (size_t i = 0; i < g_num_runs; ++i) { |
| 112 | const size_t buffer_size = dist(rng); |
| 113 | |
| 114 | KAI_TEST_DISABLE_SWITCH_DEFAULT_WARNING |
| 115 | EXPECT_DEATH( |
| 116 | { [[maybe_unused]] const auto buffer = Buffer(buffer_size); }, |
| 117 | invalid_policy_error_matcher(invalid_policy)); |
| 118 | KAI_TEST_RESTORE_SWITCH_DEFAULT_WARNING |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | #if defined(__linux__) || defined(__APPLE__) |
| 123 | TEST_F(BufferDeathTest, ProtectUnderflowPolicy) { |
| 124 | std::mt19937 rng(seed_stream(current_test_key())()); |
| 125 | std::uniform_int_distribution<size_t> dist(1, std::numeric_limits<uint16_t>::max()); |
| 126 | |
| 127 | // Overwrite the buffer policy for purpose of the test |
| 128 | set_buffer_policy("PROTECT_UNDERFLOW"); |
| 129 | |
| 130 | for (size_t i = 0; i < g_num_runs; ++i) { |
| 131 | const size_t buffer_size = dist(rng); |
| 132 | |
| 133 | const auto buffer = Buffer(buffer_size); |
| 134 | |
| 135 | const auto* data = reinterpret_cast<uint8_t*>(buffer.data()); |
| 136 | ASSERT_NE(data, nullptr); |
| 137 | ASSERT_NE(data, MAP_FAILED); |
| 138 | ASSERT_FALSE(buffer.has_no_protection()); |
| 139 | ASSERT_TRUE(buffer.protects_underflow()); |
| 140 | ASSERT_FALSE(buffer.protects_overflow()); |
| 141 | |
| 142 | KAI_TEST_DISABLE_SWITCH_DEFAULT_WARNING |
| 143 | EXPECT_EXIT( |
| 144 | // Underflow by one byte |
| 145 | { [[maybe_unused]] const volatile auto val = *--data; }, protected_access_was_trapped, ""); |
| 146 | KAI_TEST_RESTORE_SWITCH_DEFAULT_WARNING |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | TEST_F(BufferDeathTest, ProtectOverflowPolicy) { |
| 151 | std::mt19937 rng(seed_stream(current_test_key())()); |
| 152 | std::uniform_int_distribution<size_t> dist(1, std::numeric_limits<uint16_t>::max()); |
| 153 | |
| 154 | // Overwrite the buffer policy for purpose of the test |
| 155 | set_buffer_policy("PROTECT_OVERFLOW"); |
| 156 | |
| 157 | for (size_t i = 0; i < g_num_runs; ++i) { |
| 158 | const size_t buffer_size = dist(rng); |
| 159 | |
| 160 | const auto buffer = Buffer(buffer_size); |
| 161 | |
| 162 | const auto* data = reinterpret_cast<uint8_t*>(buffer.data()); |
| 163 | ASSERT_NE(data, nullptr); |
| 164 | ASSERT_NE(data, MAP_FAILED); |
| 165 | ASSERT_FALSE(buffer.has_no_protection()); |
| 166 | ASSERT_FALSE(buffer.protects_underflow()); |
| 167 | ASSERT_TRUE(buffer.protects_overflow()); |
| 168 | |
| 169 | KAI_TEST_DISABLE_SWITCH_DEFAULT_WARNING |
| 170 | EXPECT_EXIT( |
| 171 | // Overflow by one byte |
| 172 | { [[maybe_unused]] const volatile auto val = *(data + buffer_size); }, protected_access_was_trapped, ""); |
| 173 | KAI_TEST_RESTORE_SWITCH_DEFAULT_WARNING |
| 174 | } |
| 175 | } |
| 176 | #endif // if defined(__linux__) || defined(__APPLE__) |
| 177 | |
| 178 | TEST_F(BufferDeathTest, DefaultProtectOverflowPolicy) { |
| 179 | set_buffer_policy(nullptr); |
| 180 | |
| 181 | constexpr size_t buffer_size = 64; |
| 182 | const auto buffer = Buffer(buffer_size); |
| 183 | const auto* data = reinterpret_cast<const uint8_t*>(buffer.data()); |
| 184 | |
| 185 | ASSERT_FALSE(buffer.has_no_protection()); |
| 186 | ASSERT_FALSE(buffer.protects_underflow()); |
| 187 | ASSERT_TRUE(buffer.protects_overflow()); |
| 188 | |
| 189 | KAI_TEST_DISABLE_SWITCH_DEFAULT_WARNING |
| 190 | EXPECT_EXIT( |
| 191 | { [[maybe_unused]] const volatile auto val = *(data + buffer_size); }, protected_access_was_trapped, ""); |
| 192 | KAI_TEST_RESTORE_SWITCH_DEFAULT_WARNING |
| 193 | } |
| 194 | |
| 195 | } // namespace kai::test |