feat(memhub): 分配重叠即时检测

SlotsBugTest 崩溃实锤:同一块被重复分配(记录表同 ptr 两条,[353]sz=7 与 [10298]sz=72)——7B 字符串缓冲与 72B Token 重叠,Token 初始化覆盖 buf 导致越界写,KwEntry cargs 只写 0/40 根因。

根因指向 MemBuddy free list 损坏(active 块混入 free list)。新增检测:每次 alloc 前将新块地址与 active 记录比对,命中即打印两个 size 并 exit(1),在第一次重叠处拦截,便于回溯具体分配调用。
This commit is contained in:
TermiNexus committed 2026-08-25 16:01:07 +08:00
1 parent 49feb6a2b4
commit 74ed6f2dbc
1 file changed
+14
+14
View File
@@ -668,6 +668,20 @@ class MemBuddy(MemManager):
m_t_d: t.CUInt16T | t.CPtr = (t.CUInt16T | t.CPtr)(t.CVoid(tail_d, t.CPtr))
m_t_d[0] = t.CUInt16T(MEMBUDDY_DET_MARKER)
if self._det_records is not None:
# 分配重叠即时检测:新块地址是否已在 active 记录(同一块重复
# 分配实锤——SlotsBugTest 7B buf 与 72B Token 同 ptr、cargs
# 只写 0/40 根因)。重叠 = MemBuddy free list 损坏(active 块
# 混入 free list),在第一次重叠处拦截并打印两个 size。
ff_d: t.CUInt64T = t.CUInt64T(1) << 63
dup_i: t.CSizeT = 0
while dup_i < self._det_count:
drp: t.CUInt64T = self._det_records[dup_i * 2]
drs: t.CUInt64T = self._det_records[dup_i * 2 + 1]
if drp == base_ret_d + 2 and (drs & ff_d) == 0:
stdio.printf("[DET] 分配重叠: 新alloc(size=%llu)与记录[%zu](sz=%llu)同地址 ptr=%llu(同一块重复分配,free list 损坏)\n",
t.CUInt64T(size), dup_i, drs & (ff_d - 1), base_ret_d + 2)
stdlib.exit(1)
dup_i += 1
if self._det_count >= self._det_cap:
# 记录表满:stdlib.realloc 翻倍扩容(防 is_alive/_det_probe
# 因容量耗尽漏检——SlotsBugTest 编译 includes 超 2M 次 alloc,