Merged
为 aclDestroyXxx 系列 API 增加 double-free 检测能力 #610
ren-botaoAdd on Jul 6
为 aclDestroyXxx 系列 API 增加 double-free 检测能力 #610
Merged
Merged fromren-botao/opbasemaintain_test_logintocann/opbasemaster
Total: 8 files changed+257-2
| @@ -25,6 +25,8 @@ | |||
| 25 | #include "dlopen_api.h" | 25 | #include "dlopen_api.h" |
| 26 | #include "op_dfx_internal.h" | 26 | #include "op_dfx_internal.h" |
| 27 | #include "file_utils.h" | 27 | #include "file_utils.h" |
| 28 | +#include "bridge_pool.h" | ||
| 29 | + | ||
| 28 | #ifdef __cplusplus | 30 | #ifdef __cplusplus |
| 29 | extern "C" { | 31 | extern "C" { |
| 30 | #endif | 32 | #endif |
| @@ -92,6 +94,10 @@ aclnnStatus aclDestroyTensor(const aclTensor* tensor) | |||
| 92 | if (tensor == nullptr) { | 94 | if (tensor == nullptr) { |
| 93 | return OK; | 95 | return OK; |
| 94 | } | 96 | } |
| 97 | + if (unlikely(op::internal::IsAclnnDebugEnabled()) && | ||
| 98 | + op::internal::CheckDoubleFree(const_cast<aclTensor*>(tensor))) { | ||
| 99 | + OP_LOGW("possible double-free at addr %p.", static_cast<const void*>(tensor)); | ||
| 100 | + } | ||
| 95 | delete tensor; | 101 | delete tensor; |
| 96 | return OK; | 102 | return OK; |
| 97 | } | 103 | } |
| @@ -101,6 +107,10 @@ aclnnStatus aclDestroyScalar(const aclScalar* scalar) | |||
| 101 | if (scalar == nullptr) { | 107 | if (scalar == nullptr) { |
| 102 | return OK; | 108 | return OK; |
| 103 | } | 109 | } |
| 110 | + if (unlikely(op::internal::IsAclnnDebugEnabled()) && | ||
| 111 | + op::internal::CheckDoubleFree(const_cast<aclScalar*>(scalar))) { | ||
| 112 | + OP_LOGW("possible double-free at addr %p.", static_cast<const void*>(scalar)); | ||
| 113 | + } | ||
| 104 | delete scalar; | 114 | delete scalar; |
| 105 | return OK; | 115 | return OK; |
| 106 | } | 116 | } |
| @@ -110,6 +120,10 @@ aclnnStatus aclDestroyIntArray(const aclIntArray* array) | |||
| 110 | if (array == nullptr) { | 120 | if (array == nullptr) { |
| 111 | return OK; | 121 | return OK; |
| 112 | } | 122 | } |
| 123 | + if (unlikely(op::internal::IsAclnnDebugEnabled()) && | ||
| 124 | + op::internal::CheckDoubleFree(const_cast<aclIntArray*>(array))) { | ||
| 125 | + OP_LOGW("possible double-free at addr %p.", static_cast<const void*>(array)); | ||
| 126 | + } | ||
| 113 | delete array; | 127 | delete array; |
| 114 | return OK; | 128 | return OK; |
| 115 | } | 129 | } |
| @@ -119,6 +133,10 @@ aclnnStatus aclDestroyFloatArray(const aclFloatArray* array) | |||
| 119 | if (array == nullptr) { | 133 | if (array == nullptr) { |
| 120 | return OK; | 134 | return OK; |
| 121 | } | 135 | } |
| 136 | + if (unlikely(op::internal::IsAclnnDebugEnabled()) && | ||
| 137 | + op::internal::CheckDoubleFree(const_cast<aclFloatArray*>(array))) { | ||
| 138 | + OP_LOGW("possible double-free at addr %p.", static_cast<const void*>(array)); | ||
| 139 | + } | ||
| 122 | delete array; | 140 | delete array; |
| 123 | return OK; | 141 | return OK; |
| 124 | } | 142 | } |
| @@ -128,6 +146,10 @@ aclnnStatus aclDestroyBoolArray(const aclBoolArray* array) | |||
| 128 | if (array == nullptr) { | 146 | if (array == nullptr) { |
| 129 | return OK; | 147 | return OK; |
| 130 | } | 148 | } |
| 149 | + if (unlikely(op::internal::IsAclnnDebugEnabled()) && | ||
| 150 | + op::internal::CheckDoubleFree(const_cast<aclBoolArray*>(array))) { | ||
| 151 | + OP_LOGW("possible double-free at addr %p.", static_cast<const void*>(array)); | ||
| 152 | + } | ||
| 131 | delete array; | 153 | delete array; |
| 132 | return OK; | 154 | return OK; |
| 133 | } | 155 | } |
| @@ -137,6 +159,11 @@ aclnnStatus aclDestroyTensorList(const aclTensorList* array) | |||
| 137 | if (array == nullptr) { | 159 | if (array == nullptr) { |
| 138 | return OK; | 160 | return OK; |
| 139 | } | 161 | } |
| 162 | + // 仅对外层 list 指针本身做一次预检;元素由循环内 aclDestroyTensor 各自覆盖 | ||
| 163 | + if (unlikely(op::internal::IsAclnnDebugEnabled()) && | ||
| 164 | + op::internal::CheckDoubleFree(const_cast<aclTensorList*>(array))) { | ||
| 165 | + OP_LOGW("possible double-free at addr %p.", static_cast<const void*>(array)); | ||
| 166 | + } | ||
| 140 | for (uint64_t i = 0; i < array->Size(); i++) { | 167 | for (uint64_t i = 0; i < array->Size(); i++) { |
| 141 | aclDestroyTensor((*array)[i]); | 168 | aclDestroyTensor((*array)[i]); |
| 142 | } | 169 | } |
| @@ -149,6 +176,11 @@ aclnnStatus aclDestroyScalarList(const aclScalarList* array) | |||
| 149 | if (array == nullptr) { | 176 | if (array == nullptr) { |
| 150 | return OK; | 177 | return OK; |
| 151 | } | 178 | } |
| 179 | + // 仅对外层 list 指针本身做一次预检;元素由循环内 aclDestroyScalar 各自覆盖 | ||
| 180 | + if (unlikely(op::internal::IsAclnnDebugEnabled()) && | ||
| 181 | + op::internal::CheckDoubleFree(const_cast<aclScalarList*>(array))) { | ||
| 182 | + OP_LOGW("possible double-free at addr %p.", static_cast<const void*>(array)); | ||
| 183 | + } | ||
| 152 | for (uint64_t i = 0; i < array->Size(); i++) { | 184 | for (uint64_t i = 0; i < array->Size(); i++) { |
| 153 | aclDestroyScalar((*array)[i]); | 185 | aclDestroyScalar((*array)[i]); |
| 154 | } | 186 | } |
| @@ -14,11 +14,13 @@ | |||
| 14 | #include <algorithm> | 14 | #include <algorithm> |
| 15 | #include <array> | 15 | #include <array> |
| 16 | #include <atomic> | 16 | #include <atomic> |
| 17 | +#include <cstdint> | ||
| 17 | #include <cstdlib> | 18 | #include <cstdlib> |
| 18 | #include <cstring> | 19 | #include <cstring> |
| 19 | #include <functional> | 20 | #include <functional> |
| 20 | #include <limits> | 21 | #include <limits> |
| 21 | #include <mutex> | 22 | #include <mutex> |
| 23 | +#include <new> | ||
| 22 | #include <numeric> | 24 | #include <numeric> |
| 23 | #include <type_traits> | 25 | #include <type_traits> |
| 24 | #include <vector> | 26 | #include <vector> |
| @@ -111,6 +113,8 @@ private: | |||
| 111 | } | 113 | } |
| 112 | 114 | ||
| 113 | BlockStore::BlockHeader* head = static_cast<BlockStore::BlockHeader*>(p); | 115 | BlockStore::BlockHeader* head = static_cast<BlockStore::BlockHeader*>(p); |
| 116 | + // std::malloc 不调用构造函数,需 placement new 初始化 magic_ 等非静态成员数据初始化 | ||
| 117 | + new (head) BlockStore::BlockHeader; | ||
| 114 | head->userTag_ = SYS_TAG; | 118 | head->userTag_ = SYS_TAG; |
| 115 | head->cacheExt_ = BlockStore::NOT_IN_CACHE; | 119 | head->cacheExt_ = BlockStore::NOT_IN_CACHE; |
| 116 | return head + 1; | 120 | return head + 1; |
| @@ -141,6 +145,8 @@ private: | |||
| 141 | void* block = std::malloc(sizeof(BlockStore::BlockHeader) + store->GetBlockSize()); | 145 | void* block = std::malloc(sizeof(BlockStore::BlockHeader) + store->GetBlockSize()); |
| 142 | if (block) { | 146 | if (block) { |
| 143 | BlockStore::BlockHeader* head = static_cast<BlockStore::BlockHeader*>(block); | 147 | BlockStore::BlockHeader* head = static_cast<BlockStore::BlockHeader*>(block); |
| 148 | + // std::malloc 不调用构造函数,需 placement new 初始化 magic_ 等非静态成员数据初始化 | ||
| 149 | + new (head) BlockStore::BlockHeader; | ||
| 144 | head->userTag_ = SYS_TAG; | 150 | head->userTag_ = SYS_TAG; |
| 145 | addrList[n++] = head + 1; | 151 | addrList[n++] = head + 1; |
| 146 | } else { | 152 | } else { |
| @@ -211,7 +217,7 @@ private: | |||
| 211 | * @brief Block size and count of BlockStore | 217 | * @brief Block size and count of BlockStore |
| 212 | */ | 218 | */ |
| 213 | struct BlockDesc { | 219 | struct BlockDesc { |
| 214 | - BlockDesc(size_t s, size_t c) : size(s), count(c) {}; | 220 | + BlockDesc(size_t s, size_t c) : size(s), count(c){}; |
| 215 | size_t size{0}; | 221 | size_t size{0}; |
| 216 | size_t count{0}; | 222 | size_t count{0}; |
| 217 | }; | 223 | }; |
| @@ -295,6 +301,9 @@ public: | |||
| 295 | 301 | ||
| 296 | static void CacheFree(void* block) { return get_instance().CacheFreeImpl(block); } | 302 | static void CacheFree(void* block) { return get_instance().CacheFreeImpl(block); } |
| 297 | 303 | ||
| 304 | + // 返回当前线程 BlockCache 实例地址,供 CheckDoubleFree 判定 block 活跃态使用 | ||
| 305 | + static uintptr_t CurrentThreadCacheAddr() { return reinterpret_cast<uintptr_t>(&get_instance()); } | ||
| 306 | + | ||
| 298 | OpSpinlock guard_; | 307 | OpSpinlock guard_; |
| 299 | 308 | ||
| 300 | private: | 309 | private: |
| @@ -323,6 +332,8 @@ private: | |||
| 323 | break; | 332 | break; |
| 324 | } | 333 | } |
| 325 | BlockStore::BlockHeader* head = static_cast<BlockStore::BlockHeader*>(block); | 334 | BlockStore::BlockHeader* head = static_cast<BlockStore::BlockHeader*>(block); |
| 335 | + // std::malloc 不调用构造函数,需 placement new 初始化 magic_ 等非静态成员数据初始化 | ||
| 336 | + new (head) BlockStore::BlockHeader; | ||
| 326 | head->userTag_ = BlockPool::SYS_TAG; | 337 | head->userTag_ = BlockPool::SYS_TAG; |
| 327 | addrList[n++] = head + 1; | 338 | addrList[n++] = head + 1; |
| 328 | } | 339 | } |
| @@ -22,6 +22,7 @@ int32_t GetPoolIndex(); | |||
| 22 | void UpdateHugeMemIndex(int32_t id); | 22 | void UpdateHugeMemIndex(int32_t id); |
| 23 | void FreeHugeMem(); | 23 | void FreeHugeMem(); |
| 24 | void* GetAddr(const int32_t id, size_t size); | 24 | void* GetAddr(const int32_t id, size_t size); |
| 25 | +bool CheckDoubleFree(void* addr); | ||
| 25 | } // namespace internal | 26 | } // namespace internal |
| 26 | } // namespace op | 27 | } // namespace op |
| 27 | #endif | 28 | #endif |
| @@ -10,6 +10,7 @@ | |||
| 10 | 10 | ||
| 11 | #ifndef OP_DFX_INTERNAL_H_ | 11 | #ifndef OP_DFX_INTERNAL_H_ |
| 12 | #define OP_DFX_INTERNAL_H_ | 12 | #define OP_DFX_INTERNAL_H_ |
| 13 | +#include <atomic> | ||
| 13 | #include "profiling/aprof_pub.h" | 14 | #include "profiling/aprof_pub.h" |
| 14 | #include "op_dfx_util.h" | 15 | #include "op_dfx_util.h" |
| 15 | #include "op_cache_internal.h" | 16 | #include "op_cache_internal.h" |
| @@ -164,6 +165,11 @@ void PrepareL2DumpTensor(std::vector<Adx::TensorInfoV2>& dumpTensors, const std: | |||
| 164 | bool GetOpProfilingRecordArgFlag(); | 165 | bool GetOpProfilingRecordArgFlag(); |
| 165 | void SetOpProfilingRecordArgFlag(bool value); | 166 | void SetOpProfilingRecordArgFlag(bool value); |
| 166 | 167 | ||
| 168 | +extern std::atomic<bool> g_aclnnDebugEnabled; | ||
| 169 | + | ||
| 170 | +void InitAclnnDebugSwitch(); | ||
| 171 | +inline bool IsAclnnDebugEnabled() { return g_aclnnDebugEnabled.load(std::memory_order_relaxed); } | ||
| 172 | + | ||
| 167 | #ifdef __cplusplus | 173 | #ifdef __cplusplus |
| 168 | extern "C" { | 174 | extern "C" { |
| 169 | #endif | 175 | #endif |
| @@ -8,8 +8,13 @@ | |||
| 8 | * See LICENSE in the root of the software repository for the full text of the License. | 8 | * See LICENSE in the root of the software repository for the full text of the License. |
| 9 | */ | 9 | */ |
| 10 | 10 | ||
| 11 | -#include "thread_local_context.h" | ||
| 12 | #include "bridge_pool.h" | 11 | #include "bridge_pool.h" |
| 12 | +#include <stddef.h> | ||
| 13 | +#include <stdint.h> | ||
| 14 | +#include "block_pool.h" | ||
| 15 | +#include "opdev/op_dfx.h" | ||
| 16 | +#include "opdev/op_log.h" | ||
| 17 | +#include "thread_local_context.h" | ||
| 13 | 18 | ||
| 14 | namespace op { | 19 | namespace op { |
| 15 | namespace internal { | 20 | namespace internal { |
| @@ -42,5 +47,47 @@ void UpdateHugeMemIndex(int32_t id) | |||
| 42 | op::internal::GetThreadLocalContext().poolIndex_ = id; | 47 | op::internal::GetThreadLocalContext().poolIndex_ = id; |
| 43 | } | 48 | } |
| 44 | 49 | ||
| 50 | +bool CheckDoubleFree(void* addr) | ||
| 51 | +{ | ||
| 52 | + if (addr == nullptr) { | ||
| 53 | + return false; | ||
| 54 | + } | ||
| 55 | + if (BlockPool::InHugeMemRange(addr)) { | ||
| 56 | + return false; | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + BlockStore::BlockHeader* head = BlockStore::GetBlockHeader(addr); | ||
| 60 | + if (head->magic_ != BlockStore::MAGIC) { | ||
| 61 | + return false; | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + uintptr_t ext = head->cacheExt_; | ||
| 65 | + if (ext == BlockStore::NOT_IN_CACHE) { | ||
| 66 | + return false; | ||
| 67 | + } | ||
| 68 | + if (ext == BlockCache::CurrentThreadCacheAddr()) { | ||
| 69 | + return false; | ||
| 70 | + } | ||
| 71 | + if (ext == reinterpret_cast<uintptr_t>(nullptr)) { | ||
| 72 | + OP_LOGW("cacheExt_ is nullptr, block has been returned to cache list (list head was empty)."); | ||
| 73 | + return true; | ||
| 74 | + } | ||
| 75 | + // 区分"指向合法 BlockHeader(已归还到 cache 链表,cacheExt_ 存的是 cacheHead_[idx] 即 BlockHeader*)" | ||
| 76 | + // 与"指向活跃 block 的 BlockCache* this(含跨线程 free 场景)" | ||
| 77 | + // cacheExt_ 在 CacheFreeImpl/PoolAlloc 中写入的是 uintptr_t(cacheHead_[idx]) 或 uintptr_t(this): | ||
| 78 | + // - 已归还到链表:cacheExt_ = cacheHead_[idx],类型为 BlockHeader*,直接强转即可读 magic_ | ||
| 79 | + // - 活跃态(本线程):上面已通过 CurrentThreadCacheAddr() 短路返回 false | ||
| 80 | + // - 跨线程 free:cacheExt_ 指向分配方线程的 BlockCache 实例对象(this),强转后落在该对象内部某偏移, | ||
| 81 | + // 4 字节偶然匹配 MAGIC 概率 1/2^32,可忽略 | ||
| 82 | + // 注:block_pool.h 中 CacheFreeImpl/PoolAlloc 对 cacheExt_ 的写入均直接 reinterpret_cast 指针为 uintptr_t | ||
| 83 | + // (无 +1/-1 算术),故此处读 magic_ 也不做指针算术,保持与写入端对称 | ||
| 84 | + BlockStore::BlockHeader* nextHead = reinterpret_cast<BlockStore::BlockHeader*>(ext); // NOLINT | ||
| 85 | + if (nextHead->magic_ == BlockStore::MAGIC) { | ||
| 86 | + OP_LOGW("cacheExt_ points to another block header in cache list, block has been returned already."); | ||
| 87 | + return true; | ||
| 88 | + } | ||
| 89 | + return false; | ||
| 90 | +} | ||
| 91 | + | ||
| 45 | } // namespace internal | 92 | } // namespace internal |
| 46 | } // namespace op | 93 | } // namespace op |
| @@ -124,6 +124,19 @@ void SetOpProfilingRecordArgFlag(bool value) | |||
| 124 | opProfilingSwitch.recordOpArgFlag = value; | 124 | opProfilingSwitch.recordOpArgFlag = value; |
| 125 | } | 125 | } |
| 126 | 126 | ||
| 127 | +std::atomic<bool> g_aclnnDebugEnabled{false}; | ||
| 128 | +static std::once_flag g_aclnnDebugOnceFlag; | ||
| 129 | + | ||
| 130 | +void InitAclnnDebugSwitch() | ||
| 131 | +{ | ||
| 132 | + std::call_once(g_aclnnDebugOnceFlag, []() { | ||
| 133 | + if (CheckLogLevelInner(OP_ID, OP_LOG_INFO) == 1) { | ||
| 134 | + g_aclnnDebugEnabled.store(true, std::memory_order_relaxed); | ||
| 135 | + OP_LOGI("aclnn debug mode enabled by log level"); | ||
| 136 | + } | ||
| 137 | + }); | ||
| 138 | +} | ||
| 139 | + | ||
| 127 | OpThreadLocalContext& GetThreadLocalContext() | 140 | OpThreadLocalContext& GetThreadLocalContext() |
| 128 | { | 141 | { |
| 129 | thread_local static OpThreadLocalContext oPThreadLocalContext; | 142 | thread_local static OpThreadLocalContext oPThreadLocalContext; |
| @@ -27,6 +27,7 @@ | |||
| 27 | #include "op_cache_internal.h" | 27 | #include "op_cache_internal.h" |
| 28 | #include "kernel_utils.h" | 28 | #include "kernel_utils.h" |
| 29 | #include "thread_local_context.h" | 29 | #include "thread_local_context.h" |
| 30 | +#include "op_dfx_internal.h" | ||
| 30 | #include "dlopen_api.h" | 31 | #include "dlopen_api.h" |
| 31 | 32 | ||
| 32 | using namespace op::internal; | 33 | using namespace op::internal; |
| @@ -1027,6 +1028,7 @@ int64_t GetDeterministicLevelFromRt() | |||
| 1027 | 1028 | ||
| 1028 | void InitL2Phase1Context(const char* l2Name, [[maybe_unused]] aclOpExecutor** executor) | 1029 | void InitL2Phase1Context(const char* l2Name, [[maybe_unused]] aclOpExecutor** executor) |
| 1029 | { | 1030 | { |
| 1031 | + InitAclnnDebugSwitch(); | ||
| 1030 | auto& opTlsCtx = op::internal::GetThreadLocalContext(); | 1032 | auto& opTlsCtx = op::internal::GetThreadLocalContext(); |
| 1031 | opTlsCtx.logInfo_.l2ApiName = l2Name; | 1033 | opTlsCtx.logInfo_.l2ApiName = l2Name; |
| 1032 | opTlsCtx.logInfo_.l2SequenceCounter = op::internal::OpGetLogSequence(); | 1034 | opTlsCtx.logInfo_.l2SequenceCounter = op::internal::OpGetLogSequence(); |
| @@ -0,0 +1,143 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +#include "gtest/gtest.h" | ||
| 12 | +#include <cstdint> | ||
| 13 | +#include <thread> | ||
| 14 | + | ||
| 15 | +#include "acl/acl.h" | ||
| 16 | +#include "opdev/object.h" | ||
| 17 | +#include "block_pool.h" | ||
| 18 | +#include "block_store.h" | ||
| 19 | +#include "bridge_pool.h" | ||
| 20 | +#include "thread_local_context.h" | ||
| 21 | + | ||
| 22 | +using namespace op; | ||
| 23 | +using namespace op::internal; | ||
| 24 | + | ||
| 25 | +// 大页内存初始化接口(huge_mem.cpp 导出,extern "C"),供 HugeMemRange_NotReported 用例使用 | ||
| 26 | +extern "C" int InitHugeMemThreadLocal(void* arg, bool sync); | ||
| 27 | +extern "C" void UnInitHugeMemThreadLocal(void* arg, bool sync); | ||
| 28 | +extern "C" void ReleaseHugeMem(void* arg, bool sync); | ||
| 29 | + | ||
| 30 | +class CheckDoubleFreeUt : public testing::Test { | ||
| 31 | +protected: | ||
| 32 | + static void SetUpTestCase() {} | ||
| 33 | + static void TearDownTestCase() {} | ||
| 34 | +}; | ||
| 35 | + | ||
| 36 | +// Task 1: CurrentThreadCacheAddr 应返回当前线程 BlockCache 实例地址(稳定且非零) | ||
| 37 | +TEST_F(CheckDoubleFreeUt, CurrentThreadCacheAddr_ReturnsStableNonZeroAddr) | ||
| 38 | +{ | ||
| 39 | + uintptr_t addr1 = BlockCache::CurrentThreadCacheAddr(); | ||
| 40 | + EXPECT_NE(addr1, 0u); | ||
| 41 | + uintptr_t addr2 = BlockCache::CurrentThreadCacheAddr(); | ||
| 42 | + EXPECT_EQ(addr1, addr2); // thread_local 实例稳定 | ||
| 43 | +} | ||
| 44 | + | ||
| 45 | +// nullptr 传入不报 | ||
| 46 | +TEST_F(CheckDoubleFreeUt, Nullptr_ReturnsFalse) { EXPECT_FALSE(CheckDoubleFree(nullptr)); } | ||
| 47 | + | ||
| 48 | +// 野指针(栈上随机内存,magic 几乎不可能匹配)不报 | ||
| 49 | +TEST_F(CheckDoubleFreeUt, WildPointer_MagicMismatch_ReturnsFalse) | ||
| 50 | +{ | ||
| 51 | + uintptr_t buf[16] = {0}; // 全零,magic 不匹配 | ||
| 52 | + void* wild = reinterpret_cast<void*>(buf); | ||
| 53 | + EXPECT_FALSE(CheckDoubleFree(wild)); | ||
| 54 | +} | ||
| 55 | + | ||
| 56 | +// 同线程 alloc → delete 后再 CheckDoubleFree 应返回 true(block 已归还 cache 链表) | ||
| 57 | +// 注:命中 nullptr 分支还是链表节点分支取决于此前同 size class 的 cache 链表状态 | ||
| 58 | +// (首次归还时链表为空命中 nullptr;非空命中链表节点)。两种分支均返回 true,断言不变。 | ||
| 59 | +// 分支级覆盖:实测 SameThreadDoubleFree 命中"链表节点"分支、CrossThreadDoubleFree 命中"nullptr"分支, | ||
| 60 | +// 两个命中分支在本套件中均被实际触发;分支级日志文案断言依赖任务 3 的日志捕获框架,本任务不引入。 | ||
| 61 | +TEST_F(CheckDoubleFreeUt, SameThreadDoubleFree_Detected) | ||
| 62 | +{ | ||
| 63 | + void* addr = Allocate(64); // 走内存池场景(非大页) | ||
| 64 | + ASSERT_NE(addr, nullptr); | ||
| 65 | + DeAllocate(addr); // 首次归还:cacheExt_=nullptr 或链表节点 head+1 | ||
| 66 | + | ||
| 67 | + // 再次检测:应命中(具体分支见上方注释) | ||
| 68 | + EXPECT_TRUE(CheckDoubleFree(addr)); | ||
| 69 | +} | ||
| 70 | + | ||
| 71 | +// 线程 A alloc,线程 B 检查:cacheExt_ 指向 A 的 BlockCache 实例,不应误报 | ||
| 72 | +// 注:子线程 .join() 必须在主线程 DeAllocate 之前,否则触发 spec 7.2 漏报场景(UAF) | ||
| 73 | +TEST_F(CheckDoubleFreeUt, CrossThreadAccess_NoFalsePositive) | ||
| 74 | +{ | ||
| 75 | + void* addr = Allocate(64); | ||
| 76 | + ASSERT_NE(addr, nullptr); | ||
| 77 | + | ||
| 78 | + std::thread([addr]() { | ||
| 79 | + // 跨线程仅检查,不归还:cacheExt_=线程A的BlockCache地址,nextHead->magic_ 不匹配 | ||
| 80 | + EXPECT_FALSE(CheckDoubleFree(addr)); | ||
| 81 | + }).join(); // 必须先 join,确保子线程访问期间 block 仍活跃 | ||
| 82 | + | ||
| 83 | + DeAllocate(addr); // 主线程归还(join 之后才安全) | ||
| 84 | +} | ||
| 85 | + | ||
| 86 | +// 线程 A alloc → 线程 B 归还 → 线程 B 再 CheckDoubleFree:应命中 | ||
| 87 | +// 注:命中分支(nullptr / 链表节点)取决于线程 B 此前在同 size class 的 cache 状态,均返回 true | ||
| 88 | +TEST_F(CheckDoubleFreeUt, CrossThreadDoubleFree_Detected) | ||
| 89 | +{ | ||
| 90 | + void* addr = Allocate(64); | ||
| 91 | + ASSERT_NE(addr, nullptr); | ||
| 92 | + | ||
| 93 | + std::thread([addr]() { | ||
| 94 | + DeAllocate(addr); // 线程 B 归还:cacheExt_ 写入线程 B 的 cache 链表 | ||
| 95 | + EXPECT_TRUE(CheckDoubleFree(addr)); // 线程 B 再检测:应命中 | ||
| 96 | + }).join(); | ||
| 97 | +} | ||
| 98 | + | ||
| 99 | +// 大页内存场景不报(InHugeMemRange 短路) | ||
| 100 | +TEST_F(CheckDoubleFreeUt, HugeMemRange_NotReported) | ||
| 101 | +{ | ||
| 102 | + InitHugeMemThreadLocal(nullptr, false); | ||
| 103 | + void* addr = Allocate(64); // 大页场景 | ||
| 104 | + ASSERT_NE(addr, nullptr); | ||
| 105 | + // 前导断言:确认已进入大页路径(避免用例名误导) | ||
| 106 | + ASSERT_TRUE(op::internal::BlockPool::InHugeMemRange(addr)) << "测试前提:已进入大页路径"; | ||
| 107 | + EXPECT_FALSE(CheckDoubleFree(addr)); // 短路:大页 free 是 no-op | ||
| 108 | + // 仅调 ReleaseHugeMem 即可:其内部 FreeHugeMem 会读 poolIndex_ 归还 baseArray_ 后再置 -1。 | ||
| 109 | + // 若先调 UnInitHugeMemThreadLocal 把 poolIndex_ 置 -1,FreeHugeMem 会因 id 无效早退, | ||
| 110 | + // 不归还 huge block,污染全局 hugeMemArray_ 配额,导致后续 MemoryPoolUt.test_huge_memory 失败。 | ||
| 111 | + ReleaseHugeMem(nullptr, false); | ||
| 112 | +} | ||
| 113 | + | ||
| 114 | +// 冷路径(>64KB)block 释放后:SYS_TAG block 经 FreeImpl 直接 std::free(见 block_pool.h FreeImpl), | ||
| 115 | +// CheckDoubleFree 读已释放内存(UB),magic 几乎不可能匹配 → magic 短路返回 false(漏报,符合设计)。 | ||
| 116 | +// best-effort 验证:依赖 OS 不立即复用该页,若复用且恰好填入 MAGIC 字节会 flaky | ||
| 117 | +TEST_F(CheckDoubleFreeUt, ColdPath_Over64KB_NotReported) | ||
| 118 | +{ | ||
| 119 | + void* addr = Allocate(70 * 1024); // >64KB,走冷路径(SYS_TAG) | ||
| 120 | + ASSERT_NE(addr, nullptr); | ||
| 121 | + DeAllocate(addr); // FreeImpl:SYS_TAG 直接 std::free,block 已归还系统 | ||
| 122 | + EXPECT_FALSE(CheckDoubleFree(addr)); // magic 不匹配短路:漏报,符合设计 | ||
| 123 | +} | ||
| 124 | + | ||
| 125 | +// 冷路径(>64KB)block 分配后未释放:cacheExt_ 仍为 MallocImpl 初始值 NOT_IN_CACHE(见 block_pool.h MallocImpl), | ||
| 126 | +// magic_ == MAGIC(placement new 初始化),命中 NOT_IN_CACHE 短路分支 | ||
| 127 | +TEST_F(CheckDoubleFreeUt, ColdPath_NotInCacheShortCircuit) | ||
| 128 | +{ | ||
| 129 | + void* addr = Allocate(70 * 1024); // >64KB 冷路径,MallocImpl 设 cacheExt_=NOT_IN_CACHE、magic_=MAGIC | ||
| 130 | + ASSERT_NE(addr, nullptr); | ||
| 131 | + EXPECT_FALSE(CheckDoubleFree(addr)); // ext==NOT_IN_CACHE → 短路返回 false | ||
| 132 | + DeAllocate(addr); // 清理(FreeImpl std::free) | ||
| 133 | +} | ||
| 134 | + | ||
| 135 | +// 活跃 block(同线程 alloc 后未释放):cacheExt_ == CurrentThreadCacheAddr | ||
| 136 | +// (CacheAllocImpl/PoolAlloc 分配时写入 this 指针),命中 CurrentThreadCacheAddr 短路分支(防活跃 block 误报) | ||
| 137 | +TEST_F(CheckDoubleFreeUt, ActiveBlock_SameThread_NoFalsePositive) | ||
| 138 | +{ | ||
| 139 | + void* addr = Allocate(64); // ≤64KB 走内存池,block 活跃 | ||
| 140 | + ASSERT_NE(addr, nullptr); | ||
| 141 | + EXPECT_FALSE(CheckDoubleFree(addr)); // ext==CurrentThreadCacheAddr → 短路返回 false | ||
| 142 | + DeAllocate(addr); // 清理 | ||
| 143 | +} | ||
【openlibing.ci】检测到当前PR中存在代码检查告警抑制 1 处,详情见下表,请Committer检视合理性。 / Detected 1 code check alert suppression(s) in this PR, see table below. Committers please review.
bridge_pool.cpp
BlockStore::BlockHeader* nextHead = reinterpret_cast<BlockStore::BlockHeader*>(ext); // NOLINT