Files
hakmem/core/tiny_tls_guard.h

79 lines
2.4 KiB
C
Raw Normal View History

#ifndef TINY_TLS_GUARD_H
#define TINY_TLS_GUARD_H
#include <stdatomic.h>
#include <inttypes.h>
#include <stdio.h>
#include "hakmem_tiny.h"
#include "hakmem_tiny_superslab.h"
#include "hakmem_tiny_tls_list.h"
#include "tiny_debug_ring.h"
#include "tiny_remote.h"
static inline void tiny_tls_list_guard_push(int class_idx, TinyTLSList* tls, void* node) {
if (!node) return;
extern int g_debug_remote_guard;
if (!__builtin_expect(g_debug_remote_guard, 0)) {
return;
}
uintptr_t prior = atomic_load_explicit((_Atomic uintptr_t*)node, memory_order_relaxed);
tiny_remote_report_corruption("tls_push", node, prior);
int warn = 0;
size_t blk = (size_t)g_tiny_class_sizes[class_idx];
int slab_idx = -1;
SuperSlab* ss = hak_super_lookup(node);
TinySlabMeta* meta = NULL;
uintptr_t base_val = 0;
size_t ss_size = 0;
if (ss && ss->magic == SUPERSLAB_MAGIC) {
slab_idx = slab_index_for(ss, node);
if (slab_idx >= 0) {
meta = &ss->slabs[slab_idx];
uint8_t* base = tiny_slab_base_for(ss, slab_idx);
base_val = (uintptr_t)base;
ss_size = (size_t)1ULL << ss->lg_size;
if (blk != 0) {
uintptr_t delta = (uintptr_t)node - (uintptr_t)base;
if ((delta % blk) != 0 || (meta && meta->capacity > 0 && (delta / blk) >= meta->capacity)) {
warn = 1;
}
}
} else {
warn = 1;
}
} else {
warn = 1;
}
if (!warn && (prior & (sizeof(void*) - 1)) != 0) {
warn = 1;
}
if (warn) {
uintptr_t aux = 0;
if (ss) {
aux = tiny_remote_pack_diag(0xB301u, base_val, ss_size, (uintptr_t)node);
} else {
aux = ((uintptr_t)(uint16_t)class_idx << 32) | 0xB302u;
}
tiny_debug_ring_record(TINY_RING_EVENT_REMOTE_INVALID, (uint16_t)class_idx, node, aux);
fprintf(stderr,
"[TLS_LIST_GUARD] cls=%d node=%p prior=0x%016" PRIxPTR " ss=%p meta=%p count=%u cap=%u\n",
class_idx,
node,
prior,
(void*)ss,
(void*)meta,
tls ? tls->count : 0u,
tls ? tls->cap : 0u);
}
if (tiny_remote_watch_is(node)) {
tiny_remote_watch_note("tls_push", ss, slab_idx, node, 0xA241u, 0, 0);
}
}
#endif