Tiny: fix header/stride mismatch and harden refill paths
- Root cause: header-based class indexing (HEADER_CLASSIDX=1) wrote a 1-byte header during allocation, but linear carve/refill and initial slab capacity still used bare class block sizes. This mismatch could overrun slab usable space and corrupt freelists, causing reproducible SEGV at ~100k iters. Changes - Superslab: compute capacity with effective stride (block_size + header for classes 0..6; class7 remains headerless) in superslab_init_slab(). Add a debug-only bound check in superslab_alloc_from_slab() to fail fast if carve would exceed usable bytes. - Refill (non-P0 and P0): use header-aware stride for all linear carving and TLS window bump operations. Ensure alignment/validation in tiny_refill_opt.h also uses stride, not raw class size. - Drain: keep existing defense-in-depth for remote sentinel and sanitize nodes before splicing into freelist (already present). Notes - This unifies the memory layout across alloc/linear-carve/refill with a single stride definition and keeps class7 (1024B) headerless as designed. - Debug builds add fail-fast checks; release builds remain lean. Next - Re-run Tiny benches (256/1024B) in debug to confirm stability, then in release. If any remaining crash persists, bisect with HAKMEM_TINY_P0_BATCH_REFILL=0 to isolate P0 batch carve, and continue reducing branch-miss as planned.
This commit is contained in:
@ -3,15 +3,54 @@
|
||||
#define POOL_API_INC_H
|
||||
|
||||
void* hak_pool_try_alloc(size_t size, uintptr_t site_id) {
|
||||
// Debug: IMMEDIATE output to verify function is called
|
||||
static int first_call = 1;
|
||||
if (first_call) {
|
||||
fprintf(stderr, "[Pool] hak_pool_try_alloc FIRST CALL EVER!\n");
|
||||
first_call = 0;
|
||||
}
|
||||
|
||||
if (size == 40960) { // Exactly 40KB
|
||||
fprintf(stderr, "[Pool] hak_pool_try_alloc called with 40KB (Bridge class 5)\n");
|
||||
}
|
||||
|
||||
hak_pool_init(); // pthread_once() ensures thread-safe init (no data race!)
|
||||
|
||||
// Debug for 33-41KB allocations
|
||||
if (size >= 33000 && size <= 41000) {
|
||||
fprintf(stderr, "[Pool] hak_pool_try_alloc: size=%zu (after init)\n", size);
|
||||
}
|
||||
|
||||
// P1.7 approach: Avoid using pool during ALL wrapper calls (conservative but safe)
|
||||
extern int hak_in_wrapper(void);
|
||||
if (hak_in_wrapper() && !g_wrap_l2_enabled) return NULL;
|
||||
if (!hak_pool_is_poolable(size)) return NULL;
|
||||
if (hak_in_wrapper() && !g_wrap_l2_enabled) {
|
||||
if (size >= 33000 && size <= 41000) {
|
||||
fprintf(stderr, "[Pool] REJECTED: in_wrapper=%d, wrap_l2=%d\n",
|
||||
hak_in_wrapper(), g_wrap_l2_enabled);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
if (!hak_pool_is_poolable(size)) {
|
||||
if (size >= 33000 && size <= 41000) {
|
||||
fprintf(stderr, "[Pool] REJECTED: not poolable (min=%d, max=%d)\n",
|
||||
POOL_MIN_SIZE, POOL_MAX_SIZE);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Get class and shard indices
|
||||
int class_idx = hak_pool_get_class_index(size);
|
||||
if (class_idx < 0) return NULL;
|
||||
if (class_idx < 0) {
|
||||
if (size >= 33000 && size <= 41000) {
|
||||
fprintf(stderr, "[Pool] REJECTED: class_idx=%d (size=%zu not mapped)\n",
|
||||
class_idx, size);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (size >= 33000 && size <= 41000) {
|
||||
fprintf(stderr, "[Pool] ACCEPTED: class_idx=%d, proceeding with allocation\n", class_idx);
|
||||
}
|
||||
|
||||
// MF2: Per-Page Sharding path
|
||||
if (g_mf2_enabled) {
|
||||
|
||||
Reference in New Issue
Block a user