feat(memhub): 读取侧存活校验 is_alive - 补 detection 写入检测盲区:struct_ctor 读 cargs 前校验 cl.args 是否在记录表 active 记录中,不在即 [DET] 读取悬空 打印 cargs 值并终止;拦截 use-after-free 读取(SlotsBugTest KwEntry cargs=cl.args 悬空根因),detection==0 时 is_alive 恒返回 1 不拦截

This commit is contained in:
TermiNexus committed 2026-08-25 15:31:39 +08:00
1 parent 1de7c8d2ca
commit fefc1e8343
2 files changed
+32

No files matched your search

+9
View File
@@ -127,6 +127,15 @@ class CallGenCtor(CallGenBase.CallGenBase):
OOP 结构体:alloca → call __before_init__(ptr) → call __init__(ptr, args) 或 store 参数 → load
普通结构体:alloca → zero + defaults → store 参数 → load
"""
# [DET] 读取侧存活校验(detection 模式启用):cargs(cl.args)必须指向
# 存活块——不在记录表(或已 free)= use-after-free 读取(SlotsBugTest
# KwEntry cargs 悬空根因定位)。is_alive 内部处理 detection==0 时不拦截,
# 默认关闭无开销。补 _det_probe 写入侧检测的盲区。
if cargs is not None and pool is not None:
if pool.is_alive(cargs) == 0:
stdio.printf("[DET] 读取悬空: struct-ctor cargs=%llu 不在存活记录(cl.args 悬空 = use-after-free 读取)\n",
t.CUInt64T(cargs))
stdlib.exit(1)
# 创建临时 alloca
# 确保跨模块结构体的完整定义在当前模块中可用(供 alloca 分配空间)
self.Gen.StructGen.ensure_struct_def_in_module(pool, mod, struct_ty)
+23
View File
@@ -786,6 +786,29 @@ class MemBuddy(MemManager):
self.free(ptr)
return new_ptr
# ============================================================
# [DET] is_alive - 读取侧存活校验:ptr 是否在当前记录表的 active
# 记录中(alloc 后未 free)。返回 1=存活 / 0=悬空(不在记录或已
# free)。detection 关闭或记录表不可用时返回 1(不拦截)。
# 补 _det_probe 盲区:写入侧检测(标记/0xCD)拦不住 use-after-free
# 读取(如 SlotsBugTest KwEntry cargs=cl.args 悬空指向复用块)。
# ============================================================
def is_alive(self, ptr: t.CVoid | t.CPtr) -> t.CInt:
if self.detection == 0:
return 1
if self._det_records is None:
return 1
target: t.CUInt64T = t.CUInt64T(ptr)
free_flag: t.CUInt64T = t.CUInt64T(1) << 63
i: t.CSizeT = 0
while i < self._det_count:
rp: t.CUInt64T = self._det_records[i * 2]
rs: t.CUInt64T = self._det_records[i * 2 + 1]
if rp == target and (rs & free_flag) == 0:
return 1
i += 1
return 0
# ============================================================
# [DET] _det_probe - 分配检测探测:遍历记录表,验证每个存活块的
# 后标记(返回 ptr + size 处 2 字节)仍为 0xCDCD。