Elffuss BraidHub

kikocis/kleidiai public

main · 4.8 KiBRawPermalink
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 "buffer.hpp"
8
9#if defined(__linux__) || defined(__APPLE__)
10#include <sys/mman.h>
11#include <unistd.h>
12#endif // defined(__linux__) || defined(__APPLE__)
13
14#include <algorithm>
15#include <cstddef>
16#include <functional>
17#include <sstream>
18#include <string>
19
20#include "kai/kai_common.h"
21
22namespace kai::test {
23
24Buffer::Buffer(const size_t size) : Buffer(size, 0) {
25}
26
27Buffer::Buffer(const size_t size, const uint8_t init_value = 0) : m_user_buffer_size(size) {
28 KAI_ASSUME_ALWAYS_MSG(size > 0, "Buffers must be of non-zero size");
29
30 // We want to default to overflow protection if supported as it is the most likely failure mode.
31#if defined(__linux__) || defined(__APPLE__)
32 const std::string default_protect_mode = "PROTECT_OVERFLOW";
33#else // defined(__linux__) || defined(__APPLE__)
34 const std::string default_protect_mode = "NONE";
35#endif // defined(__linux__) || defined(__APPLE__)
36
37 const char* val = getenv(buffer_policy_env_name);
38 const std::string buffer_policy = (val != nullptr) ? std::string(val) : default_protect_mode;
39
40 std::ostringstream oss;
41
42 if (buffer_policy == "PROTECT_UNDERFLOW" || buffer_policy == "PROTECT_OVERFLOW") {
43#if defined(__linux__) || defined(__APPLE__)
44 m_protection_policy = (buffer_policy == "PROTECT_UNDERFLOW") ? BufferProtectionPolicy::ProtectUnderflow
45 : BufferProtectionPolicy::ProtectOverflow;
46#else // defined(__linux__) || defined(__APPLE__)
47 oss << buffer_policy << " buffer protection policy is not supported on target platform";
48#endif // defined(__linux__) || defined(__APPLE__)
49 } else if (buffer_policy == "NONE") {
50 m_protection_policy = BufferProtectionPolicy::None;
51 } else {
52 oss << "Unrecognized buffer protection policy provided by " << buffer_policy_env_name << ": ";
53 oss << buffer_policy;
54 }
55
56 if (!oss.str().empty()) {
57 KAI_ERROR(oss.str().c_str());
58 }
59
60 switch (m_protection_policy) {
61#if defined(__linux__) || defined(__APPLE__)
62 case BufferProtectionPolicy::ProtectUnderflow:
63 case BufferProtectionPolicy::ProtectOverflow:
64 allocate_with_guard_pages();
65 break;
66#endif // defined(__linux__) || defined(__APPLE__)
67 default:
68 allocate();
69 }
70
71 memset(data(), init_value, size);
72}
73
74void Buffer::allocate() {
75 m_buffer = handle(std::malloc(m_user_buffer_size), &std::free);
76 KAI_ASSUME_ALWAYS_MSG(m_buffer.get() != nullptr, "Failure allocating memory");
77 KAI_ASSUME_ALWAYS_MSG(m_user_buffer_offset == 0, "Buffer offset must be zero for naive allocation");
78}
79
80#if defined(__linux__) || defined(__APPLE__)
81void Buffer::allocate_with_guard_pages() {
82 const auto sc_pagesize_res = sysconf(_SC_PAGESIZE);
83 KAI_ASSUME_ALWAYS_MSG(sc_pagesize_res != -1, "Error finding page size");
84
85 const auto page_size = static_cast<size_t>(sc_pagesize_res);
86
87 // Offset the user buffer by the size of the first guard page
88 m_user_buffer_offset = page_size;
89
90 // The user buffer is rounded to the size of the nearest whole page.
91 // This forms the valid region between the two guard pages
92 const size_t valid_region_size = kai_roundup(m_user_buffer_size, page_size);
93 const size_t protected_region_size = 2 * page_size;
94 const size_t total_memory_size = valid_region_size + protected_region_size;
95
96 if (m_protection_policy == BufferProtectionPolicy::ProtectOverflow) {
97 // To detect overflows we offset the user buffer so that edge of the buffer is aligned to the start of the
98 // higher guard page thus detecting whenever a buffer overflow occurs.
99 m_user_buffer_offset += valid_region_size - m_user_buffer_size;
100 }
101
102 auto mmap_deleter = [total_memory_size](void* ptr) {
103 if (munmap(ptr, total_memory_size) != 0) {
104 KAI_ERROR("Failure deleting memory mappings");
105 }
106 };
107
108 m_buffer =
109 handle(mmap(nullptr, total_memory_size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0), mmap_deleter);
110 if (m_buffer.get() == MAP_FAILED) {
111 KAI_ERROR("Failure mapping memory");
112 }
113
114 void* head_guard_page = m_buffer.get();
115 void* tail_guard_page = static_cast<std::byte*>(m_buffer.get()) + (total_memory_size - page_size);
116
117 if (mprotect(head_guard_page, std::max(static_cast<size_t>(0), page_size), PROT_NONE) != 0) {
118 KAI_ERROR("Failure protecting page immediately preceding buffer");
119 }
120 if (mprotect(tail_guard_page, std::max(static_cast<size_t>(0), page_size), PROT_NONE) != 0) {
121 KAI_ERROR("Failure protecting page immediately following buffer");
122 }
123}
124#endif // defined(__linux__) || defined(__APPLE__)
125
126} // namespace kai::test