diff --git a/CURRENT_TASK.md b/CURRENT_TASK.md index 639d43fe..9e1204a0 100644 --- a/CURRENT_TASK.md +++ b/CURRENT_TASK.md @@ -1,9 +1,11 @@ -# CURRENT TASK – Phase 14 (TinyUltraHot / C2/C3 Ultra-Fast Path) +# CURRENT TASK – Phase 15 (Box Separation / Wrapper Domain Check) -**Date**: 2025-11-15 -**Status**: ✅ TinyUltraHot 実装完了 (+21-36% on C2/C3 workloads), Phase 13 TinyHeapV2 = 安全な stub +**Date**: 2025-11-16 +**Status**: ✅ Phase 15 完了 - Box 境界違反を修正、99.29% BenchMeta 正常解放、crash-free 安定動作 **Owner**: Claude Code → 実装完了 +**Next**: Phase 16 - Tiny 守備範囲の最適化 (512/1024B を Mid に移す A/B テスト) + --- ## 1. 全体の今どこ @@ -169,14 +171,126 @@ typedef struct { - 16/32/64B fixed-size (100K) で ±1% 以内 → hook オーバーヘッドはほぼゼロ。 - `alloc_calls` は 200K まで増えるが `mag_hits=0`(supply なしのため)。 -**要点:** TinyHeapV2 は「壊さず差し込めた L0 stub」。 +**要点:** TinyHeapV2 は「壊さず差し込めた L0 stub」。 これから **supply をどう設計するか** が Phase 13-B の主題。 --- -## 3. 最近のバグ修正・仕様調整(もう触らなくてよい箱) +## 3. Phase 15: Box Separation / Wrapper Domain Check (2025-11-16) ✅ -### 3.1 Tiny / Mid サイズ境界ギャップ修正(完了) +### 3.1 問題発見 - Box 境界違反 + +**ChatGPT 先生の分析**: +- free() wrapper が**無条件で全ポインタを hak_free_at() に流していた** +- BenchMeta の slots[] (外部アロケーション) も CoreAlloc (hakmem) に侵入 ← **箱侵犯!** +- LD_PRELOAD が `__libc_free()` も傍受するため、allocation 側の修正だけでは不十分 + +**症状**: +- Page-aligned pointer (0x...000) が ExternalGuard まで到達 +- SuperSlab registry lookup 失敗 → crash or leak +- 0.84% memory leak (BenchMeta slots[] が解放されていない) + +### 3.2 実装: Wrapper Domain Check + +**戦略** (User 提案 + ChatGPT レビュー): +1. **Allocation 側**: BENCH_META_CALLOC/FREE で分離 (`bench_random_mixed.c`) +2. **Wrapper 側**: Domain check で hakmem vs external を識別 ← **本質的修正** + +**実装詳細** (`core/box/hak_wrappers.inc.h:227-256`): +```c +// Phase 15: Domain check - 1-byte header 検査で hakmem vs external を分離 +uintptr_t offset_in_page = (uintptr_t)ptr & 0xFFF; +if (offset_in_page > 0) { + // Not page-aligned → ptr-1 を安全に読める + uint8_t header = *((uint8_t*)ptr - 1); + if ((header & 0xF0) == 0xA0 || (header & 0xF0) == 0xB0) { + // HEADER_MAGIC found → hakmem Tiny allocation + hak_free_at(ptr, ...); // ✅ CoreAlloc へ + } else { + // No magic → external/BenchMeta + __libc_free(ptr); // ✅ __libc_free へ + } +} else { + // Page-aligned → full classification (safe fallback) + hak_free_at(ptr, ...); +} +``` + +### 3.3 Results + +**Stability** (500K iterations): +- ✅ **No crashes** - crash-free stable operation +- ✅ **99.29% BenchMeta correctly freed** via `__libc_free()` +- ⚠️ **0.71% leak** - page-aligned fallthrough (acceptable tradeoff) + +**Performance** (Random Mixed, 500K iterations): +| Size | Before Fix | After Fix | Change | +|------|-----------|-----------|--------| +| 128B | - | 16.64M ops/s | - | +| 256B | 15.9M ops/s | 16.17M ops/s | **+1.7%** | +| 512B | - | 14.94M ops/s | - | +| 1024B | - | 15.11M ops/s | - | + +**vs System malloc** (256B): 16.17M vs 89.58M ops/s = **18.1%** (5.5倍遅い) + +### 3.4 0.71% Leak 詳細 + +**10K iterations 分析**: +- Total: 10,000 slots[] allocations +- ExternalGuard calls: 71 +- **Leak rate: 0.71%** + +**原因**: +- 71個の slots[] がページ境界に配置 (random, system allocator 依存) +- Page-aligned では ptr-1 を読むと SEGV リスク → full classification へ +- ExternalGuard まで到達 → leak (crash 回避のための安全な tradeoff) + +**99.29% の成功**: +- ヘッダチェック成功 → external と認識 +- `__libc_free()` へ正しくルーティング ✅ + +### 3.5 Files Modified + +1. **core/box/hak_wrappers.inc.h** (lines 227-256) + - Domain check logic with 1-byte header inspection + - Split routing: hakmem → hak_free_at(), external → __libc_free() + +2. **core/box/external_guard_box.h** (lines 121-145) + - Conservative unknown pointer handling (leak instead of crash) + - Enhanced debug logging + +3. **core/hakmem_super_registry.h** (line 28) + - SUPER_MAX_PROBE: 8 → 32 (hash collision tolerance) + +4. **bench_random_mixed.c** (lines 15-25, 46, 99) + - BENCH_META_CALLOC/FREE macros (allocation side separation) + +### 3.6 Lessons Learned + +**LD_PRELOAD 傍受範囲**: +- `__libc_free()` も傍受される (内部からの呼び出しも wrapper 経由) +- → Wrapper 自体に defense-in-depth が必要 + +**Box 分離の Defense in Depth**: +1. Allocation 側: BENCH_META_CALLOC で分離 +2. Wrapper 側: Domain check で二重防御 +3. Last resort: ExternalGuard で conservative leak + +**Page 境界 edge case**: +- ptr-1 を読むと SEGV リスク (0.71%) +- Tradeoff: leak (acceptable) vs crash (fatal) +- 安全性を優先 ✅ + +### 3.7 Documentation + +- **PHASE15_WRAPPER_DOMAIN_CHECK_FIX.md** - 完全な実装解説 + User contribution 記録 +- **PHASE15_REGISTRY_LOOKUP_INVESTIGATION.md** - SuperSlab registry 調査過程 + +--- + +## 4. 最近のバグ修正・仕様調整(もう触らなくてよい箱) + +### 4.1 Tiny / Mid サイズ境界ギャップ修正(完了) - 以前: - `TINY_MAX_SIZE = 1024` / `MID_MIN_SIZE = 8192` で 1KB–8KB が誰の担当でもなく mmap 直行。 diff --git a/perf.data.off b/perf.data.off new file mode 100644 index 00000000..c5936f78 Binary files /dev/null and b/perf.data.off differ diff --git a/perf.data.off.old b/perf.data.off.old new file mode 100644 index 00000000..5b874da5 Binary files /dev/null and b/perf.data.off.old differ diff --git a/perf.data.on b/perf.data.on new file mode 100644 index 00000000..6d6eafb5 Binary files /dev/null and b/perf.data.on differ diff --git a/perf.data.on.old b/perf.data.on.old new file mode 100644 index 00000000..bc1cde6e Binary files /dev/null and b/perf.data.on.old differ