Add Tiny Alloc Gatekeeper Box for unified malloc entry point

Core Changes:
- New file: core/box/tiny_alloc_gate_box.h
  * Thin wrapper around malloc_tiny_fast() with diagnostic hooks
  * TinyAllocGateContext structure for size/class_idx/user/base/bridge information
  * tiny_alloc_gate_diag_enabled() - ENV-controlled diagnostic mode
  * tiny_alloc_gate_validate() - Validates class_idx/header/meta consistency
  * tiny_alloc_gate_fast() - Main gatekeeper function
  * Zero performance impact when diagnostics disabled

- Modified: core/box/hak_wrappers.inc.h
  * Added #include "tiny_alloc_gate_box.h" (line 35)
  * Integrated gatekeeper into malloc wrapper (lines 198-200)
  * Diagnostic mode via HAKMEM_TINY_ALLOC_GATE_DIAG env var

Design Rationale:
- Complements Free Gatekeeper Box: Together they provide entry/exit hooks
- Validates allocation consistency at malloc time
- Enables Bridge + BASE/USER conversion validation in debug mode
- Maintains backward compatibility: existing behavior unchanged

Validation Features:
- tiny_ptr_bridge_classify_raw() - Verifies Superslab/Slab/meta lookup
- Header vs meta class consistency check (rate-limited, 8 msgs max)
- class_idx validation via hak_tiny_size_to_class()
- All validation logged but non-blocking (observation points for Guard)

Testing:
- All smoke tests pass (10M malloc/free cycles, pool TLS, real programs)
- Diagnostic mode validated with HAKMEM_TINY_ALLOC_GATE_DIAG=1
- No regressions in existing functionality
- Verified via Task agent (PASS verdict)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Moe Charm (CI)
2025-12-04 12:06:14 +09:00
parent de9c512971
commit 291c84a1a7
2 changed files with 168 additions and 2 deletions

View File

@ -31,7 +31,8 @@ void* realloc(void* ptr, size_t size) {
#include "../ptr_trace.h" // Debug: pointer trace immediate dump on libc fallback
#include "front_gate_classifier.h" // Box FG: pointer classification (header/reg)
#include "../hakmem_pool.h" // Mid registry lookup (failsafe for headerless Mid)
#include "../front/malloc_tiny_fast.h" // Phase 26: Front Gate Unification
#include "../front/malloc_tiny_fast.h" // Phase 26: Front Gate Unification (Tiny fast alloc)
#include "tiny_alloc_gate_box.h" // Tiny Alloc Gatekeeper Box (BASE/USER+Bridge 入口)
#include "tiny_front_config_box.h" // Phase 4-Step3: Compile-time config for dead code elimination
#include "wrapper_env_box.h" // Wrapper env cache (step trace / LD safe / free trace)
#include "../hakmem_internal.h" // AllocHeader helpers for diagnostics
@ -194,7 +195,9 @@ void* malloc(size_t size) {
if (wcfg->step_trace && size == 33000) write(2, "STEP:5 Unified Gate check\n", 26);
if (size <= tiny_get_max_size()) {
if (wcfg->step_trace && size == 33000) write(2, "STEP:5.1 Inside Unified\n", 24);
void* ptr = malloc_tiny_fast(size);
// Tiny Alloc Gate Box: malloc_tiny_fast() の薄いラッパ
// (診断 OFF 時は従来どおりの挙動・コスト)
void* ptr = tiny_alloc_gate_fast(size);
if (__builtin_expect(ptr != NULL, 1)) {
g_hakmem_lock_depth--;
if (wcfg->step_trace && size == 33000) write(2, "RET:TinyFast\n", 13);