| 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 | #pragma once |
| 8 | |
| 9 | #include <cstddef> |
| 10 | #include <cstdint> |
| 11 | #include <functional> |
| 12 | #include <memory> |
| 13 | |
| 14 | #include "test/common/span.hpp" |
| 15 | |
| 16 | namespace kai::test { |
| 17 | |
| 18 | /// Buffer is a high-level abstraction for a block of memory. |
| 19 | /// |
| 20 | /// The class performs dynamic memory allocation and management in an opaque manner. The underlying memory resource can |
| 21 | /// be requested using the familiar @ref Buffer::data() method and interacted with using @ref |
| 22 | /// kai::test::read_array<T>() and @ref kai::test::write_array<T>() utilities. |
| 23 | /// |
| 24 | /// Buffer comes with protection mechanisms defined by @ref BufferProtectionPolicy. When guard-page protection is |
| 25 | /// supported, overflow protection is enabled by default. This can be overridden by setting the KAI_TEST_BUFFER_POLICY |
| 26 | /// environment variable, for example: |
| 27 | /// KAI_TEST_BUFFER_POLICY=PROTECT_UNDERFLOW to enable @ref BufferProtectionPolicy::ProtectUnderflow. |
| 28 | /// KAI_TEST_BUFFER_POLICY=PROTECT_OVERFLOW to enable @ref BufferProtectionPolicy::ProtectOverflow. |
| 29 | /// KAI_TEST_BUFFER_POLICY=NONE to disable protection. |
| 30 | /// |
| 31 | class Buffer { |
| 32 | // Handle to the underlying memory resource and its deleter |
| 33 | using handle = std::unique_ptr<void, std::function<void(void*)>>; |
| 34 | |
| 35 | public: |
| 36 | Buffer() = default; |
| 37 | explicit Buffer(size_t size); |
| 38 | Buffer(size_t size, uint8_t init_value); |
| 39 | |
| 40 | Buffer(const Buffer& other) = delete; |
| 41 | Buffer(Buffer&& other) noexcept = default; |
| 42 | Buffer& operator=(const Buffer& other) = delete; |
| 43 | Buffer& operator=(Buffer&& other) noexcept = default; |
| 44 | |
| 45 | ~Buffer() = default; |
| 46 | |
| 47 | /// Gets the base memory address of the user buffer. |
| 48 | /// |
| 49 | /// @return Base memory address of the user buffer. |
| 50 | [[nodiscard]] std::byte* data() const { |
| 51 | return static_cast<std::byte*>(m_buffer.get()) + m_user_buffer_offset; |
| 52 | } |
| 53 | |
| 54 | /// Gets a view of the data. |
| 55 | operator Span<const std::byte>() const { |
| 56 | return {data(), size()}; |
| 57 | } |
| 58 | |
| 59 | /// Gets a view of the data. |
| 60 | operator Span<std::byte>() { |
| 61 | return {data(), size()}; |
| 62 | } |
| 63 | |
| 64 | /// Gets a view of the data. |
| 65 | [[nodiscard]] Span<const std::byte> view() const { |
| 66 | return {data(), size()}; |
| 67 | } |
| 68 | |
| 69 | /// Gets a view of the data. |
| 70 | [[nodiscard]] Span<std::byte> view() { |
| 71 | return {data(), size()}; |
| 72 | } |
| 73 | |
| 74 | /// Gets the size of the user buffer. |
| 75 | /// |
| 76 | /// Depending on the @ref BufferProtectionPolicy policy enabled, the actual size of memory allocated may be larger. |
| 77 | /// However, this function guarantees to always provide the size of the user buffer only. |
| 78 | /// |
| 79 | /// @return Size of the user buffer in bytes. |
| 80 | [[nodiscard]] size_t size() const { |
| 81 | return m_user_buffer_size; |
| 82 | } |
| 83 | |
| 84 | [[nodiscard]] bool has_no_protection() const { |
| 85 | return m_protection_policy == BufferProtectionPolicy::None; |
| 86 | } |
| 87 | |
| 88 | [[nodiscard]] bool protects_underflow() const { |
| 89 | return m_protection_policy == BufferProtectionPolicy::ProtectUnderflow; |
| 90 | } |
| 91 | |
| 92 | [[nodiscard]] bool protects_overflow() const { |
| 93 | return m_protection_policy == BufferProtectionPolicy::ProtectOverflow; |
| 94 | } |
| 95 | |
| 96 | static constexpr const char* buffer_policy_env_name = "KAI_TEST_BUFFER_POLICY"; |
| 97 | |
| 98 | private: |
| 99 | /// Buffer can be protected with one of the following protection policies: |
| 100 | /// - @ref BufferProtectionPolicy::None No protection mechanisms are enabled. This is selected with |
| 101 | /// KAI_TEST_BUFFER_POLICY=NONE, or by default when guard-page |
| 102 | /// protection is not supported. |
| 103 | /// - @ref BufferProtectionPolicy::ProtectUnderflow Memory equal to the size of the user buffer rounded to the |
| 104 | /// nearest whole page plus adjacent guard pages is allocated, |
| 105 | /// and the user buffer is aligned to the end of the head guard |
| 106 | /// page thus detecting whenever a buffer underflow occurs. |
| 107 | /// - @ref BufferProtectionPolicy::ProtectOverflow Same as above, but now the edge of the user buffer is aligned |
| 108 | /// to the start of the tail guard page thus detecting whenever a |
| 109 | /// buffer overflow occurs. |
| 110 | enum class BufferProtectionPolicy : uint8_t { |
| 111 | None = 0, |
| 112 | ProtectUnderflow = 1, |
| 113 | ProtectOverflow = 2, |
| 114 | }; |
| 115 | |
| 116 | /// Naively allocate memory. |
| 117 | void allocate(); |
| 118 | |
| 119 | #if defined(__linux__) || defined(__APPLE__) |
| 120 | /// Allocate memory with adjacent guard pages. |
| 121 | void allocate_with_guard_pages(); |
| 122 | #endif // defined(__linux__) || defined(__APPLE__) |
| 123 | |
| 124 | handle m_buffer = nullptr; |
| 125 | |
| 126 | size_t m_user_buffer_size = 0; |
| 127 | size_t m_user_buffer_offset = 0; |
| 128 | |
| 129 | BufferProtectionPolicy m_protection_policy = BufferProtectionPolicy::None; |
| 130 | }; |
| 131 | |
| 132 | } // namespace kai::test |