Files
hakmem/POINTER_CONVERSION_FIX.patch
Moe Charm (CI) 72b38bc994 Phase E3-FINAL: Fix Box API offset bugs - ALL classes now use correct offsets
## Root Cause Analysis (GPT5)

**Physical Layout Constraints**:
- Class 0: 8B = [1B header][7B payload] → offset 1 = 9B needed =  IMPOSSIBLE
- Class 1-6: >=16B = [1B header][15B+ payload] → offset 1 =  POSSIBLE
- Class 7: 1KB → offset 0 (compatibility)

**Correct Specification**:
- HAKMEM_TINY_HEADER_CLASSIDX != 0:
  - Class 0, 7: next at offset 0 (overwrites header when on freelist)
  - Class 1-6: next at offset 1 (after header)
- HAKMEM_TINY_HEADER_CLASSIDX == 0:
  - All classes: next at offset 0

**Previous Bug**:
- Attempted "ALL classes offset 1" unification
- Class 0 with offset 1 caused immediate SEGV (9B > 8B block size)
- Mixed 2-arg/3-arg API caused confusion

## Fixes Applied

### 1. Restored 3-Argument Box API (core/box/tiny_next_ptr_box.h)
```c
// Correct signatures
void tiny_next_write(int class_idx, void* base, void* next_value)
void* tiny_next_read(int class_idx, const void* base)

// Correct offset calculation
size_t offset = (class_idx == 0 || class_idx == 7) ? 0 : 1;
```

### 2. Updated 123+ Call Sites Across 34 Files
- hakmem_tiny_hot_pop_v4.inc.h (4 locations)
- hakmem_tiny_fastcache.inc.h (3 locations)
- hakmem_tiny_tls_list.h (12 locations)
- superslab_inline.h (5 locations)
- tiny_fastcache.h (3 locations)
- ptr_trace.h (macro definitions)
- tls_sll_box.h (2 locations)
- + 27 additional files

Pattern: `tiny_next_read(base)` → `tiny_next_read(class_idx, base)`
Pattern: `tiny_next_write(base, next)` → `tiny_next_write(class_idx, base, next)`

### 3. Added Sentinel Detection Guards
- tiny_fast_push(): Block nodes with sentinel in ptr or ptr->next
- tls_list_push(): Block nodes with sentinel in ptr or ptr->next
- Defense-in-depth against remote free sentinel leakage

## Verification (GPT5 Report)

**Test Command**: `./out/release/bench_random_mixed_hakmem --iterations=70000`

**Results**:
-  Main loop completed successfully
-  Drain phase completed successfully
-  NO SEGV (previous crash at iteration 66151 is FIXED)
- ℹ️ Final log: "tiny_alloc(1024) failed" is normal fallback to Mid/ACE layers

**Analysis**:
- Class 0 immediate SEGV:  RESOLVED (correct offset 0 now used)
- 66K iteration crash:  RESOLVED (offset consistency fixed)
- Box API conflicts:  RESOLVED (unified 3-arg API)

## Technical Details

### Offset Logic Justification
```
Class 0:  8B block → next pointer (8B) fits ONLY at offset 0
Class 1: 16B block → next pointer (8B) fits at offset 1 (after 1B header)
Class 2: 32B block → next pointer (8B) fits at offset 1
...
Class 6: 512B block → next pointer (8B) fits at offset 1
Class 7: 1024B block → offset 0 for legacy compatibility
```

### Files Modified (Summary)
- Core API: `box/tiny_next_ptr_box.h`
- Hot paths: `hakmem_tiny_hot_pop*.inc.h`, `tiny_fastcache.h`
- TLS layers: `hakmem_tiny_tls_list.h`, `hakmem_tiny_tls_ops.h`
- SuperSlab: `superslab_inline.h`, `tiny_superslab_*.inc.h`
- Refill: `hakmem_tiny_refill.inc.h`, `tiny_refill_opt.h`
- Free paths: `tiny_free_magazine.inc.h`, `tiny_superslab_free.inc.h`
- Documentation: Multiple Phase E3 reports

## Remaining Work

None for Box API offset bugs - all structural issues resolved.

Future enhancements (non-critical):
- Periodic `grep -R '*(void**)' core/` to detect direct pointer access violations
- Enforce Box API usage via static analysis
- Document offset rationale in architecture docs

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-13 06:50:20 +09:00

342 lines
11 KiB
Diff

