|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <executorch/backends/qualcomm/runtime/SharedBuffer.h> |
| 10 | +#include <executorch/runtime/platform/compiler.h> |
| 11 | +#include <gtest/gtest.h> |
| 12 | +#include <atomic> |
| 13 | +#include <cstdint> |
| 14 | +#include <cstdlib> |
| 15 | +#include <limits> |
| 16 | +#include <memory> |
| 17 | +#include <new> |
| 18 | +#include <thread> |
| 19 | +#include <vector> |
| 20 | + |
| 21 | +#if ET_HAS_EXCEPTIONS && !defined(_GLIBCXX_RESTRICT_EXCEPTIONS) |
| 22 | +namespace { |
| 23 | +thread_local int allocationsUntilFailure = -1; |
| 24 | +} // namespace |
| 25 | + |
| 26 | +void* operator new(std::size_t size) { |
| 27 | + if (allocationsUntilFailure >= 0 && allocationsUntilFailure-- == 0) { |
| 28 | + throw std::bad_alloc(); |
| 29 | + } |
| 30 | + if (void* pointer = std::malloc(size == 0 ? 1 : size)) { |
| 31 | + return pointer; |
| 32 | + } |
| 33 | + throw std::bad_alloc(); |
| 34 | +} |
| 35 | + |
| 36 | +void operator delete(void* pointer) noexcept { |
| 37 | + std::free(pointer); |
| 38 | +} |
| 39 | + |
| 40 | +void operator delete(void* pointer, std::size_t) noexcept { |
| 41 | + std::free(pointer); |
| 42 | +} |
| 43 | + |
| 44 | +void* operator new(std::size_t size, const std::nothrow_t&) noexcept { |
| 45 | + try { |
| 46 | + return ::operator new(size); |
| 47 | + } catch (const std::bad_alloc&) { |
| 48 | + return nullptr; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +void operator delete(void* pointer, const std::nothrow_t&) noexcept { |
| 53 | + std::free(pointer); |
| 54 | +} |
| 55 | +#endif |
| 56 | + |
| 57 | +namespace executorch::backends::qnn { |
| 58 | + |
| 59 | +class SharedBufferTest : public ::testing::Test { |
| 60 | + protected: |
| 61 | + void SetUp() override { |
| 62 | + allocationCalls_ = 0; |
| 63 | + buffer_.reset(new SharedBuffer()); |
| 64 | + buffer_->rpc_mem_alloc_ = [](int, uint32_t, int bytes) -> void* { |
| 65 | + ++allocationCalls_; |
| 66 | + return bytes > 0 && bytes <= 1024 * 1024 |
| 67 | + ? std::malloc(static_cast<size_t>(bytes)) |
| 68 | + : nullptr; |
| 69 | + }; |
| 70 | + buffer_->rpc_mem_free_ = [](void* pointer) { std::free(pointer); }; |
| 71 | + buffer_->rpc_mem_to_fd_ = [](void*) { return 17; }; |
| 72 | + buffer_->SetInitialize(true); |
| 73 | + } |
| 74 | + |
| 75 | + void TearDown() override { |
| 76 | + for (const auto& allocation : buffer_->restore_map_) { |
| 77 | + std::free(allocation.second); |
| 78 | + } |
| 79 | + buffer_->SetInitialize(false); |
| 80 | + } |
| 81 | + |
| 82 | + void useOffsetRpcAllocator() { |
| 83 | + buffer_->rpc_mem_alloc_ = [](int, uint32_t, int bytes) -> void* { |
| 84 | + rpcAllocation_ = |
| 85 | + static_cast<uint8_t*>(std::malloc(static_cast<size_t>(bytes) + 1)) + |
| 86 | + 1; |
| 87 | + return rpcAllocation_; |
| 88 | + }; |
| 89 | + buffer_->rpc_mem_free_ = [](void* pointer) { |
| 90 | + rpcFreed_ = pointer; |
| 91 | + std::free(static_cast<uint8_t*>(pointer) - 1); |
| 92 | + }; |
| 93 | + } |
| 94 | + |
| 95 | + bool empty() const { |
| 96 | + return buffer_->restore_map_.empty() && |
| 97 | + buffer_->allocated_size_map_.empty() && |
| 98 | + buffer_->tensor_addr_to_custom_mem_.empty() && |
| 99 | + buffer_->custom_mem_to_tensor_addr_.empty(); |
| 100 | + } |
| 101 | + |
| 102 | +#if ET_HAS_EXCEPTIONS && !defined(_GLIBCXX_RESTRICT_EXCEPTIONS) |
| 103 | + void failBookkeepingAllocation(int successfulAllocations) { |
| 104 | + buffer_->restore_map_.reserve(1); |
| 105 | + buffer_->allocated_size_map_.reserve(1); |
| 106 | + buffer_->rpc_mem_alloc_ = [](int, uint32_t, int bytes) -> void* { |
| 107 | + rpcAllocation_ = std::malloc(static_cast<size_t>(bytes)); |
| 108 | + return rpcAllocation_; |
| 109 | + }; |
| 110 | + buffer_->rpc_mem_free_ = [](void* pointer) { |
| 111 | + rpcFreed_ = pointer; |
| 112 | + std::free(pointer); |
| 113 | + }; |
| 114 | + rpcFreed_ = nullptr; |
| 115 | + allocationsUntilFailure = successfulAllocations; |
| 116 | + } |
| 117 | +#endif |
| 118 | + |
| 119 | + std::unique_ptr<SharedBuffer> buffer_; |
| 120 | + inline static std::atomic<size_t> allocationCalls_{0}; |
| 121 | + inline static void* rpcAllocation_{nullptr}; |
| 122 | + inline static void* rpcFreed_{nullptr}; |
| 123 | +}; |
| 124 | + |
| 125 | +TEST_F(SharedBufferTest, AlignsOffsetRpcAllocationAndFreesTheOriginal) { |
| 126 | + constexpr size_t kAlignment = 64; |
| 127 | + constexpr size_t kBytes = 128; |
| 128 | + useOffsetRpcAllocator(); |
| 129 | + |
| 130 | + auto* aligned = static_cast<uint8_t*>(buffer_->AllocMem(kBytes, kAlignment)); |
| 131 | + ASSERT_NE(aligned, nullptr); |
| 132 | + auto* original = static_cast<uint8_t*>(rpcAllocation_); |
| 133 | + EXPECT_EQ(reinterpret_cast<uintptr_t>(aligned) % kAlignment, 0); |
| 134 | + EXPECT_GT(aligned, original); |
| 135 | + EXPECT_LE(aligned + kBytes, original + buffer_->GetAllocatedSize(aligned)); |
| 136 | + buffer_->FreeMem(aligned); |
| 137 | + |
| 138 | + EXPECT_EQ(rpcFreed_, original); |
| 139 | + EXPECT_TRUE(empty()); |
| 140 | +} |
| 141 | + |
| 142 | +TEST_F(SharedBufferTest, InvalidSizeAndAlignmentDoNotCallRpcAllocator) { |
| 143 | + EXPECT_EQ(buffer_->AllocMem(0, alignof(void*)), nullptr); |
| 144 | + EXPECT_EQ(buffer_->AllocMem(64, 0), nullptr); |
| 145 | + EXPECT_EQ(buffer_->AllocMem(64, 3), nullptr); |
| 146 | + EXPECT_EQ( |
| 147 | + buffer_->AllocMem(std::numeric_limits<int32_t>::max(), alignof(void*)), |
| 148 | + nullptr); |
| 149 | + EXPECT_EQ( |
| 150 | + buffer_->AllocMem(std::numeric_limits<size_t>::max(), alignof(void*)), |
| 151 | + nullptr); |
| 152 | + EXPECT_EQ(allocationCalls_.load(), 0); |
| 153 | + EXPECT_TRUE(empty()); |
| 154 | +} |
| 155 | + |
| 156 | +TEST_F(SharedBufferTest, ConcurrentAllocateRegisterLookupAndFree) { |
| 157 | + constexpr size_t kThreads = 4; |
| 158 | + constexpr size_t kAllocations = 256; |
| 159 | + constexpr size_t kBytes = 64; |
| 160 | + std::vector<void*> buffers(kThreads * kAllocations, nullptr); |
| 161 | + std::vector<std::thread> workers; |
| 162 | + workers.reserve(kThreads); |
| 163 | + std::atomic<bool> start{false}; |
| 164 | + for (size_t worker = 0; worker < kThreads; ++worker) { |
| 165 | + workers.emplace_back([&, worker] { |
| 166 | + while (!start.load(std::memory_order_acquire)) { |
| 167 | + std::this_thread::yield(); |
| 168 | + } |
| 169 | + for (size_t index = worker; index < buffers.size(); index += kThreads) { |
| 170 | + void* pointer = buffer_->AllocMem(kBytes, alignof(void*)); |
| 171 | + ASSERT_NE(pointer, nullptr); |
| 172 | + buffers[index] = pointer; |
| 173 | + void* alias = static_cast<uint8_t*>(pointer) + sizeof(uint64_t); |
| 174 | + buffer_->AddCusomMemTensorAddr(alias, pointer); |
| 175 | + EXPECT_TRUE(buffer_->IsAllocated(pointer)); |
| 176 | + EXPECT_EQ(buffer_->GetCustomMemBase(alias), pointer); |
| 177 | + EXPECT_GE(buffer_->GetAllocatedSize(pointer), kBytes); |
| 178 | + EXPECT_EQ(buffer_->MemToFd(pointer), 17); |
| 179 | + } |
| 180 | + }); |
| 181 | + } |
| 182 | + start.store(true, std::memory_order_release); |
| 183 | + for (auto& worker : workers) { |
| 184 | + worker.join(); |
| 185 | + } |
| 186 | + EXPECT_EQ(allocationCalls_.load(), buffers.size()); |
| 187 | + |
| 188 | + workers.clear(); |
| 189 | + for (size_t worker = 0; worker < kThreads; ++worker) { |
| 190 | + workers.emplace_back([&, worker] { |
| 191 | + for (size_t index = worker; index < buffers.size(); index += kThreads) { |
| 192 | + void* pointer = buffers[index]; |
| 193 | + ASSERT_NE(pointer, nullptr); |
| 194 | + void* alias = static_cast<uint8_t*>(pointer) + sizeof(uint64_t); |
| 195 | + buffer_->FreeMem(pointer); |
| 196 | + EXPECT_FALSE(buffer_->IsAllocated(pointer)); |
| 197 | + EXPECT_EQ(buffer_->GetAllocatedSize(pointer), 0); |
| 198 | + EXPECT_EQ(buffer_->GetCustomMemBase(alias), nullptr); |
| 199 | + } |
| 200 | + }); |
| 201 | + } |
| 202 | + for (auto& worker : workers) { |
| 203 | + worker.join(); |
| 204 | + } |
| 205 | + EXPECT_TRUE(empty()); |
| 206 | +} |
| 207 | + |
| 208 | +#if ET_HAS_EXCEPTIONS && !defined(_GLIBCXX_RESTRICT_EXCEPTIONS) |
| 209 | +TEST_F(SharedBufferTest, BookkeepingAllocationFailureRollsBackAndReturnsNull) { |
| 210 | + // Reserved buckets leave one node allocation per map insertion. |
| 211 | + for (int successfulAllocations : {0, 1}) { |
| 212 | + failBookkeepingAllocation(successfulAllocations); |
| 213 | + void* result = buffer_->AllocMem(64, alignof(void*)); |
| 214 | + allocationsUntilFailure = -1; |
| 215 | + EXPECT_EQ(result, nullptr); |
| 216 | + EXPECT_EQ(rpcFreed_, rpcAllocation_); |
| 217 | + EXPECT_TRUE(empty()); |
| 218 | + |
| 219 | + void* retry = buffer_->AllocMem(64, alignof(void*)); |
| 220 | + ASSERT_NE(retry, nullptr); |
| 221 | + buffer_->FreeMem(retry); |
| 222 | + EXPECT_TRUE(empty()); |
| 223 | + } |
| 224 | +} |
| 225 | +#endif |
| 226 | + |
| 227 | +} // namespace executorch::backends::qnn |
0 commit comments