2025-11-07 01:27:04 +09:00
|
|
|
#include "free_local_box.h"
|
|
|
|
|
#include "free_publish_box.h"
|
|
|
|
|
#include "hakmem_tiny.h"
|
Phase E1-CORRECT: Fix USER/BASE pointer conversion bugs in slab_index_for calls
CRITICAL BUG FIX: Phase E1 introduced 1-byte headers for ALL size classes (C0-C7),
changing the pointer contract. However, many locations still called slab_index_for()
with USER pointers (storage+1) instead of BASE pointers (storage), causing off-by-one
slab index calculations that corrupted memory.
Root Cause:
- USER pointer = BASE + 1 (returned by malloc, points past header)
- BASE pointer = storage start (where 1-byte header is written)
- slab_index_for() expects BASE pointer for correct slab boundary calculations
- Passing USER pointer → wrong slab_idx → wrong metadata → freelist corruption
Impact Before Fix:
- bench_random_mixed crashes at ~14K iterations with SEGV
- Massive C7 alignment check failures (wrong slab classification)
- Memory corruption from writing to wrong slab freelists
Fixes Applied (8 locations):
1. core/hakmem_tiny_free.inc:137
- Added USER→BASE conversion before slab_index_for()
2. core/hakmem_tiny_ultra_simple.inc:148
- Added USER→BASE conversion before slab_index_for()
3. core/tiny_free_fast.inc.h:220
- Added USER→BASE conversion before slab_index_for()
4-5. core/tiny_free_magazine.inc.h:126,315
- Added USER→BASE conversion before slab_index_for() (2 locations)
6. core/box/free_local_box.c:14,22,62
- Added USER→BASE conversion before slab_index_for()
- Fixed delta calculation to use BASE instead of USER
- Fixed debug logging to use BASE instead of USER
7. core/hakmem_tiny.c:448,460,473 (tiny_debug_track_alloc_ret)
- Added USER→BASE conversion before slab_index_for() (2 calls)
- Fixed delta calculation to use BASE instead of USER
- This function is called on EVERY allocation in debug builds
Results After Fix:
✅ bench_random_mixed stable up to 66K iterations (~4.7x improvement)
✅ C7 alignment check failures eliminated (was: 100% failure rate)
✅ Front Gate "Unknown" classification dropped to 0% (was: 1.67%)
✅ No segfaults for workloads up to ~33K allocations
Remaining Issue:
❌ Segfault still occurs at iteration 66152 (allocs=33137, frees=33014)
- Different bug from USER/BASE conversion issues
- Likely capacity/boundary condition (further investigation needed)
Testing:
- bench_random_mixed_hakmem 1K-66K iterations: PASS
- bench_random_mixed_hakmem 67K+ iterations: FAIL (different bug)
- bench_fixed_size_hakmem 200K iterations: PASS
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-13 05:21:36 +09:00
|
|
|
#include "tiny_next_ptr_box.h" // Phase E1-CORRECT: Box API
|
2025-11-21 04:56:48 +09:00
|
|
|
#include "ss_hot_cold_box.h" // Phase 12-1.1: EMPTY slab marking
|
2025-11-21 23:00:24 +09:00
|
|
|
#include "tiny_region_id.h" // HEADER_MAGIC / HEADER_CLASS_MASK
|
|
|
|
|
|
|
|
|
|
// Local prototypes (fail-fast helpers live in tiny_failfast.c)
|
|
|
|
|
int tiny_refill_failfast_level(void);
|
|
|
|
|
void tiny_failfast_abort_ptr(const char* stage,
|
|
|
|
|
SuperSlab* ss,
|
|
|
|
|
int slab_idx,
|
|
|
|
|
void* ptr,
|
|
|
|
|
const char* reason);
|
|
|
|
|
void tiny_failfast_log(const char* stage,
|
|
|
|
|
int class_idx,
|
|
|
|
|
SuperSlab* ss,
|
|
|
|
|
TinySlabMeta* meta,
|
|
|
|
|
void* ptr,
|
|
|
|
|
void* prev);
|
2025-11-07 01:27:04 +09:00
|
|
|
|
2025-12-01 13:47:23 +09:00
|
|
|
int tiny_free_local_box(SuperSlab* ss, int slab_idx, TinySlabMeta* meta, void* ptr, uint32_t my_tid) {
|
2025-11-07 01:27:04 +09:00
|
|
|
extern _Atomic uint64_t g_free_local_box_calls;
|
|
|
|
|
atomic_fetch_add_explicit(&g_free_local_box_calls, 1, memory_order_relaxed);
|
2025-12-01 13:47:23 +09:00
|
|
|
if (!(ss && ss->magic == SUPERSLAB_MAGIC)) return 0;
|
|
|
|
|
if (slab_idx < 0 || slab_idx >= ss_slabs_capacity(ss)) return 0;
|
2025-11-07 01:27:04 +09:00
|
|
|
(void)my_tid;
|
|
|
|
|
|
Phase E1-CORRECT: Fix USER/BASE pointer conversion bugs in slab_index_for calls
CRITICAL BUG FIX: Phase E1 introduced 1-byte headers for ALL size classes (C0-C7),
changing the pointer contract. However, many locations still called slab_index_for()
with USER pointers (storage+1) instead of BASE pointers (storage), causing off-by-one
slab index calculations that corrupted memory.
Root Cause:
- USER pointer = BASE + 1 (returned by malloc, points past header)
- BASE pointer = storage start (where 1-byte header is written)
- slab_index_for() expects BASE pointer for correct slab boundary calculations
- Passing USER pointer → wrong slab_idx → wrong metadata → freelist corruption
Impact Before Fix:
- bench_random_mixed crashes at ~14K iterations with SEGV
- Massive C7 alignment check failures (wrong slab classification)
- Memory corruption from writing to wrong slab freelists
Fixes Applied (8 locations):
1. core/hakmem_tiny_free.inc:137
- Added USER→BASE conversion before slab_index_for()
2. core/hakmem_tiny_ultra_simple.inc:148
- Added USER→BASE conversion before slab_index_for()
3. core/tiny_free_fast.inc.h:220
- Added USER→BASE conversion before slab_index_for()
4-5. core/tiny_free_magazine.inc.h:126,315
- Added USER→BASE conversion before slab_index_for() (2 locations)
6. core/box/free_local_box.c:14,22,62
- Added USER→BASE conversion before slab_index_for()
- Fixed delta calculation to use BASE instead of USER
- Fixed debug logging to use BASE instead of USER
7. core/hakmem_tiny.c:448,460,473 (tiny_debug_track_alloc_ret)
- Added USER→BASE conversion before slab_index_for() (2 calls)
- Fixed delta calculation to use BASE instead of USER
- This function is called on EVERY allocation in debug builds
Results After Fix:
✅ bench_random_mixed stable up to 66K iterations (~4.7x improvement)
✅ C7 alignment check failures eliminated (was: 100% failure rate)
✅ Front Gate "Unknown" classification dropped to 0% (was: 1.67%)
✅ No segfaults for workloads up to ~33K allocations
Remaining Issue:
❌ Segfault still occurs at iteration 66152 (allocs=33137, frees=33014)
- Different bug from USER/BASE conversion issues
- Likely capacity/boundary condition (further investigation needed)
Testing:
- bench_random_mixed_hakmem 1K-66K iterations: PASS
- bench_random_mixed_hakmem 67K+ iterations: FAIL (different bug)
- bench_fixed_size_hakmem 200K iterations: PASS
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-13 05:21:36 +09:00
|
|
|
// ✅ Phase E1-CORRECT: ALL classes have headers, calculate BASE pointer once
|
|
|
|
|
void* base = (void*)((uint8_t*)ptr - 1);
|
|
|
|
|
|
2025-11-21 23:00:24 +09:00
|
|
|
// Targeted header integrity check (env: HAKMEM_TINY_SLL_DIAG, C7 focus)
|
2025-11-28 01:06:15 +09:00
|
|
|
#if !HAKMEM_BUILD_RELEASE
|
2025-11-21 23:00:24 +09:00
|
|
|
do {
|
|
|
|
|
static int g_free_diag_en = -1;
|
|
|
|
|
static _Atomic uint32_t g_free_diag_shot = 0;
|
|
|
|
|
if (__builtin_expect(g_free_diag_en == -1, 0)) {
|
|
|
|
|
const char* e = getenv("HAKMEM_TINY_SLL_DIAG");
|
|
|
|
|
g_free_diag_en = (e && *e && *e != '0') ? 1 : 0;
|
|
|
|
|
}
|
|
|
|
|
if (__builtin_expect(g_free_diag_en && meta && meta->class_idx == 7, 0)) {
|
|
|
|
|
uint8_t hdr = *(uint8_t*)base;
|
|
|
|
|
uint8_t expect = (uint8_t)(HEADER_MAGIC | (meta->class_idx & HEADER_CLASS_MASK));
|
|
|
|
|
if (hdr != expect) {
|
|
|
|
|
uint32_t shot = atomic_fetch_add_explicit(&g_free_diag_shot, 1, memory_order_relaxed);
|
|
|
|
|
if (shot < 8) {
|
|
|
|
|
fprintf(stderr,
|
|
|
|
|
"[C7_FREE_HDR_DIAG] ss=%p slab=%d base=%p hdr=0x%02x expect=0x%02x freelist=%p used=%u\n",
|
|
|
|
|
(void*)ss,
|
|
|
|
|
slab_idx,
|
|
|
|
|
base,
|
|
|
|
|
hdr,
|
|
|
|
|
expect,
|
|
|
|
|
meta ? meta->freelist : NULL,
|
|
|
|
|
meta ? meta->used : 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} while (0);
|
2025-11-28 01:06:15 +09:00
|
|
|
#endif
|
2025-11-21 23:00:24 +09:00
|
|
|
|
2025-11-07 20:31:01 +09:00
|
|
|
if (__builtin_expect(tiny_refill_failfast_level() >= 2, 0)) {
|
Phase E1-CORRECT: Fix USER/BASE pointer conversion bugs in slab_index_for calls
CRITICAL BUG FIX: Phase E1 introduced 1-byte headers for ALL size classes (C0-C7),
changing the pointer contract. However, many locations still called slab_index_for()
with USER pointers (storage+1) instead of BASE pointers (storage), causing off-by-one
slab index calculations that corrupted memory.
Root Cause:
- USER pointer = BASE + 1 (returned by malloc, points past header)
- BASE pointer = storage start (where 1-byte header is written)
- slab_index_for() expects BASE pointer for correct slab boundary calculations
- Passing USER pointer → wrong slab_idx → wrong metadata → freelist corruption
Impact Before Fix:
- bench_random_mixed crashes at ~14K iterations with SEGV
- Massive C7 alignment check failures (wrong slab classification)
- Memory corruption from writing to wrong slab freelists
Fixes Applied (8 locations):
1. core/hakmem_tiny_free.inc:137
- Added USER→BASE conversion before slab_index_for()
2. core/hakmem_tiny_ultra_simple.inc:148
- Added USER→BASE conversion before slab_index_for()
3. core/tiny_free_fast.inc.h:220
- Added USER→BASE conversion before slab_index_for()
4-5. core/tiny_free_magazine.inc.h:126,315
- Added USER→BASE conversion before slab_index_for() (2 locations)
6. core/box/free_local_box.c:14,22,62
- Added USER→BASE conversion before slab_index_for()
- Fixed delta calculation to use BASE instead of USER
- Fixed debug logging to use BASE instead of USER
7. core/hakmem_tiny.c:448,460,473 (tiny_debug_track_alloc_ret)
- Added USER→BASE conversion before slab_index_for() (2 calls)
- Fixed delta calculation to use BASE instead of USER
- This function is called on EVERY allocation in debug builds
Results After Fix:
✅ bench_random_mixed stable up to 66K iterations (~4.7x improvement)
✅ C7 alignment check failures eliminated (was: 100% failure rate)
✅ Front Gate "Unknown" classification dropped to 0% (was: 1.67%)
✅ No segfaults for workloads up to ~33K allocations
Remaining Issue:
❌ Segfault still occurs at iteration 66152 (allocs=33137, frees=33014)
- Different bug from USER/BASE conversion issues
- Likely capacity/boundary condition (further investigation needed)
Testing:
- bench_random_mixed_hakmem 1K-66K iterations: PASS
- bench_random_mixed_hakmem 67K+ iterations: FAIL (different bug)
- bench_fixed_size_hakmem 200K iterations: PASS
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-13 05:21:36 +09:00
|
|
|
int actual_idx = slab_index_for(ss, base);
|
2025-11-07 20:31:01 +09:00
|
|
|
if (actual_idx != slab_idx) {
|
|
|
|
|
tiny_failfast_abort_ptr("free_local_box_idx", ss, slab_idx, ptr, "slab_idx_mismatch");
|
|
|
|
|
} else {
|
2025-11-13 16:33:03 +09:00
|
|
|
uint8_t cls = (meta && meta->class_idx < TINY_NUM_CLASSES) ? meta->class_idx : 0;
|
|
|
|
|
size_t blk = g_tiny_class_sizes[cls];
|
2025-11-07 20:31:01 +09:00
|
|
|
uint8_t* slab_base = tiny_slab_base_for(ss, slab_idx);
|
Phase E1-CORRECT: Fix USER/BASE pointer conversion bugs in slab_index_for calls
CRITICAL BUG FIX: Phase E1 introduced 1-byte headers for ALL size classes (C0-C7),
changing the pointer contract. However, many locations still called slab_index_for()
with USER pointers (storage+1) instead of BASE pointers (storage), causing off-by-one
slab index calculations that corrupted memory.
Root Cause:
- USER pointer = BASE + 1 (returned by malloc, points past header)
- BASE pointer = storage start (where 1-byte header is written)
- slab_index_for() expects BASE pointer for correct slab boundary calculations
- Passing USER pointer → wrong slab_idx → wrong metadata → freelist corruption
Impact Before Fix:
- bench_random_mixed crashes at ~14K iterations with SEGV
- Massive C7 alignment check failures (wrong slab classification)
- Memory corruption from writing to wrong slab freelists
Fixes Applied (8 locations):
1. core/hakmem_tiny_free.inc:137
- Added USER→BASE conversion before slab_index_for()
2. core/hakmem_tiny_ultra_simple.inc:148
- Added USER→BASE conversion before slab_index_for()
3. core/tiny_free_fast.inc.h:220
- Added USER→BASE conversion before slab_index_for()
4-5. core/tiny_free_magazine.inc.h:126,315
- Added USER→BASE conversion before slab_index_for() (2 locations)
6. core/box/free_local_box.c:14,22,62
- Added USER→BASE conversion before slab_index_for()
- Fixed delta calculation to use BASE instead of USER
- Fixed debug logging to use BASE instead of USER
7. core/hakmem_tiny.c:448,460,473 (tiny_debug_track_alloc_ret)
- Added USER→BASE conversion before slab_index_for() (2 calls)
- Fixed delta calculation to use BASE instead of USER
- This function is called on EVERY allocation in debug builds
Results After Fix:
✅ bench_random_mixed stable up to 66K iterations (~4.7x improvement)
✅ C7 alignment check failures eliminated (was: 100% failure rate)
✅ Front Gate "Unknown" classification dropped to 0% (was: 1.67%)
✅ No segfaults for workloads up to ~33K allocations
Remaining Issue:
❌ Segfault still occurs at iteration 66152 (allocs=33137, frees=33014)
- Different bug from USER/BASE conversion issues
- Likely capacity/boundary condition (further investigation needed)
Testing:
- bench_random_mixed_hakmem 1K-66K iterations: PASS
- bench_random_mixed_hakmem 67K+ iterations: FAIL (different bug)
- bench_fixed_size_hakmem 200K iterations: PASS
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-13 05:21:36 +09:00
|
|
|
uintptr_t delta = (uintptr_t)base - (uintptr_t)slab_base;
|
2025-11-07 20:31:01 +09:00
|
|
|
if (blk == 0 || (delta % blk) != 0) {
|
|
|
|
|
tiny_failfast_abort_ptr("free_local_box_align", ss, slab_idx, ptr, "misaligned");
|
|
|
|
|
} else if (meta && delta / blk >= meta->capacity) {
|
|
|
|
|
tiny_failfast_abort_ptr("free_local_box_range", ss, slab_idx, ptr, "out_of_capacity");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-07 01:27:04 +09:00
|
|
|
void* prev = meta->freelist;
|
2025-11-08 01:18:37 +09:00
|
|
|
|
2025-11-21 23:00:24 +09:00
|
|
|
// Detect suspicious prev before writing next (env-gated)
|
2025-11-28 01:06:15 +09:00
|
|
|
#if !HAKMEM_BUILD_RELEASE
|
2025-11-21 23:00:24 +09:00
|
|
|
do {
|
|
|
|
|
static int g_prev_diag_en = -1;
|
|
|
|
|
static _Atomic uint32_t g_prev_diag_shot = 0;
|
|
|
|
|
if (__builtin_expect(g_prev_diag_en == -1, 0)) {
|
|
|
|
|
const char* e = getenv("HAKMEM_TINY_SLL_DIAG");
|
|
|
|
|
g_prev_diag_en = (e && *e && *e != '0') ? 1 : 0;
|
|
|
|
|
}
|
|
|
|
|
if (__builtin_expect(g_prev_diag_en && prev && ((uintptr_t)prev < 4096 || (uintptr_t)prev > 0x00007fffffffffffULL), 0)) {
|
|
|
|
|
uint8_t cls_dbg = (meta && meta->class_idx < TINY_NUM_CLASSES) ? meta->class_idx : 0xFF;
|
|
|
|
|
uint32_t shot = atomic_fetch_add_explicit(&g_prev_diag_shot, 1, memory_order_relaxed);
|
|
|
|
|
if (shot < 8) {
|
|
|
|
|
fprintf(stderr,
|
|
|
|
|
"[FREELIST_PREV_INVALID] cls=%u slab=%d ptr=%p base=%p prev=%p freelist=%p used=%u\n",
|
|
|
|
|
cls_dbg,
|
|
|
|
|
slab_idx,
|
|
|
|
|
ptr,
|
|
|
|
|
base,
|
|
|
|
|
prev,
|
|
|
|
|
meta ? meta->freelist : NULL,
|
|
|
|
|
meta ? meta->used : 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} while (0);
|
2025-11-28 01:06:15 +09:00
|
|
|
#endif
|
2025-11-21 23:00:24 +09:00
|
|
|
|
2025-11-08 01:18:37 +09:00
|
|
|
// FREELIST CORRUPTION DEBUG: Validate pointer before writing
|
|
|
|
|
if (__builtin_expect(tiny_refill_failfast_level() >= 2, 0)) {
|
2025-11-13 16:33:03 +09:00
|
|
|
uint8_t cls = (meta && meta->class_idx < TINY_NUM_CLASSES) ? meta->class_idx : 0;
|
|
|
|
|
size_t blk = g_tiny_class_sizes[cls];
|
2025-11-08 01:18:37 +09:00
|
|
|
uint8_t* base_ss = (uint8_t*)ss;
|
|
|
|
|
uint8_t* slab_base = tiny_slab_base_for(ss, slab_idx);
|
|
|
|
|
|
|
|
|
|
// Verify prev pointer is valid (if not NULL)
|
|
|
|
|
if (prev != NULL) {
|
|
|
|
|
uintptr_t prev_addr = (uintptr_t)prev;
|
|
|
|
|
uintptr_t slab_addr = (uintptr_t)slab_base;
|
|
|
|
|
|
|
|
|
|
// Check if prev is within this slab
|
|
|
|
|
if (prev_addr < (uintptr_t)base_ss || prev_addr >= (uintptr_t)base_ss + (2*1024*1024)) {
|
2025-11-13 16:33:03 +09:00
|
|
|
fprintf(stderr, "[FREE_CORRUPT] prev=%p outside SuperSlab ss=%p slab=%d\n",
|
|
|
|
|
prev, ss, slab_idx);
|
2025-11-08 01:18:37 +09:00
|
|
|
tiny_failfast_abort_ptr("free_local_prev_range", ss, slab_idx, ptr, "prev_outside_ss");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check alignment of prev
|
|
|
|
|
if ((prev_addr - slab_addr) % blk != 0) {
|
|
|
|
|
fprintf(stderr, "[FREE_CORRUPT] prev=%p misaligned (cls=%u slab=%d blk=%zu offset=%zu)\n",
|
2025-11-13 16:33:03 +09:00
|
|
|
prev, cls, slab_idx, blk, (size_t)(prev_addr - slab_addr));
|
2025-11-08 01:18:37 +09:00
|
|
|
fprintf(stderr, "[FREE_CORRUPT] Writing from ptr=%p, freelist was=%p\n", ptr, prev);
|
|
|
|
|
tiny_failfast_abort_ptr("free_local_prev_misalign", ss, slab_idx, ptr, "prev_misaligned");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fprintf(stderr, "[FREE_VERIFY] cls=%u slab=%d ptr=%p prev=%p (offset_ptr=%zu offset_prev=%zu)\n",
|
2025-11-13 16:33:03 +09:00
|
|
|
cls, slab_idx, ptr, prev,
|
Phase E1-CORRECT: Fix USER/BASE pointer conversion bugs in slab_index_for calls
CRITICAL BUG FIX: Phase E1 introduced 1-byte headers for ALL size classes (C0-C7),
changing the pointer contract. However, many locations still called slab_index_for()
with USER pointers (storage+1) instead of BASE pointers (storage), causing off-by-one
slab index calculations that corrupted memory.
Root Cause:
- USER pointer = BASE + 1 (returned by malloc, points past header)
- BASE pointer = storage start (where 1-byte header is written)
- slab_index_for() expects BASE pointer for correct slab boundary calculations
- Passing USER pointer → wrong slab_idx → wrong metadata → freelist corruption
Impact Before Fix:
- bench_random_mixed crashes at ~14K iterations with SEGV
- Massive C7 alignment check failures (wrong slab classification)
- Memory corruption from writing to wrong slab freelists
Fixes Applied (8 locations):
1. core/hakmem_tiny_free.inc:137
- Added USER→BASE conversion before slab_index_for()
2. core/hakmem_tiny_ultra_simple.inc:148
- Added USER→BASE conversion before slab_index_for()
3. core/tiny_free_fast.inc.h:220
- Added USER→BASE conversion before slab_index_for()
4-5. core/tiny_free_magazine.inc.h:126,315
- Added USER→BASE conversion before slab_index_for() (2 locations)
6. core/box/free_local_box.c:14,22,62
- Added USER→BASE conversion before slab_index_for()
- Fixed delta calculation to use BASE instead of USER
- Fixed debug logging to use BASE instead of USER
7. core/hakmem_tiny.c:448,460,473 (tiny_debug_track_alloc_ret)
- Added USER→BASE conversion before slab_index_for() (2 calls)
- Fixed delta calculation to use BASE instead of USER
- This function is called on EVERY allocation in debug builds
Results After Fix:
✅ bench_random_mixed stable up to 66K iterations (~4.7x improvement)
✅ C7 alignment check failures eliminated (was: 100% failure rate)
✅ Front Gate "Unknown" classification dropped to 0% (was: 1.67%)
✅ No segfaults for workloads up to ~33K allocations
Remaining Issue:
❌ Segfault still occurs at iteration 66152 (allocs=33137, frees=33014)
- Different bug from USER/BASE conversion issues
- Likely capacity/boundary condition (further investigation needed)
Testing:
- bench_random_mixed_hakmem 1K-66K iterations: PASS
- bench_random_mixed_hakmem 67K+ iterations: FAIL (different bug)
- bench_fixed_size_hakmem 200K iterations: PASS
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-13 05:21:36 +09:00
|
|
|
(size_t)((uintptr_t)base - (uintptr_t)slab_base),
|
2025-11-08 01:18:37 +09:00
|
|
|
prev ? (size_t)((uintptr_t)prev - (uintptr_t)slab_base) : 0);
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-21 23:00:24 +09:00
|
|
|
// Use per-slab class for freelist linkage (BASE pointers only)
|
2025-11-13 16:33:03 +09:00
|
|
|
uint8_t cls = (meta && meta->class_idx < TINY_NUM_CLASSES) ? meta->class_idx : 0;
|
2025-11-21 23:00:24 +09:00
|
|
|
tiny_next_write(cls, base, prev); // Phase E1-CORRECT: Box API with shared pool
|
|
|
|
|
meta->freelist = base;
|
2025-11-08 01:18:37 +09:00
|
|
|
|
|
|
|
|
// FREELIST CORRUPTION DEBUG: Verify write succeeded
|
|
|
|
|
if (__builtin_expect(tiny_refill_failfast_level() >= 2, 0)) {
|
2025-11-13 16:33:03 +09:00
|
|
|
void* readback = tiny_next_read(cls, ptr); // Phase E1-CORRECT: Box API
|
2025-11-08 01:18:37 +09:00
|
|
|
if (readback != prev) {
|
|
|
|
|
fprintf(stderr, "[FREE_CORRUPT] Wrote prev=%p to ptr=%p but read back %p!\n",
|
|
|
|
|
prev, ptr, readback);
|
|
|
|
|
fprintf(stderr, "[FREE_CORRUPT] Memory corruption detected during freelist push\n");
|
|
|
|
|
tiny_failfast_abort_ptr("free_local_readback", ss, slab_idx, ptr, "write_corrupted");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-21 23:00:24 +09:00
|
|
|
tiny_failfast_log("free_local_box", cls, ss, meta, base, prev);
|
2025-11-07 01:27:04 +09:00
|
|
|
// BUGFIX: Memory barrier to ensure freelist visibility before used decrement
|
|
|
|
|
// Without this, other threads can see new freelist but old used count (race)
|
|
|
|
|
atomic_thread_fence(memory_order_release);
|
|
|
|
|
|
|
|
|
|
// Optional freelist mask update on first push
|
2025-11-28 01:06:15 +09:00
|
|
|
#if !HAKMEM_BUILD_RELEASE
|
2025-11-07 01:27:04 +09:00
|
|
|
do {
|
|
|
|
|
static int g_mask_en = -1;
|
|
|
|
|
if (__builtin_expect(g_mask_en == -1, 0)) {
|
|
|
|
|
const char* e = getenv("HAKMEM_TINY_FREELIST_MASK");
|
|
|
|
|
g_mask_en = (e && *e && *e != '0') ? 1 : 0;
|
|
|
|
|
}
|
|
|
|
|
if (__builtin_expect(g_mask_en, 0) && prev == NULL) {
|
|
|
|
|
uint32_t bit = (1u << slab_idx);
|
|
|
|
|
atomic_fetch_or_explicit(&ss->freelist_mask, bit, memory_order_release);
|
|
|
|
|
}
|
|
|
|
|
} while (0);
|
2025-11-28 01:06:15 +09:00
|
|
|
#endif
|
2025-11-07 01:27:04 +09:00
|
|
|
|
|
|
|
|
// Track local free (debug helpers may be no-op)
|
|
|
|
|
tiny_remote_track_on_local_free(ss, slab_idx, ptr, "local_free", my_tid);
|
2025-12-01 13:47:23 +09:00
|
|
|
|
|
|
|
|
// BUGFIX Phase 9-2: Use atomic_fetch_sub to detect 1->0 transition reliably
|
|
|
|
|
// meta->used--; // old
|
|
|
|
|
uint16_t prev_used = atomic_fetch_sub_explicit(&meta->used, 1, memory_order_release);
|
|
|
|
|
int is_empty = (prev_used == 1); // Transitioned from 1 to 0
|
|
|
|
|
|
2025-11-07 01:27:04 +09:00
|
|
|
ss_active_dec_one(ss);
|
|
|
|
|
|
2025-11-21 04:56:48 +09:00
|
|
|
// Phase 12-1.1: EMPTY slab detection (immediate reuse optimization)
|
2025-12-01 13:47:23 +09:00
|
|
|
if (is_empty) {
|
2025-11-21 04:56:48 +09:00
|
|
|
// Slab became EMPTY → mark for highest-priority reuse
|
|
|
|
|
ss_mark_slab_empty(ss, slab_idx);
|
|
|
|
|
|
|
|
|
|
// DEBUG LOGGING - Track when used reaches 0
|
2025-11-28 01:06:15 +09:00
|
|
|
#if !HAKMEM_BUILD_RELEASE
|
2025-11-21 04:56:48 +09:00
|
|
|
static int dbg = -1;
|
|
|
|
|
if (__builtin_expect(dbg == -1, 0)) {
|
|
|
|
|
const char* e = getenv("HAKMEM_SS_FREE_DEBUG");
|
|
|
|
|
dbg = (e && *e && *e != '0') ? 1 : 0;
|
|
|
|
|
}
|
2025-11-28 01:06:15 +09:00
|
|
|
#else
|
|
|
|
|
const int dbg = 0;
|
|
|
|
|
#endif
|
2025-11-21 04:56:48 +09:00
|
|
|
if (dbg == 1) {
|
|
|
|
|
fprintf(stderr, "[FREE_LOCAL_BOX] EMPTY detected: cls=%u ss=%p slab=%d empty_mask=0x%x empty_count=%u\n",
|
|
|
|
|
cls, (void*)ss, slab_idx, ss->empty_mask, ss->empty_count);
|
|
|
|
|
}
|
2025-11-14 06:49:32 +09:00
|
|
|
}
|
|
|
|
|
|
2025-11-07 01:27:04 +09:00
|
|
|
if (prev == NULL) {
|
2025-11-13 16:33:03 +09:00
|
|
|
// First-free → advertise slab to adopters using per-slab class
|
|
|
|
|
uint8_t cls0 = (meta && meta->class_idx < TINY_NUM_CLASSES) ? meta->class_idx : 0;
|
|
|
|
|
tiny_free_publish_first_free((int)cls0, ss, slab_idx);
|
2025-11-07 01:27:04 +09:00
|
|
|
}
|
2025-12-01 13:47:23 +09:00
|
|
|
|
|
|
|
|
return is_empty;
|
2025-11-07 01:27:04 +09:00
|
|
|
}
|