# Pointer Conversion Bug Fix Patch
# Root Cause: DOUBLE CONVERSION (USER → BASE executed twice)
# Solution: Single conversion at API boundary (hak_free_at)
## Summary of Changes
1. **hak_free_api.inc.h**: Add USER → BASE conversion at API boundary (2 locations)
2. **tiny_superslab_free.inc.h**: Remove DOUBLE CONVERSION (delete line 28)
3. **hakmem_tiny_free.inc**: Update call sites to pass USER pointer (2 locations)
---
## File 1: core/box/hak_free_api.inc.h
### Change 1: PTR_KIND_TINY_HEADER (line 102-121)
BEFORE:
```c
case PTR_KIND_TINY_HEADER: {
// C0-C6: Has 1-byte header, class_idx already determined by Front Gate
// Fast path: Use class_idx directly without SuperSlab lookup
hak_free_route_log("tiny_header", ptr);
#if HAKMEM_TINY_HEADER_CLASSIDX
// Use ultra-fast free path with pre-determined class_idx
if (__builtin_expect(hak_tiny_free_fast_v2(ptr), 1)) {
#if !HAKMEM_BUILD_RELEASE
hak_free_v2_track_fast();
#endif
goto done;
}
// Fallback to slow path if TLS cache full
#if !HAKMEM_BUILD_RELEASE
hak_free_v2_track_slow();
#endif
#endif
hak_tiny_free(ptr);
goto done;
}
```
AFTER:
```c
case PTR_KIND_TINY_HEADER: {
// C0-C6: Has 1-byte header, class_idx already determined by Front Gate
// Fast path: Use class_idx directly without SuperSlab lookup
hak_free_route_log("tiny_header", ptr);
#if HAKMEM_TINY_HEADER_CLASSIDX
// Use ultra-fast free path with pre-determined class_idx
if (__builtin_expect(hak_tiny_free_fast_v2(ptr), 1)) {
#if !HAKMEM_BUILD_RELEASE
hak_free_v2_track_fast();
#endif
goto done;
}
// Fallback to slow path if TLS cache full
#if !HAKMEM_BUILD_RELEASE
hak_free_v2_track_slow();
#endif
#endif
// ✅ FIX: hak_tiny_free expects USER pointer (no conversion needed here)
// Internal paths will handle BASE pointer conversion as needed
hak_tiny_free(ptr);
goto done;
}
```
**Rationale**: hak_tiny_free_fast_v2 handles USER pointers correctly. hak_tiny_free also accepts USER pointers and converts internally when needed. No change needed here - just clarifying comment.
### Change 2: PTR_KIND_TINY_HEADERLESS (line 123-129)
BEFORE:
```c
case PTR_KIND_TINY_HEADERLESS: {
// C7: Headerless 1KB blocks, SuperSlab + slab_idx provided by Registry
// Medium path: Use Registry result, no header read needed
hak_free_route_log("tiny_headerless", ptr);
hak_tiny_free(ptr);
goto done;
}
```
AFTER:
```c
case PTR_KIND_TINY_HEADERLESS: {
// C7: Headerless 1KB blocks, SuperSlab + slab_idx provided by Registry
// Medium path: Use Registry result, no header read needed
hak_free_route_log("tiny_headerless", ptr);
// ✅ FIX: hak_tiny_free expects USER pointer (no conversion needed here)
// C7 now has headers in Phase E1, treat same as other classes
hak_tiny_free(ptr);
goto done;
}
```
**Rationale**: Same as above. hak_tiny_free will handle conversion when calling superslab free.
---
## File 2: core/tiny_superslab_free.inc.h
### Change: Remove DOUBLE CONVERSION (line 28)
BEFORE:
```c
static inline void hak_tiny_free_superslab(void* ptr, SuperSlab* ss) {
// Route trace: count SuperSlab free entries (diagnostics only)
extern _Atomic uint64_t g_free_ss_enter;
atomic_fetch_add_explicit(&g_free_ss_enter, 1, memory_order_relaxed);
ROUTE_MARK(16); // free_enter
HAK_DBG_INC(g_superslab_free_count); // Phase 7.6: Track SuperSlab frees
// Get slab index (supports 1MB/2MB SuperSlabs)
int slab_idx = slab_index_for(ss, ptr);
size_t ss_size = (size_t)1ULL << ss->lg_size;
uintptr_t ss_base = (uintptr_t)ss;
if (__builtin_expect(slab_idx < 0, 0)) {
uintptr_t aux = tiny_remote_pack_diag(0xBAD1u, ss_base, ss_size, (uintptr_t)ptr);
tiny_debug_ring_record(TINY_RING_EVENT_REMOTE_INVALID, (uint16_t)ss->size_class, ptr, aux);
if (g_tiny_safe_free_strict) { raise(SIGUSR2); return; }
return;
}
TinySlabMeta* meta = &ss->slabs[slab_idx];
// Phase E1-CORRECT: ALL classes (C0-C7) have 1-byte header
void* base = (void*)((uint8_t*)ptr - 1);
// Debug: Log first C7 alloc/free for path verification
if (ss->size_class == 7) {
static _Atomic int c7_free_count = 0;
int count = atomic_fetch_add_explicit(&c7_free_count, 1, memory_order_relaxed);
if (count == 0) {
#if !HAKMEM_BUILD_RELEASE && HAKMEM_DEBUG_VERBOSE
fprintf(stderr, "[C7_FIRST_FREE] ptr=%p base=%p slab_idx=%d\n", ptr, base, slab_idx);
#endif
}
}
```
AFTER:
```c
static inline void hak_tiny_free_superslab(void* ptr, SuperSlab* ss) {
// Route trace: count SuperSlab free entries (diagnostics only)
extern _Atomic uint64_t g_free_ss_enter;
atomic_fetch_add_explicit(&g_free_ss_enter, 1, memory_order_relaxed);
ROUTE_MARK(16); // free_enter
HAK_DBG_INC(g_superslab_free_count); // Phase 7.6: Track SuperSlab frees
// ✅ FIX: Convert USER → BASE at entry point (single conversion)
// Phase E1-CORRECT: ALL classes (C0-C7) have 1-byte header
// ptr = USER pointer (storage+1), base = BASE pointer (storage)
void* base = (void*)((uint8_t*)ptr - 1);
// Get slab index (supports 1MB/2MB SuperSlabs)
// CRITICAL: Use BASE pointer for slab_index calculation!
int slab_idx = slab_index_for(ss, base);
size_t ss_size = (size_t)1ULL << ss->lg_size;
uintptr_t ss_base = (uintptr_t)ss;
if (__builtin_expect(slab_idx < 0, 0)) {
uintptr_t aux = tiny_remote_pack_diag(0xBAD1u, ss_base, ss_size, (uintptr_t)ptr);
tiny_debug_ring_record(TINY_RING_EVENT_REMOTE_INVALID, (uint16_t)ss->size_class, ptr, aux);
if (g_tiny_safe_free_strict) { raise(SIGUSR2); return; }
return;
}
TinySlabMeta* meta = &ss->slabs[slab_idx];
// Debug: Log first C7 alloc/free for path verification
if (ss->size_class == 7) {
static _Atomic int c7_free_count = 0;
int count = atomic_fetch_add_explicit(&c7_free_count, 1, memory_order_relaxed);
if (count == 0) {
#if !HAKMEM_BUILD_RELEASE && HAKMEM_DEBUG_VERBOSE
fprintf(stderr, "[C7_FIRST_FREE] ptr=%p base=%p slab_idx=%d\n", ptr, base, slab_idx);
#endif
}
}
```
**Key Changes**:
1. Move `void* base = (void*)((uint8_t*)ptr - 1);` to TOP of function (line 10-13)
2. Add comment explaining USER → BASE conversion
3. Change `slab_index_for(ss, ptr)` to `slab_index_for(ss, base)` ← **CRITICAL FIX!**
4. Remove later `void* base = ...` line (was line 28, causing DOUBLE CONVERSION)
**Rationale**:
- Perform USER → BASE conversion ONCE at entry
- Use BASE pointer for ALL internal operations (slab_index, alignment checks, freelist push)
- Fixes C7 alignment error: delta % 1024 now == 0 instead of 1
---
## File 3: core/hakmem_tiny_free.inc
### Change 1: Direct SuperSlab free path (line ~470)
BEFORE:
```c
if (ss && ss->magic == SUPERSLAB_MAGIC) {
// BUGFIX: Validate size_class before using as array index (prevents OOB)
if (__builtin_expect(ss->size_class < 0 || ss->size_class >= TINY_NUM_CLASSES, 0)) {
tiny_debug_ring_record(TINY_RING_EVENT_REMOTE_INVALID, 0xF2, ptr, (uintptr_t)ss->size_class);
if (g_tiny_safe_free_strict) { raise(SIGUSR2); return; }
return;
}
// Direct SuperSlab free (avoid second lookup TOCTOU)
hak_tiny_free_superslab(ptr, ss);
HAK_STAT_FREE(ss->size_class);
return;
}
```
AFTER:
```c
if (ss && ss->magic == SUPERSLAB_MAGIC) {
// BUGFIX: Validate size_class before using as array index (prevents OOB)
if (__builtin_expect(ss->size_class < 0 || ss->size_class >= TINY_NUM_CLASSES, 0)) {
tiny_debug_ring_record(TINY_RING_EVENT_REMOTE_INVALID, 0xF2, ptr, (uintptr_t)ss->size_class);
if (g_tiny_safe_free_strict) { raise(SIGUSR2); return; }
return;
}
// Direct SuperSlab free (avoid second lookup TOCTOU)
// ✅ FIX: Pass USER pointer (hak_tiny_free_superslab will convert to BASE)
hak_tiny_free_superslab(ptr, ss);
HAK_STAT_FREE(ss->size_class);
return;
}
```
**Rationale**: No code change, just clarifying comment. hak_tiny_free_superslab now handles USER → BASE conversion internally.
### Change 2: Free with slab path (line ~173 in hak_tiny_free_superslab call)
Search for other calls to `hak_tiny_free_superslab` in hakmem_tiny_free.inc and verify they pass USER pointers.
**Expected locations**:
- Line ~108 in `hak_tiny_free_with_slab`: Already passes USER pointer via `ptr` parameter ✅
- Line ~173 (same file): Check and add comment if needed
**No code changes needed** - just verify consistency.
---
## Verification Steps
### 1. Build Test
```bash
cd /mnt/workdisk/public_share/hakmem
./build.sh bench_fixed_size_hakmem
```
Expected: Clean build, no warnings
### 2. Alignment Test (C7 1KB blocks)
```bash
./out/release/bench_fixed_size_hakmem 10000 1024 128
```
Expected output:
```
BEFORE FIX:
[C7_ALIGN_CHECK_FAIL] delta%blk=1 ← OFF BY ONE
AFTER FIX:
No [C7_ALIGN_CHECK_FAIL] errors
Performance: ~2.7M ops/s (same as before)
```
### 3. Stress Test (All sizes)
```bash
# Test all tiny classes
for size in 8 16 32 64 128 256 512 1024; do
echo "Testing size=$size"
./out/release/bench_fixed_size_hakmem 100000 $size 128
done
```
Expected: All tests pass, no alignment errors
### 4. Grep Audit (Verify single conversion point)
```bash
# Check USER → BASE conversions
grep -rn "(uint8_t\*)ptr - 1" core/tiny_superslab_free.inc.h
# Expected: 1 match (at line ~13, entry point conversion)
```
### 5. Performance Benchmark
```bash
# Before and after comparison
./out/release/bench_random_mixed_hakmem 100000 256 42
```
Expected: Performance unchanged (< 1% difference)
---
## Rollback Plan
If the fix causes issues:
1. Revert File 2 (tiny_superslab_free.inc.h):
- Move `void* base = ...` back to line 28 (after slab_idx calculation)
- Change `slab_index_for(ss, base)` back to `slab_index_for(ss, ptr)`
2. Revert comments in Files 1 and 3 (no functional changes)
3. Re-run old binary for immediate workaround
---
## Additional Notes
### Why slab_index_for needs BASE pointer
```c
int slab_index_for(SuperSlab* ss, void* ptr) {
uintptr_t base = (uintptr_t)ss;
uintptr_t offset = (uintptr_t)ptr - base;
int slab_idx = (int)(offset / SLAB_SIZE);
return slab_idx;
}
```
**Issue**: If ptr = USER (storage+1), offset is off by 1, potentially causing wrong slab_idx for blocks at slab boundaries!
**Fix**: Pass BASE pointer (storage) to ensure correct offset calculation.
### Performance Impact
**None**. Conversion count unchanged:
- Before: 1 conversion at line 28 (WRONG location)
- After: 1 conversion at line 13 (CORRECT location)
Same number of instructions, just moved earlier in the function.
### Future-Proofing
All internal functions now consistently use BASE pointers:
- `slab_index_for(ss, base)` ✅
- `tiny_slab_base_for(ss, slab_idx)` returns BASE ✅
- `meta->freelist = base` ✅
- `ss_remote_push(ss, slab_idx, base)` ✅
USER pointers only exist at public API boundaries (malloc/free).