Four bugs found live verifying the 8 identity thumbdrives (FABRIC-3.md §IX)
All found by actually running the identity workflow §VII/§VIII made possible, not by code review: 1. Zuse/WIREBIND cross-contamination on detach: capsule_zuse_boot_logout() and capsule_wirebind_unclean_detach() both had no device parameter, so an unrelated device detaching (while the real owner's own stayed attached) incorrectly tore down the wrong session. Both now compare the departing device against their own tracked one, mirroring capsule_wirebind.c's pre-existing g_wirebind_attached_dev precedent. 2. Dictionary-entry memory leak: vm_create_word()'s sf_malloc()'d DictEntry (plus a second per-entry allocation for transition_metrics) was never freed by vm_cleanup(), in both the hosted and kernel implementations. Caused a real kernel PANIC after 8-9 repeated VM birth/kill cycles in one boot. Fixed by walking vm->latest in both. 3. sf_malloc/sf_free (alloc_kernel.c) was a 4MB bump arena with a deliberate no-op free, sized on "VM born once, never killed" -- fix #2 alone didn't stop the panic because free() itself discarded the pointer regardless. Given a real free list (first-fit reuse). 4. Headless-console gate didn't re-engage after a mid-boot logout: the original fix (sk_console_mark_login(), one-way sticky) only gated the first login of the boot. Replaced with a live check (sk_console_identity_present()) re-evaluated continuously, including inside sk_console_readline()'s own blocking idle loop -- the console is normally sitting blocked there when a hot-unplug logout happens, so checking only at the top of the REPL loop wasn't enough. Also: MINT now verifies its own write (verify_mint(), capsule_mint.c) by reading back through the same check a real attach performs, rather than trusting blkio_write()'s BLK_OK alone -- logged via log_message(), not console_println(), per direct instruction. Verified live, amd64: the full 8-identity repeated attach/detach cycle that previously panicked at the same point every time now completes clean, and a full serial-log sweep found zero bare unauthenticated prompts anywhere in the run. Three-arch clean-qemu acceptance passed. Still open, not fixed here: a 3+-simultaneous-device USB enumeration failure found in a separate live test, not yet root-caused. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018EjXFo7mPXjUMjfJeuUUz4
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
0bae928aad
commit
2c1b3cd695
@@ -19,6 +19,7 @@
|
||||
#include "starkernel/rng.h"
|
||||
#include "block_subsystem.h" /* compute_crc64() */
|
||||
#include "blkio.h"
|
||||
#include "log.h"
|
||||
#include <string.h>
|
||||
#include <stddef.h>
|
||||
|
||||
@@ -66,6 +67,80 @@ static int write_devblock(struct blkio_dev *dev, uint32_t devblock,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Read exactly one devblock (4096 bytes) at devblock offset `devblock`,
|
||||
* mirroring write_devblock() above -- used only by the post-write
|
||||
* verification pass (2026-09-06), to read back what was just written
|
||||
* rather than trusting blkio_write()'s BLK_OK return alone. */
|
||||
static int read_devblock(struct blkio_dev *dev, uint32_t devblock,
|
||||
uint8_t *buf4096) {
|
||||
uint32_t base = devblock * 4u;
|
||||
for (uint32_t i = 0; i < 4u; i++) {
|
||||
if (blkio_read((blkio_dev_t *)dev, base + i,
|
||||
buf4096 + (size_t)i * BLKIO_FORTH_BLOCK_SIZE) != BLKIO_OK) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Post-write verification (2026-09-06, direct instruction after a live
|
||||
* xHCI enumeration failure raised the question of whether MINT itself
|
||||
* could silently succeed without the data actually being readable back):
|
||||
* re-reads the identity record and re-runs homeblocks_sig_check() --
|
||||
* the exact same check a real attach later performs -- instead of trusting
|
||||
* every write_devblock() call's own BLK_OK return as proof the drive will
|
||||
* actually read back correctly. blkio_write() succeeding only means the
|
||||
* BOT command chain completed; it says nothing about whether those bytes
|
||||
* survive to be read back under real hardware/emulation conditions.
|
||||
* Logged entirely through log_message() -- LOG_ERROR with the specific
|
||||
* mismatch on failure, LOG_INFO confirming success -- not console_println,
|
||||
* so this doesn't add unconditional console/serial noise to every mint;
|
||||
* the MINT word's own caller (mama_forth_words.c) still reports the final
|
||||
* pass/fail result to the console either way via its existing console_
|
||||
* println() switch. Returns 0 if everything reads back correctly, -1
|
||||
* otherwise. */
|
||||
static int verify_mint(struct blkio_dev *dev, uint32_t identity_src_offset,
|
||||
const user_identity_seed_t *written_idrec) {
|
||||
homeblocks_sig_t verify_sig;
|
||||
homeblocks_sig_result_t sig_rc =
|
||||
homeblocks_sig_check(dev, HOMEBLOCKS_SIG_START_FBLOCK, &verify_sig);
|
||||
if (sig_rc != HOMEBLOCKS_SIG_OK) {
|
||||
log_message(LOG_ERROR,
|
||||
"MINT verify: homeblocks_sig_check() did not read back OK (rc=%d)",
|
||||
(int) sig_rc);
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint8_t readback_block[4096];
|
||||
if (read_devblock(dev, identity_src_offset, readback_block) != 0) {
|
||||
log_message(LOG_ERROR, "MINT verify: identity record read-back failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
const user_identity_seed_t *readback_idrec =
|
||||
(const user_identity_seed_t *) readback_block;
|
||||
if (readback_idrec->magic != written_idrec->magic ||
|
||||
readback_idrec->version != written_idrec->version) {
|
||||
log_message(LOG_ERROR,
|
||||
"MINT verify: identity record magic/version mismatch on read-back");
|
||||
return -1;
|
||||
}
|
||||
uint64_t want_crc = compute_crc64((const uint8_t *) readback_idrec,
|
||||
offsetof(user_identity_seed_t, crc));
|
||||
if (want_crc != readback_idrec->crc) {
|
||||
log_message(LOG_ERROR, "MINT verify: identity record CRC mismatch on read-back");
|
||||
return -1;
|
||||
}
|
||||
if (memcmp(readback_idrec, written_idrec, sizeof(*written_idrec)) != 0) {
|
||||
log_message(LOG_ERROR,
|
||||
"MINT verify: identity record content differs from what was written");
|
||||
return -1;
|
||||
}
|
||||
|
||||
log_message(LOG_INFO, "MINT verify: identity reads back correctly");
|
||||
return 0;
|
||||
}
|
||||
|
||||
MintResult capsule_mint_identity(struct blkio_dev *dev, VM *issuer_vm,
|
||||
const char *full_name, const char *username,
|
||||
const char *email, const char *phone,
|
||||
@@ -188,5 +263,9 @@ MintResult capsule_mint_identity(struct blkio_dev *dev, VM *issuer_vm,
|
||||
|
||||
(void)blkio_flush((blkio_dev_t *)dev);
|
||||
|
||||
if (verify_mint(dev, MINT_IDENTITY_SRC_OFFSET, &idrec) != 0) {
|
||||
return MINT_ERR_VERIFY_FAILED;
|
||||
}
|
||||
|
||||
return MINT_OK;
|
||||
}
|
||||
|
||||
@@ -204,9 +204,12 @@ void capsule_wirebind_try_attach(struct blkio_dev *dev,
|
||||
memcpy(g_wirebind_attached_username, username, sizeof(g_wirebind_attached_username));
|
||||
|
||||
/* Decided 2026-09-05: no console for the running system unless a
|
||||
* thumbdrive is present -- this successful console+user VM birth is
|
||||
* exactly that login. See repl.h's own doc comment. */
|
||||
sk_console_mark_login();
|
||||
* thumbdrive is present. Revised 2026-09-06: this used to call
|
||||
* sk_console_mark_login() here (a one-way sticky flag) -- the gate is
|
||||
* now a live check (sk_console_identity_present(), repl.c) driven
|
||||
* directly by g_wirebind_attached_username being set above, re-checked
|
||||
* continuously by sk_repl_run()'s own main loop rather than a one-shot
|
||||
* signal at login time. Nothing to call here anymore. */
|
||||
|
||||
/* Register the pairing in the console's own routing table, index 3
|
||||
* -- the fixed convention sk_repl_dispatch_line() (repl.c) uses. */
|
||||
@@ -264,7 +267,18 @@ int capsule_wirebind_eject(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void capsule_wirebind_unclean_detach(void) {
|
||||
void capsule_wirebind_unclean_detach(struct blkio_dev *dev) {
|
||||
/* FABRIC-3.md §VII follow-on, 2026-09-06: same defect class as
|
||||
* capsule_zuse_boot_logout()'s own fix, found in the same live
|
||||
* verification session -- this used to take no device parameter at
|
||||
* all, so an unrelated device detaching (Zuse's own drive, or general-
|
||||
* purpose USB use) while a WIREBIND user stayed attached would
|
||||
* incorrectly tear down that user's session too. The single-USB-
|
||||
* device constraint this was written under never let a *different*
|
||||
* device be the one detaching while a WIREBIND user's own stayed live
|
||||
* -- stale now that genuine multi-device attach exists. */
|
||||
if (dev != g_wirebind_attached_dev) return;
|
||||
|
||||
VMRegistryEntry entry;
|
||||
if (wirebind_resolve_attached(&entry) != 0) return;
|
||||
|
||||
|
||||
@@ -16,19 +16,26 @@
|
||||
#include "starkernel/zuse_genesis_marker.h"
|
||||
#include "starkernel/user_identity_seed.h"
|
||||
#include "starkernel/console.h"
|
||||
#include "starkernel/repl.h" /* sk_console_mark_login() -- FABRIC-2.md headless-until-login gate */
|
||||
#include "block_subsystem.h" /* compute_crc64(), blk_meta_zone_read/write */
|
||||
#include "blkio.h"
|
||||
#include <string.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/* FABRIC-2.md §I.8 (re-scoped 2026-09-04): tracks whether the currently
|
||||
* home-blocks-attached device is Zuse's own -- the single-USB-device
|
||||
* constraint (§F.8) means capsule_zuse_boot_logout() only needs one flag,
|
||||
* not a device/uuid comparison, to know a detach event is hers to act on
|
||||
* (a regular WIREBIND user occupying the one slot instead is tracked
|
||||
* separately, in capsule_wirebind.c -- the two paths never overlap). */
|
||||
/* FABRIC-3.md §VII follow-on, 2026-09-06: g_zuse_attached_this_device was a
|
||||
* bare boolean under the single-USB-device constraint (§F.8) -- "a detach
|
||||
* event is hers" needed no device comparison because only one device could
|
||||
* ever be attached at all. That premise is stale (FABRIC-3.md §VII, the
|
||||
* xHCI/BOT driver now supports genuine simultaneous multi-device attach) --
|
||||
* confirmed live during this session's own 8-identity verification: with
|
||||
* Zuse's drive and a WIREBIND target both attached, detaching the *target*
|
||||
* incorrectly logged Zuse out too, because capsule_zuse_boot_logout() had
|
||||
* no way to tell "some other device detached" from "my own device
|
||||
* detached." g_zuse_attached_dev is the fix, mirroring capsule_wirebind.c's
|
||||
* own g_wirebind_attached_dev precedent exactly -- the boolean stays (still
|
||||
* useful as a fast "is she attached at all" check) but logout now also
|
||||
* requires the departing device to match. */
|
||||
static int g_zuse_attached_this_device = 0;
|
||||
static struct blkio_dev *g_zuse_attached_dev = (struct blkio_dev *) 0;
|
||||
|
||||
/* Read exactly one devblock (4096 bytes) at devblock offset `devblock`,
|
||||
* as 4 consecutive 1KiB forth-block reads -- mirrors capsule_runcap.c's
|
||||
@@ -53,7 +60,8 @@ static int genesis_marker_read(zuse_genesis_marker_t *out) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void install_and_activate(VM *mama_vm, const uint8_t seed[32], const uint8_t pubkey[32]) {
|
||||
static void install_and_activate(VM *mama_vm, struct blkio_dev *dev,
|
||||
const uint8_t seed[32], const uint8_t pubkey[32]) {
|
||||
/* vm_zuse_cert_install() is deliberately one-way (returns -1, no-op,
|
||||
* once vm->zuse_cert_installed is already 1) -- that's a real
|
||||
* security property (the cert/pubkey must never be re-installed or
|
||||
@@ -75,13 +83,16 @@ static void install_and_activate(VM *mama_vm, const uint8_t seed[32], const uint
|
||||
* convention. */
|
||||
vm_interpret(mama_vm, "ACL-ZUSE-BOOT");
|
||||
g_zuse_attached_this_device = 1;
|
||||
g_zuse_attached_dev = dev;
|
||||
|
||||
/* Decided 2026-09-05: no console for the running system unless a
|
||||
* thumbdrive is present. Zuse's own login is not special here --
|
||||
* "nothing special about zuse as a user except zuse has no ACLs,"
|
||||
* per direct instruction -- so this is the same shared signal
|
||||
* capsule_wirebind.c's own successful login sets. See repl.h. */
|
||||
sk_console_mark_login();
|
||||
* per direct instruction. Revised 2026-09-06: no longer calls
|
||||
* sk_console_mark_login() (a one-way sticky flag) -- the gate is now a
|
||||
* live check (sk_console_identity_present(), repl.c) driven directly
|
||||
* by mama_vm->zuse_session, which vm_interpret(mama_vm,
|
||||
* "ACL-ZUSE-BOOT") above already sets. Nothing to call here anymore. */
|
||||
}
|
||||
|
||||
void capsule_zuse_boot_try_attach(struct blkio_dev *dev,
|
||||
@@ -125,7 +136,7 @@ void capsule_zuse_boot_try_attach(struct blkio_dev *dev,
|
||||
console_println("Zuse: genesis minted onto attached thumbdrive");
|
||||
}
|
||||
|
||||
install_and_activate(mama_vm, seed, pubkey);
|
||||
install_and_activate(mama_vm, dev, seed, pubkey);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -148,14 +159,24 @@ void capsule_zuse_boot_try_attach(struct blkio_dev *dev,
|
||||
if (memcmp(idrec.pubkey, marker.zuse_pubkey, 32) != 0) return; /* not Zuse's drive */
|
||||
|
||||
console_println("Zuse: identity confirmed from attached thumbdrive");
|
||||
install_and_activate(mama_vm, idrec.seed, idrec.pubkey);
|
||||
install_and_activate(mama_vm, dev, idrec.seed, idrec.pubkey);
|
||||
}
|
||||
|
||||
void capsule_zuse_boot_logout(VM *mama_vm) {
|
||||
void capsule_zuse_boot_logout(VM *mama_vm, struct blkio_dev *dev) {
|
||||
if (!mama_vm || !g_zuse_attached_this_device) return;
|
||||
/* The device actually detaching must be hers -- see g_zuse_attached_dev's
|
||||
* own doc comment above. A different device (a WIREBIND target,
|
||||
* general-purpose USB use) detaching while her own drive stays
|
||||
* attached must not touch her session at all. */
|
||||
if (dev != g_zuse_attached_dev) return;
|
||||
|
||||
mama_vm->zuse_session = 0;
|
||||
g_zuse_attached_this_device = 0;
|
||||
g_zuse_attached_dev = (struct blkio_dev *) 0;
|
||||
|
||||
console_println("Zuse: session ended -- reattach to re-authenticate");
|
||||
}
|
||||
|
||||
struct blkio_dev *capsule_zuse_boot_attached_dev(void) {
|
||||
return g_zuse_attached_this_device ? g_zuse_attached_dev : (struct blkio_dev *) 0;
|
||||
}
|
||||
|
||||
@@ -607,7 +607,14 @@ void mama_word_kill(VM *vm)
|
||||
void mama_word_eject(VM *vm)
|
||||
{
|
||||
capsule_wirebind_eject();
|
||||
capsule_zuse_boot_logout(vm);
|
||||
/* FABRIC-3.md §VII follow-on, 2026-09-06: capsule_zuse_boot_logout()
|
||||
* now requires the departing device to match the one tracked as
|
||||
* hers (the abrupt hot-unplug path's own fix) -- EJECT isn't reacting
|
||||
* to any specific device's detach event, so it passes her own tracked
|
||||
* device straight back in, which trivially matches when she's
|
||||
* genuinely attached and no-ops via the existing g_zuse_attached_
|
||||
* this_device check otherwise. */
|
||||
capsule_zuse_boot_logout(vm, capsule_zuse_boot_attached_dev());
|
||||
/* Stack clean on exit */
|
||||
}
|
||||
|
||||
@@ -926,6 +933,10 @@ static void mama_word_mint(VM *vm)
|
||||
case MINT_ERR_INVALID_PROFILE:
|
||||
console_println("MINT: refused -- full_name/username missing or a field too long");
|
||||
break;
|
||||
case MINT_ERR_VERIFY_FAILED:
|
||||
console_println("MINT: FAILED -- wrote identity but post-write verification failed "
|
||||
"(see log for which check)");
|
||||
break;
|
||||
}
|
||||
vm_push(vm, 0);
|
||||
}
|
||||
|
||||
@@ -936,11 +936,16 @@ static void kernel_main_deep(BootInfo *boot_info) {
|
||||
* (Kconfig.heartbeat) rather than adding a second, overlapping one.
|
||||
* When off, sk_repl_headless_wait() runs the same idle-tick services
|
||||
* (heartbeat, USB/WIREBIND/Zuse-attach detection) with no banner, no
|
||||
* prompt, no input surface at all, until sk_console_mark_login()
|
||||
* fires from either login path -- neither is treated as special, per
|
||||
* direct instruction. When on (the debug/recovery escape hatch),
|
||||
* this is skipped entirely and the console shows up immediately,
|
||||
* exactly as before this change. */
|
||||
* prompt, no input surface at all, until a real identity is attached
|
||||
* via either login path -- neither is treated as special, per direct
|
||||
* instruction. This is only the boot-time gate; sk_repl_run()'s own
|
||||
* main loop (repl.c) re-checks the same live condition on every
|
||||
* iteration too, so the console goes silent again after any later
|
||||
* full logout mid-boot, not just before the first-ever login (2026-
|
||||
* 09-06 revision -- see sk_console_identity_present()'s own doc
|
||||
* comment in repl.c for the live bug this closes). When on (the
|
||||
* debug/recovery escape hatch), this is skipped entirely and the
|
||||
* console shows up immediately, exactly as before this change. */
|
||||
#if !EMERGENCY_CONSOLE_ENABLED
|
||||
sk_repl_headless_wait(mama);
|
||||
#endif
|
||||
|
||||
+99
-19
@@ -99,16 +99,26 @@ VM *sk_repl_get_active_vm(void) { return g_repl_active_vm; }
|
||||
|
||||
/*===========================================================================
|
||||
* Headless-until-login gate, decided 2026-09-05: no console for the
|
||||
* running system unless a thumbdrive is present. One shared flag, set by
|
||||
* either login path (capsule_wirebind.c's regular-user console-VM birth,
|
||||
* capsule_zuse_boot.c's own attach/genesis-mint) -- neither is special,
|
||||
* per direct instruction. See repl.h's own doc comments.
|
||||
*===========================================================================*/
|
||||
|
||||
static int g_console_login_occurred = 0;
|
||||
|
||||
void sk_console_mark_login(void) { g_console_login_occurred = 1; }
|
||||
int sk_console_login_occurred(void) { return g_console_login_occurred; }
|
||||
* running system unless a thumbdrive is present.
|
||||
*
|
||||
* Revised 2026-09-06: this was originally a one-way sticky flag
|
||||
* (sk_console_mark_login(), set once by either login path and never
|
||||
* cleared), gating only the very first entry into sk_repl_run() at boot.
|
||||
* That let a real security gap through, found live during this session's
|
||||
* own repeated identity-verification workflow: once anyone logged in even
|
||||
* once, the console stayed visible for the rest of the boot -- a later
|
||||
* full logout (nobody attached at all) fell through to a bare,
|
||||
* unauthenticated "ok>" instead of going silent again. sk_console_
|
||||
* identity_present() replaces the sticky flag with a live check (mirrors
|
||||
* sk_print_prompt()'s own zuse_session/WIREBIND-username check exactly),
|
||||
* and sk_repl_run()'s own main loop now re-checks it every iteration, not
|
||||
* just once before the loop starts -- see its own call site below. */
|
||||
static int sk_console_identity_present(void) {
|
||||
VM *mama_vm = (VM *)sk_get_mama_vm();
|
||||
if (mama_vm && mama_vm->zuse_session) return 1;
|
||||
if (capsule_wirebind_attached_username() != (const char *)0) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*===========================================================================
|
||||
* Currently attached home-blocks device: mirrors g_repl_active_vm's own
|
||||
@@ -377,13 +387,18 @@ static void sk_repl_idle(VM *active_vm)
|
||||
/* FABRIC-2.md §F.10 decision 2 (UNCLEAN, closed alongside EJECT):
|
||||
* the device is already gone -- no-op if WIREBIND never had
|
||||
* anything tracked (general-purpose USB use, not a home-blocks
|
||||
* identity drive). */
|
||||
capsule_wirebind_unclean_detach();
|
||||
* identity drive), OR if the device that left wasn't the one
|
||||
* WIREBIND tracks (FABRIC-3.md §VII follow-on, 2026-09-06 --
|
||||
* genuine multi-device attach means it might be a different
|
||||
* device leaving while a WIREBIND user's own stays attached). */
|
||||
capsule_wirebind_unclean_detach(usb_blk_dev);
|
||||
|
||||
/* FABRIC-2.md §I.8, re-scoped 2026-09-04: Zuse logs out on device
|
||||
* removal exactly like a WIREBIND user -- no-op if the device
|
||||
* that just left wasn't hers. */
|
||||
capsule_zuse_boot_logout((VM *)sk_get_mama_vm());
|
||||
* that just left wasn't hers (FABRIC-3.md §VII follow-on,
|
||||
* 2026-09-06: that no-op is now real, see capsule_zuse_boot_
|
||||
* logout()'s own updated doc comment). */
|
||||
capsule_zuse_boot_logout((VM *)sk_get_mama_vm(), usb_blk_dev);
|
||||
}
|
||||
|
||||
/* FABRIC-0.md/FABRIC-1.md Section V item 6: "a cheap 'anything dirty?
|
||||
@@ -630,11 +645,15 @@ int sk_console_getkey(VM *active_vm)
|
||||
* shape as sk_console_getkey() above, minus the key-reading entirely: no
|
||||
* banner, no prompt, no console_getc()/readline of any kind -- this is
|
||||
* exactly the "no console for the running system unless a thumbdrive is
|
||||
* present" boundary, decided 2026-09-05. Exits the moment
|
||||
* sk_console_login_occurred() becomes true. */
|
||||
* present" boundary, decided 2026-09-05. Exits the moment sk_console_
|
||||
* identity_present() becomes true -- called both once at boot
|
||||
* (kernel_main.c, before the first ever login) and again from inside
|
||||
* sk_repl_run()'s own main loop whenever the last attached identity logs
|
||||
* out mid-boot (2026-09-06 revision, see sk_console_identity_present()'s
|
||||
* own doc comment for why the boot-only version wasn't enough). */
|
||||
void sk_repl_headless_wait(VM *mama)
|
||||
{
|
||||
while (!sk_console_login_occurred()) {
|
||||
while (!sk_console_identity_present()) {
|
||||
heartbeat_service();
|
||||
uint64_t now = heartbeat_ticks();
|
||||
if (now - g_last_beat_tick >= SK_IDLE_BEAT_INTERVAL) {
|
||||
@@ -664,7 +683,17 @@ int sk_console_key_available(void)
|
||||
* Non-blocking poll of console_getc(). While no character is ready the idle
|
||||
* spin services the adaptive heartbeat at SK_IDLE_BEAT_INTERVAL tick cadence.
|
||||
* Supports backspace (0x7F and \b) and ignores other control characters.
|
||||
* Returns the number of characters placed in buf (not counting '\0').
|
||||
* Returns the number of characters placed in buf (not counting '\0'), or
|
||||
* -1 (2026-09-06) when called with reanchor_prompt nonzero and the
|
||||
* identity that was attached when the caller's prompt was printed logs
|
||||
* out while this call is still blocked waiting for input with nothing yet
|
||||
* typed (n == 0) -- callers with reanchor_prompt nonzero (the REPL's own
|
||||
* top-level prompt sites) must check for this and route back to
|
||||
* sk_repl_headless_wait() rather than treating it as an empty line; buf
|
||||
* is left as an empty string in this case too, matching a real empty
|
||||
* line, so a caller that doesn't check the return value degrades to the
|
||||
* pre-fix behavior (an extra harmless " ok") rather than misbehaving.
|
||||
* shim.c's fgets() (reanchor_prompt == 0) never receives -1.
|
||||
*
|
||||
* Public (declared in repl.h): shim.c's fgets()/QUERY's own real body call
|
||||
* this directly -- same line-editing behavior for a mid-word EXPECT/QUERY as
|
||||
@@ -766,6 +795,27 @@ int sk_console_readline(char* buf, int size, VM* active_vm, int reanchor_prompt)
|
||||
* unchanged, so the final state after the chatter dies down is
|
||||
* a fresh prompt on the last visible line, cursor on it.
|
||||
*/
|
||||
/* Headless-until-login gate, 2026-09-06: the identity that was
|
||||
* attached when the caller printed its prompt (sk_print_prompt(),
|
||||
* reflected in reanchor_prompt callers only -- shim.c's fgets()
|
||||
* passes 0 and is unaffected) may have logged out while we sat
|
||||
* here blocked waiting for input -- WIREBIND EJECT/unclean
|
||||
* detach, or Zuse's own logout, both reachable from
|
||||
* sk_repl_idle() just above. Re-printing the prompt in that
|
||||
* case (the block below) would just show a *correct* bare
|
||||
* "ok>" -- true to current state, but still an unauthenticated
|
||||
* interactive surface sitting on screen, which the headless-
|
||||
* until-login design (Kconfig.heartbeat's EMERGENCY_CONSOLE_
|
||||
* ENABLED) exists specifically to prevent. Bail out instead so
|
||||
* the caller (sk_repl_run()'s own main loop) can drop back into
|
||||
* sk_repl_headless_wait() -- confirmed live as a real gap
|
||||
* before this fix (a bare, unauthenticated prompt stayed on
|
||||
* screen after every logout for the rest of the boot). n == 0
|
||||
* only: never abandon a line the user is actively typing. */
|
||||
if (reanchor_prompt && n == 0 && !sk_console_identity_present()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (reanchor_prompt && n == 0 &&
|
||||
console_tx_count() != prompt_tx_mark)
|
||||
{
|
||||
@@ -1005,6 +1055,22 @@ void sk_repl_run(VM *vm)
|
||||
vm->halted = 0;
|
||||
|
||||
while (!vm->halted) {
|
||||
#if !EMERGENCY_CONSOLE_ENABLED
|
||||
/* Headless-until-login gate, revised 2026-09-06: re-checked every
|
||||
* iteration, not just once before this loop starts (kernel_main.c's
|
||||
* own sk_repl_headless_wait() call, still in place, only covers the
|
||||
* very first login of the boot). Whoever was attached may have
|
||||
* logged out since the last iteration (WIREBIND EJECT/unclean
|
||||
* detach, Zuse's own logout) -- if nobody is attached right now,
|
||||
* go back to silent waiting instead of falling through to a bare,
|
||||
* unauthenticated prompt. See sk_console_identity_present()'s own
|
||||
* doc comment for the live bug this closes. */
|
||||
if (!sk_console_identity_present()) {
|
||||
sk_repl_headless_wait(vm);
|
||||
if (vm->halted) break;
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
/* USE may redirect input to a different VM each iteration */
|
||||
active = g_repl_active_vm ? g_repl_active_vm : vm;
|
||||
|
||||
@@ -1017,7 +1083,21 @@ void sk_repl_run(VM *vm)
|
||||
* sk_print_prompt() extends this with a "(user)" segment. */
|
||||
sk_print_prompt();
|
||||
|
||||
sk_console_readline(input, sizeof(input), active, 1);
|
||||
int n = sk_console_readline(input, sizeof(input), active, 1);
|
||||
|
||||
#if !EMERGENCY_CONSOLE_ENABLED
|
||||
/* n < 0: sk_console_readline() bailed out because the identity
|
||||
* that was attached when this prompt was printed logged out
|
||||
* while we were still blocked waiting for input (2026-09-06 --
|
||||
* see sk_console_readline()'s own doc comment on this return
|
||||
* value). No " ok" here -- nothing was typed, nothing ran --
|
||||
* just loop back to the top, where the check above re-enters
|
||||
* headless silence immediately instead of showing yet another
|
||||
* prompt first. */
|
||||
if (n < 0) {
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (input[0] == '\0') {
|
||||
console_puts(" ok\n");
|
||||
|
||||
@@ -23,17 +23,37 @@
|
||||
/*
|
||||
* platform/alloc_kernel.c - Kernel (bare-metal) memory allocator
|
||||
*
|
||||
* Static arena with bump allocation. Free is a no-op.
|
||||
* This is appropriate for kernel use where:
|
||||
* - VM is long-lived (no restart)
|
||||
* - Allocations happen at init time
|
||||
* - Runtime allocations are rare
|
||||
* Static arena, bump-allocated with a real free list on top.
|
||||
*
|
||||
* FABRIC-3.md §VII follow-on, 2026-09-06: this used to bump-allocate only,
|
||||
* with sf_free() a deliberate no-op -- "VM is long-lived (no restart),
|
||||
* allocations happen at init time, runtime allocations are rare... no
|
||||
* fragmentation issues in practice." That premise held until this
|
||||
* session's own repeated identity-verification workflow (WIREBIND
|
||||
* birth/kill cycles, one console+user VM pair per identity, all sharing
|
||||
* this one global arena) needed VMs born and killed repeatedly within a
|
||||
* single boot -- confirmed live: a real kernel PANIC ("malloc failed"
|
||||
* cascading into "Stadium: eviction... governor invariant broken", full
|
||||
* halt) at the exact same cycle count on every run, because freed
|
||||
* dictionary entries (vm_cleanup() was itself also missing this free
|
||||
* before an earlier pass of this same fix) were never actually reclaimed
|
||||
* -- sf_free() threw them away regardless.
|
||||
*
|
||||
* Every allocation now carries a small header (size + free-list link) so
|
||||
* a freed block can be pushed onto g_free_list and reused by a
|
||||
* later sf_malloc() of equal or smaller size (first-fit, no splitting --
|
||||
* deliberately simple: this workload's repeated allocations are for the
|
||||
* same capsules loaded into a fresh VM each time, so freed blocks from a
|
||||
* just-killed VM's dictionary are typically an exact or near-exact fit
|
||||
* for the next VM's own). Falls back to bump-allocating a fresh block
|
||||
* from the arena when no free block is large enough, exactly as before.
|
||||
*
|
||||
* Arena size: 4MB by default (configurable via SF_ARENA_SIZE)
|
||||
*/
|
||||
|
||||
#include "platform_alloc.h"
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#ifndef SF_ARENA_SIZE
|
||||
#define SF_ARENA_SIZE (4 * 1024 * 1024) /* 4MB default */
|
||||
@@ -43,10 +63,22 @@
|
||||
#define SF_ALIGN 8
|
||||
#define SF_ALIGN_UP(x) (((x) + (SF_ALIGN - 1)) & ~(SF_ALIGN - 1))
|
||||
|
||||
/* Per-allocation header, immediately before the pointer sf_malloc()
|
||||
* returns. `next` is meaningful only while the block is on the free
|
||||
* list -- it's live/garbage data for the caller otherwise, matching the
|
||||
* classic free-list-node-in-freed-space technique, just kept as a fixed
|
||||
* header field instead of reusing payload bytes so there's no minimum-
|
||||
* payload-size constraint to worry about. */
|
||||
typedef struct sf_block_header {
|
||||
size_t size; /* payload size, in bytes, SF_ALIGN_UP'd */
|
||||
struct sf_block_header *next; /* free-list link; valid only while free */
|
||||
} sf_block_header_t;
|
||||
|
||||
/* Static arena */
|
||||
static uint8_t g_arena[SF_ARENA_SIZE] __attribute__((aligned(SF_ALIGN)));
|
||||
static size_t g_arena_offset = 0;
|
||||
static int g_initialized = 0;
|
||||
static sf_block_header_t *g_free_list = (sf_block_header_t *)0;
|
||||
|
||||
/* Statistics */
|
||||
static sf_alloc_stats_t g_stats = {0};
|
||||
@@ -64,6 +96,7 @@ static sf_alloc_stats_t g_stats = {0};
|
||||
int sf_alloc_init(void)
|
||||
{
|
||||
g_arena_offset = 0;
|
||||
g_free_list = (sf_block_header_t *)0;
|
||||
g_initialized = 1;
|
||||
|
||||
g_stats.total_bytes = SF_ARENA_SIZE;
|
||||
@@ -76,27 +109,55 @@ int sf_alloc_init(void)
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief Allocate memory from the static kernel arena.
|
||||
* @brief Allocate memory from the static kernel arena, reusing a freed
|
||||
* block first if one is large enough.
|
||||
*
|
||||
* Bump-allocates @p size bytes from @c g_arena, rounding up to @c SF_ALIGN
|
||||
* (8 bytes) to preserve alignment for 64-bit values. Lazily calls
|
||||
* @c sf_alloc_init() on the first invocation if the arena has not been
|
||||
* Rounds @p size up to @c SF_ALIGN (8 bytes). First searches @c
|
||||
* g_free_list for the first block whose payload is >= the requested size
|
||||
* (first-fit, no splitting -- see this file's own top-of-file doc comment
|
||||
* for why that's the right tradeoff for this workload) and reuses it
|
||||
* whole if found. Otherwise bump-allocates a fresh header+payload block
|
||||
* from @c g_arena, exactly as before this fix. Lazily calls @c
|
||||
* sf_alloc_init() on the first invocation if the arena has not been
|
||||
* explicitly initialised. Returns @c NULL for zero-size requests and when
|
||||
* the arena is exhausted.
|
||||
*
|
||||
* @note Because this is a bump allocator there is no reclaim path; once the
|
||||
* arena is full it stays full until the kernel is reset.
|
||||
* neither a free block nor remaining arena space can satisfy the request.
|
||||
*
|
||||
* @param size Number of bytes to allocate.
|
||||
* @return Pointer to the allocated block on success, @c NULL on failure.
|
||||
* @return Pointer to the allocated block's payload on success, @c NULL on
|
||||
* failure.
|
||||
*/
|
||||
void* sf_malloc(size_t size)
|
||||
{
|
||||
if (!g_initialized) sf_alloc_init();
|
||||
if (size == 0) return (void*)0;
|
||||
|
||||
size_t aligned_size = SF_ALIGN_UP(size);
|
||||
size_t new_offset = g_arena_offset + aligned_size;
|
||||
size_t payload = SF_ALIGN_UP(size);
|
||||
|
||||
/* First-fit scan of the free list. */
|
||||
sf_block_header_t **pp = &g_free_list;
|
||||
while (*pp)
|
||||
{
|
||||
if ((*pp)->size >= payload)
|
||||
{
|
||||
sf_block_header_t *blk = *pp;
|
||||
*pp = blk->next;
|
||||
blk->next = (sf_block_header_t *)0;
|
||||
|
||||
g_stats.used_bytes += blk->size;
|
||||
g_stats.alloc_count++;
|
||||
if (g_stats.used_bytes > g_stats.peak_bytes)
|
||||
{
|
||||
g_stats.peak_bytes = g_stats.used_bytes;
|
||||
}
|
||||
return (void *)(blk + 1);
|
||||
}
|
||||
pp = &(*pp)->next;
|
||||
}
|
||||
|
||||
/* No free block large enough -- bump-allocate a fresh one. */
|
||||
size_t header_size = SF_ALIGN_UP(sizeof(sf_block_header_t));
|
||||
size_t total = header_size + payload;
|
||||
size_t new_offset = g_arena_offset + total;
|
||||
|
||||
if (new_offset > SF_ARENA_SIZE)
|
||||
{
|
||||
@@ -104,17 +165,19 @@ void* sf_malloc(size_t size)
|
||||
return (void*)0;
|
||||
}
|
||||
|
||||
void* ptr = &g_arena[g_arena_offset];
|
||||
sf_block_header_t *blk = (sf_block_header_t *)&g_arena[g_arena_offset];
|
||||
g_arena_offset = new_offset;
|
||||
blk->size = payload;
|
||||
blk->next = (sf_block_header_t *)0;
|
||||
|
||||
g_stats.used_bytes = g_arena_offset;
|
||||
g_stats.used_bytes += payload;
|
||||
g_stats.alloc_count++;
|
||||
if (g_stats.used_bytes > g_stats.peak_bytes)
|
||||
{
|
||||
g_stats.peak_bytes = g_stats.used_bytes;
|
||||
}
|
||||
|
||||
return ptr;
|
||||
return (void *)(blk + 1);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -195,35 +258,33 @@ void* sf_realloc(void* ptr, size_t new_size)
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief Release a kernel arena allocation (no-op).
|
||||
* @brief Release a kernel arena allocation, making it available for reuse.
|
||||
*
|
||||
* The kernel bump allocator has no reclaim mechanism — once bytes are
|
||||
* allocated from @c g_arena they remain consumed until the kernel resets.
|
||||
* This function exists solely to satisfy the @c sf_free() contract expected
|
||||
* by shared VM code, and to keep @c g_stats.free_count accurate for
|
||||
* diagnostic purposes.
|
||||
*
|
||||
* Callers must not assume that freed memory is reclaimed or reusable.
|
||||
* FABRIC-3.md §VII follow-on, 2026-09-06: this used to be a documented
|
||||
* no-op (see this file's own top-of-file doc comment for why that
|
||||
* stopped being acceptable). Pushes the block's header onto @c
|
||||
* g_free_list, where a future @c sf_malloc() of equal or smaller size
|
||||
* will find and reuse it -- no coalescing with neighboring free blocks,
|
||||
* matching the same simplicity tradeoff @c sf_malloc()'s first-fit search
|
||||
* makes.
|
||||
*
|
||||
* @param ptr Pointer previously returned by @c sf_malloc() / @c sf_calloc()
|
||||
* (may be @c NULL; silently ignored).
|
||||
* (may be @c NULL; silently ignored). Must not be used again
|
||||
* by the caller after this call, and must not be freed twice.
|
||||
*/
|
||||
void sf_free(void* ptr)
|
||||
{
|
||||
/* Bump allocator: free is a no-op.
|
||||
*
|
||||
* This is acceptable because:
|
||||
* 1. VM allocations happen at init time
|
||||
* 2. VM runs until power-off
|
||||
* 3. No fragmentation issues in practice
|
||||
*
|
||||
* If needed, could implement a simple free list here.
|
||||
*/
|
||||
if (ptr)
|
||||
if (!ptr) return;
|
||||
|
||||
sf_block_header_t *blk = ((sf_block_header_t *)ptr) - 1;
|
||||
blk->next = g_free_list;
|
||||
g_free_list = blk;
|
||||
|
||||
if (g_stats.used_bytes >= blk->size)
|
||||
{
|
||||
g_stats.free_count++;
|
||||
g_stats.used_bytes -= blk->size;
|
||||
}
|
||||
(void)ptr;
|
||||
g_stats.free_count++;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -321,6 +321,35 @@ void vm_cleanup(VM* vm)
|
||||
vm->ssm_config = NULL;
|
||||
}
|
||||
|
||||
/* FABRIC-3.md §VII follow-on, 2026-09-06: every DictEntry is its own
|
||||
* sf_malloc() (vm_create_word(), dictionary_management.c) -- separate
|
||||
* from vm->memory entirely, so freeing that arena below never touched
|
||||
* them. Neither did anything else in this function, or anywhere else
|
||||
* in the codebase (confirmed by grep before writing this fix) -- every
|
||||
* word a VM ever defined leaked permanently on kill. Never noticed
|
||||
* before: the hosted binary normally only calls this once at process
|
||||
* exit (the OS reclaims everything anyway), and kernel VMs were
|
||||
* normally born once and kept alive for a whole boot, not repeatedly
|
||||
* born and killed -- confirmed live as a real PANIC ("malloc failed"
|
||||
* cascading into "Stadium: eviction... governor invariant broken",
|
||||
* full halt) during this session's own repeated identity-verification
|
||||
* workflow (8 consecutive WIREBIND birth/kill cycles in one boot).
|
||||
* transition_metrics (vm_create_word()'s own second, per-entry
|
||||
* sf_malloc()) must be freed too, before the entry itself -- freeing
|
||||
* entry first would leave no way to reach it. */
|
||||
{
|
||||
DictEntry *dict_entry = vm->latest;
|
||||
while (dict_entry) {
|
||||
DictEntry *next_entry = dict_entry->link;
|
||||
if (dict_entry->transition_metrics) {
|
||||
sf_free(dict_entry->transition_metrics);
|
||||
}
|
||||
sf_free(dict_entry);
|
||||
dict_entry = next_entry;
|
||||
}
|
||||
vm->latest = NULL;
|
||||
}
|
||||
|
||||
if (vm->memory)
|
||||
{
|
||||
vm_host_free(vm, vm->memory);
|
||||
|
||||
Reference in New Issue
Block a user