From 3abcd7bb77cfd3e48b9a4b38357c92a3a349523c Mon Sep 17 00:00:00 2001 From: yeshanshan Date: Thu, 16 Oct 2025 10:02:50 +0800 Subject: [PATCH] fix: improve vtable ownership verification in hasVtable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Enhanced hasVtable function to verify vtable pointer matches recorded ghost vtable 2. Added address reuse detection with detailed warning logging 3. Implemented cleanup of stale mappings when address reuse is detected 4. Fixed potential false positives when memory addresses are reused by different objects Log: Fixed vtable hook detection reliability when memory addresses are reused Influence: 1. Test vtable hooking with objects that have similar memory addresses 2. Verify hasVtable returns correct results after object destruction and recreation 3. Test memory reuse scenarios with multiple object allocations 4. Check warning logs for address reuse detection 5. Verify no memory leaks when stale mappings are cleaned up 6. Test vtable hook stability in long-running applications fix: 改进 hasVtable 函数中的虚表所有权验证 1. 增强 hasVtable 函数以验证虚表指针是否与记录的 ghost 虚表匹配 2. 添加地址重用检测和详细的警告日志记录 3. 在检测到地址重用时实现陈旧映射的清理 4. 修复当内存地址被不同对象重用时可能出现的误判问题 Log: 修复了内存地址被重用时虚表钩子检测的可靠性问题 Influence: 1. 测试具有相似内存地址的对象的虚表钩子功能 2. 验证对象销毁和重新创建后 hasVtable 返回正确结果 3. 测试多个对象分配时的内存重用场景 4. 检查地址重用检测的警告日志 5. 验证清理陈旧映射时没有内存泄漏 6. 测试长时间运行应用程序中虚表钩子的稳定性 PMS: BUG-329331 BUG-334591 --- src/vtablehook.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/vtablehook.cpp b/src/vtablehook.cpp index e6920dee..9da45d1f 100644 --- a/src/vtablehook.cpp +++ b/src/vtablehook.cpp @@ -238,12 +238,29 @@ bool VtableHook::ensureVtable(const void *obj, std::function destoryObj * \brief VtableHook::hasVtable 对象的虚表已经被覆盖时返回true,否则返回false * \param obj * \return + * + * 修复: 不仅检查地址是否在映射表中,还要验证当前对象的 vtable 是否与记录的 ghost vtable 匹配 + * 防止地址重用导致误判 */ bool VtableHook::hasVtable(const void *obj) { quintptr **_obj = (quintptr**)(obj); - return objToGhostVfptr.contains(_obj); + // 验证 vtable 是否匹配 + quintptr *ghost_vtable = objToGhostVfptr.value(obj); + if (!ghost_vtable) { + return false; + } + + // 检查当前对象的 vtable 指针是否指向我们记录的 ghost vtable + if (*_obj != adjustToEntry(ghost_vtable)) { + // vtable 不匹配,说明地址被重用了 + qCDebug(vtableHook) << "hasVtable: vtable mismatch! Address reused by different object." + << "obj:" << QString("0x%1").arg((quintptr)obj, 0, 16); + return false; + } + + return true; } void VtableHook::resetVtable(const void *obj)