47 lines
1.8 KiB
C
47 lines
1.8 KiB
C
// fg_tiny_gate_box.h - Tiny Front Gate guard box
|
|
// 役割: FG_DOMAIN_TINY 判定が出た時に Superslab 登録を確認し、誤分類なら MIDCAND に切替
|
|
// 境界: Tiny 経路へ入る前に 1 箇所でのみ実行する
|
|
#ifndef HAK_BOX_FG_TINY_GATE_H
|
|
#define HAK_BOX_FG_TINY_GATE_H
|
|
|
|
#include "../hakmem_super_registry.h" // hak_super_lookup / SUPERSLAB_MAGIC
|
|
#include "front_gate_v2.h"
|
|
#include <stdatomic.h>
|
|
#include <stdio.h>
|
|
|
|
typedef struct {
|
|
fg_classification_t fg;
|
|
int misclassified; // 1 when Tiny→MIDCAND に落とした
|
|
} fg_tiny_gate_result_t;
|
|
|
|
// 簡易ログ(初回のみ)
|
|
static inline void fg_tiny_gate_log(void* ptr, uint8_t hdr, uintptr_t off, int use_ss, int reg_init) {
|
|
static _Atomic int log_budget = 4;
|
|
int n = atomic_fetch_add_explicit(&log_budget, 1, memory_order_relaxed);
|
|
if (n >= 4) return;
|
|
fprintf(stderr,
|
|
"[FG_TINY_MISCLASS] ptr=%p hdr=0x%02x off=0x%lx g_use_ss=%d reg_init=%d → reroute to MIDCAND\n",
|
|
ptr, hdr, (unsigned long)off, use_ss, reg_init);
|
|
}
|
|
|
|
// Tiny判定のガード: Superslab登録が無ければ MIDCAND に変換
|
|
static inline fg_tiny_gate_result_t fg_tiny_gate(void* ptr, fg_classification_t fg) {
|
|
fg_tiny_gate_result_t r = {.fg = fg, .misclassified = 0};
|
|
if (fg.domain != FG_DOMAIN_TINY) return r;
|
|
|
|
SuperSlab* fg_ss = hak_super_lookup(ptr);
|
|
if (__builtin_expect(!(fg_ss && fg_ss->magic == SUPERSLAB_MAGIC), 0)) {
|
|
extern int g_use_superslab;
|
|
extern int g_super_reg_initialized;
|
|
uint8_t hdrb = 0;
|
|
if ((uintptr_t)ptr > 0) { hdrb = *((uint8_t*)ptr - 1); }
|
|
uintptr_t off = (uintptr_t)ptr & 0xFFFu;
|
|
fg_tiny_gate_log(ptr, hdrb, off, g_use_superslab, g_super_reg_initialized);
|
|
r.fg.domain = FG_DOMAIN_MIDCAND;
|
|
r.misclassified = 1;
|
|
}
|
|
return r;
|
|
}
|
|
|
|
#endif // HAK_BOX_FG_TINY_GATE_H
|