|
- #include "guard.h"
- #include <assert.h>
- #include <string.h>
- #include <stdlib.h>
- #include <inttypes.h>
-
- static unsigned checks;
- #define CHECK(x) do { if (!(x)) { fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #x); exit(1); } checks++; } while (0)
- static const char *uuid = "11111111-1111-4111-8111-111111111111";
-
- static config_t allowed(void)
- {
- config_t c; defaults(&c);
- c.nzones = 1; strcpy(c.zones[0].uuid, uuid);
- strcpy(c.zones[0].alias, "fixture-web"); c.zones[0].policy = ALLOW;
- return c;
- }
- static process_t process(int pid)
- {
- process_t p; memset(&p, 0, sizeof (p));
- p.pid = pid; p.ppid = 100; p.zid = 9; p.initpid = 99; p.nlwp = 1;
- p.zone_generation = 20; p.start_sec = 1000; p.start_nsec = 123;
- p.rss_kib = 1048576; strcpy(p.uuid, uuid); strcpy(p.name, "php-fpm");
- return p;
- }
- static sample_t sample(double at, uint64_t free_pages, uint64_t scanner)
- {
- sample_t s; memset(&s, 0, sizeof (s)); s.at = at; s.free = free_pages;
- s.lots = 130875; s.des = 65437; s.min = 49077; s.scan = scanner;
- return s;
- }
- static void step(engine_t *e, config_t *c, double at, uint64_t free_pages, uint64_t scanner)
- {
- sample_t s = sample(at, free_pages, scanner); delta_t d = {0, 0, 0};
- engine_step(e, c, &s, d, d, d);
- }
- static int parse(const char *text, config_t *c)
- {
- FILE *f = tmpfile(); int result; char error[128];
- CHECK(f != NULL); CHECK(fputs(text, f) >= 0); rewind(f);
- result = config_read(f, c, error, sizeof (error)); fclose(f); return result;
- }
- static void configs(void)
- {
- config_t c;
- CHECK(parse("# defaults\n", &c) == 0 && !c.armed && c.pre_s == 1 &&
- c.pressure_s == 2 && c.max_cached_candidate_age_s == 15 &&
- c.max_emergency_victims == 8 && c.max_floor_victims == 12);
- CHECK(parse("ACTION_MODE dry-run\nprotect_process postgres\n", &c) == 0);
- CHECK(c.syslog_enabled && c.syslog_facility == 5);
- CHECK(parse("SYSLOG_ENABLED no\nSYSLOG_FACILITY local0\n", &c) == 0 &&
- !c.syslog_enabled && c.syslog_facility == 0);
- CHECK(parse("SYSLOG_ENABLED yes\nSYSLOG_FACILITY local7\n", &c) == 0 &&
- c.syslog_enabled && c.syslog_facility == 7);
- CHECK(parse("SYSLOG_ENABLED maybe\n", &c) != 0);
- CHECK(parse("SYSLOG_FACILITY daemon\n", &c) != 0);
- CHECK(parse("SYSLOG_FACILITY local8\n", &c) != 0);
- CHECK(parse("SYSLOG_FACILITY local5\nSYSLOG_FACILITY local6\n", &c) != 0);
- CHECK(parse("ACTION_MODE armd\n", &c) != 0);
- CHECK(parse("ACTION_MODE armed\n", &c) != 0);
- CHECK(parse("ACTION_MODE dry-run\nACTION_MODE armed\n", &c) != 0);
- CHECK(parse("LOG_KEEP 0\n", &c) != 0);
- CHECK(parse("TERM_GRACE_SECONDS -1\n", &c) != 0);
- CHECK(parse("TERM_GRACE_SECONDS 1 garbage\n", &c) != 0);
- CHECK(parse("CRITICAL_TERM_GRACE_SECONDS 4\nEMERGENCY_TERM_GRACE_SECONDS 1\n"
- "FLOOR_KILL_DELAY_SECONDS 0\nMAX_CACHED_CANDIDATE_AGE_SECONDS 15\n", &c) == 0);
- CHECK(parse("MAX_EMERGENCY_VICTIMS_EPISODE 3\n", &c) != 0);
- CHECK(parse("MAX_FLOOR_VICTIMS_EPISODE 7\n", &c) != 0);
- CHECK(parse("MAX_FLOOR_VICTIMS_EPISODE 16\n", &c) != 0);
- CHECK(parse("MAX_HARD_FLOOR_VICTIMS_EPISODE 16\n", &c) == 0 && c.max_hard_floor_victims == 16);
- CHECK(parse("MAX_HARD_FLOOR_VICTIMS_EPISODE 17\n", &c) != 0);
- CHECK(parse("DEADMAN_RISK_MARGIN_PAGES 1024\nDEADMAN_RISK_SECONDS 5\n"
- "DEADMAN_RISK_SAMPLES 6\nDEADMAN_RISK_NSCAN_ACTIVE_SAMPLES 3\n"
- "DEADMAN_RISK_ANONPGOUT_ACTIVE_SAMPLES 4\n", &c) == 0);
- CHECK(parse("DEADMAN_RISK_SAMPLES 0\n", &c) != 0);
- CHECK(parse("DEADMAN_RISK_SAMPLES 33\n", &c) != 0);
- CHECK(parse("DEADMAN_RISK_SECONDS 0\n", &c) != 0);
- CHECK(parse("DEADMAN_RISK_MARGIN_PAGES -1\n", &c) != 0);
- CHECK(parse("DEADMAN_RISK_NSCAN_ACTIVE_SAMPLES 7\n", &c) != 0);
- CHECK(parse("DEADMAN_RISK_ANONPGOUT_ACTIVE_SAMPLES 6\n", &c) != 0);
- CHECK(parse("DEADMAN_RISK_SECONDS 5\nDEADMAN_RISK_SECONDS 5\n", &c) != 0);
- CHECK(parse("MAX_HARD_FLOOR_VICTIMS_EPISODE 11\n", &c) != 0);
- CHECK(parse("MAX_HARD_FLOOR_VICTIMS_EPISODE 16\nMAX_HARD_FLOOR_VICTIMS_EPISODE 16\n", &c) != 0);
- CHECK(parse("UNKNOWN foo\n", &c) != 0);
- CHECK(parse("PRE_SECONDS 20\nPRESSURE_SECONDS 10\n", &c) != 0);
- CHECK(parse("zone global global allow\n", &c) != 0);
- CHECK(parse("zone 11111111-1111-4111-8111-111111111111 web allow\nACTION_MODE armed\n", &c) == 0 && c.armed);
- CHECK(parse("zone 11111111-1111-4111-8111-111111111111 web allow\nzone 11111111-1111-4111-8111-111111111111 db protect\n", &c) != 0);
- CHECK(!uuid_valid("11111111-1111-4111-8111-11111111111g"));
- CHECK(!uuid_valid("00000000-0000-0000-0000-000000000000"));
- {
- FILE *f = fopen("etc/o6h-memory-guard.conf", "r"); char error[128];
- CHECK(f != NULL); CHECK(config_read(f, &c, error, sizeof (error)) == 0);
- fclose(f); CHECK(!c.armed && c.pre_s == 1 && c.pressure_s == 2);
- }
- puts("PASS strict configuration, safe defaults, explicit UUID authorization");
- }
- static void protections(void)
- {
- config_t c = allowed(); process_t p = process(123), q;
- CHECK(!protected_process(&c, &p));
- q = p; q.zid = 0; CHECK(protected_process(&c, &q));
- q = p; q.pid = 1; CHECK(protected_process(&c, &q));
- q = p; q.pid = q.initpid; CHECK(protected_process(&c, &q));
- q = p; q.initpid = 0; CHECK(protected_process(&c, &q));
- q = p; q.zone_generation = 0; CHECK(protected_process(&c, &q));
- q = p; q.nlwp = 0; CHECK(protected_process(&c, &q));
- q = p; q.ppid = 0; CHECK(protected_process(&c, &q));
- q = p; strcpy(q.name, "sshd"); CHECK(protected_process(&c, &q));
- q = p; q.rss_kib = 100; CHECK(protected_process(&c, &q));
- q = p; q.uuid[0] = '2'; CHECK(protected_process(&c, &q));
- c.zones[0].policy = PROTECT; CHECK(protected_process(&c, &p));
- c.zones[0].policy = OBSERVE; CHECK(protected_process(&c, &p));
- c.zones[0].policy = ALLOW;
- strcpy(c.protected_names[c.nnames++], "php-fpm"); CHECK(protected_process(&c, &p));
- c.nnames = 0; c.protected_pids[c.npids++] = p.pid; CHECK(protected_process(&c, &p));
- q = p; CHECK(same_process(&p, &q));
- q.start_nsec++; CHECK(!same_process(&p, &q));
- q = p; q.zone_generation++; CHECK(!same_process(&p, &q));
- q = p; q.uuid[0] = '2'; CHECK(!same_process(&p, &q));
- puts("PASS mandatory protections, PID reuse and zone incarnation identity");
- }
- static void histories(void)
- {
- history_t h = {0}; delta_t d; unsigned i;
- for (i = 0; i <= 70; i++) history_add(&h, i, 10000 - 10 * i);
- d = history_delta(&h, 70, 5); CHECK(d.valid && d.value == -50 && d.age == 5);
- d = history_delta(&h, 70, 15); CHECK(d.valid && d.value == -150);
- d = history_delta(&h, 70, 60); CHECK(d.valid && d.value == -600);
- CHECK(!history_delta(&h, 90, 5).valid);
- memset(&h, 0, sizeof (h));
- history_add(&h, 0, 123); history_add(&h, 15, 100);
- CHECK(!history_delta(&h, 15, 5).valid);
- CHECK(history_delta(&h, 15, 15).value == -23);
- d.valid = 1; d.value = 100;
- CHECK(rank_score(1000, d) == 1200);
- CHECK(rank_score(UINT64_MAX - 10, d) == UINT64_MAX);
- d.value = -100; CHECK(rank_score(1000, d) == 1000);
- {
- uint64_t bytes = 0;
- CHECK(swap_physical_bytes_from_blocks(67024888, 58931824, &bytes) == 0);
- CHECK(bytes == UINT64_C(4143648768));
- CHECK(swap_physical_bytes_from_blocks(10, 11, &bytes) != 0);
- CHECK(swap_physical_bytes_from_blocks(UINT64_MAX, 0, &bytes) != 0);
- }
- puts("PASS timestamped 5/15/60s windows, missing data, signed growth, overflow");
- }
- static void states(void)
- {
- config_t c = allowed(); engine_t e; unsigned i;
- engine_init(&e);
- for (i = 0; i < 10; i++) step(&e, &c, i, 425950, 0);
- CHECK(e.state == NORMAL && !e.danger);
- step(&e, &c, 10, 100000, 0); CHECK(e.state == NORMAL);
- step(&e, &c, 11, 100000, 0); CHECK(e.state == PRE_PRESSURE);
- step(&e, &c, 12, 100000, 0);
- CHECK(e.state == PRESSURE && !e.danger);
- step(&e, &c, 21, 60000, 1);
- CHECK(e.state == CRITICAL && e.danger); /* delayed threshold jump */
- step(&e, &c, 22, 59000, 1); CHECK(e.danger);
- step(&e, &c, 23, 58000, 1); CHECK(e.state == CRITICAL && e.danger);
- step(&e, &c, 24, 140000, 0); CHECK(e.state == RECOVERY && !e.danger);
- for (i = 25; i <= 39; i++) step(&e, &c, i, 140000, 0);
- CHECK(e.state == NORMAL);
- for (i = 40; i <= 56; i++) step(&e, &c, i, 150000, 0);
- CHECK(e.state == NORMAL);
- step(&e, &c, 57, 40000, 0); CHECK(e.state == EMERGENCY && e.danger && !e.floor);
- step(&e, &c, 58, 20000, 0); CHECK(e.floor);
- step(&e, &c, 59, 70000, 0); CHECK(e.state == RECOVERY && !e.danger);
- engine_init(&e);
- for (i = 0; i < 20; i++) step(&e, &c, i, 60000, 0);
- CHECK(!e.danger); /* low but stable, no scanner */
- engine_init(&e);
- step(&e, &c, 0, 60000, 1); step(&e, &c, 20, 59000, 1);
- CHECK(e.danger); /* monotonic elapsed time survives delayed sampling */
- engine_gap(&e); step(&e, &c, 21, 58000, 1); CHECK(!e.danger);
- step(&e, &c, 22, 57000, 1); step(&e, &c, 23, 56000, 1); CHECK(e.danger);
- engine_init(&e);
- step(&e, &c, 0.0, 150000, 0);
- step(&e, &c, 1.8, 100000, 0); CHECK(e.state == NORMAL);
- step(&e, &c, 4.2, 95000, 0); CHECK(e.state == PRESSURE && !e.danger);
- engine_init(&e);
- step(&e, &c, 0.0, 150000, 0);
- step(&e, &c, 3.1, 65000, 1); CHECK(e.state == CRITICAL && e.danger);
- {
- sample_t s = sample(1, 60000, 0);
- delta_t none = {0, 0, 0}, rising = {1, 5, 4096};
- engine_init(&e); engine_step(&e, &c, &s, none, rising, none);
- CHECK(!e.danger && e.critical_since < 0); /* physical swap alone is inert */
- engine_init(&e); engine_step(&e, &c, &s, none, none, rising);
- CHECK(!e.danger && e.critical_since < 0); /* pageout alone is inert */
- s.free = 150000;
- engine_init(&e); engine_step(&e, &c, &s, none, rising, rising);
- CHECK(e.state == NORMAL && e.low_since < 0); /* corroboration needs low memory */
- s.free = 60000;
- engine_init(&e);
- for (i = 0; i <= 3; i++) {
- s.at = i; engine_step(&e, &c, &s, none, rising, rising);
- }
- CHECK(e.state == CRITICAL && e.danger);
- }
- puts("PASS all six states, monotonic overruns, threshold jumps, corroboration and recovery");
- }
-
- static void successful_avoidance_fixture(void)
- {
- config_t c = allowed(); engine_t e; FILE *f; char line[256], event[64];
- double at, late; uint64_t free_pages, nscan; unsigned rows = 0;
- engine_init(&e);
- f = fopen("tests/fixtures/successful_pageout_deadman_avoidance_2026_09_21.csv", "r");
- CHECK(f != NULL); CHECK(fgets(line, sizeof (line), f) != NULL);
- while (fscanf(f, "%lf,%" SCNu64 ",%" SCNu64 ",%lf,%63s",
- &at, &free_pages, &nscan, &late, event) == 5) {
- step(&e, &c, at, free_pages, nscan); rows++;
- CHECK(late >= 0.0);
- if (!strcmp(event, "emergency")) CHECK(e.state == EMERGENCY && e.danger);
- if (!strcmp(event, "recovery")) CHECK(e.state == RECOVERY && !e.danger);
- if (!strcmp(event, "normal")) CHECK(e.state == NORMAL && !e.danger);
- }
- fclose(f); CHECK(rows == 7); CHECK(e.state == NORMAL);
- puts("PASS successful_pageout_deadman_avoidance_2026_09_21 incident fixture");
- }
- static void real_incident_fixture(void)
- {
- config_t c = allowed(); engine_t e; FILE *f;
- unsigned t, rows = 0; uint64_t free_pages, nscan;
- char line[128];
- engine_init(&e);
- f = fopen("tests/fixtures/pageout_deadman_real_incident_2026_09.csv", "r");
- CHECK(f != NULL); CHECK(fgets(line, sizeof (line), f) != NULL);
- while (fscanf(f, "%u,%" SCNu64 ",%" SCNu64, &t, &free_pages, &nscan) == 3) {
- step(&e, &c, t, free_pages, nscan); rows++;
- if (t == 2) CHECK(e.state == PRE_PRESSURE);
- if (t == 3) CHECK(e.state == PRESSURE);
- }
- fclose(f);
- CHECK(rows == 5); CHECK(e.state == EMERGENCY && e.danger);
- puts("PASS pageout_deadman_real_incident_2026_09 exits NORMAL before minfree");
- }
- static void actions(void)
- {
- config_t c = allowed(); engine_t e; controller_t a = {0};
- process_t p = process(123); sample_t s; plan_t plan; unsigned i;
- engine_init(&e); e.state = CRITICAL; e.danger = 1;
- s = sample(100, 60000, 1);
- plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == TERM);
- action_commit(&a, &c, &s, &plan); CHECK(a.has_pending && a.nvictims == 1);
- printf("SIMULATED t=100 state=CRITICAL UUID=%s pid=123 WOULD_ACTION=SIGTERM\n", uuid);
- s.at = 104; plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == NONE);
- s.at = 105; plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
- action_commit(&a, &c, &s, &plan); CHECK(!a.has_pending && a.nvictims == 1);
- puts("SIMULATED t=105 state=CRITICAL pid=123 WOULD_ACTION=SIGKILL reason=memory_not_recovered");
- p.pid++; s.at = 106; CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
- e.state = EMERGENCY; s.free = 40000;
- plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == TERM);
- action_commit(&a, &c, &s, &plan);
- s.at = 106.5; CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
- s.at = 107; CHECK(action_plan(&a, &c, &e, &s, &p).action == KILL);
- s.free = 20000; e.floor = 1;
- plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
- action_commit(&a, &c, &s, &plan);
- p.pid++; s.at++;
- plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
- action_commit(&a, &c, &s, &plan);
- puts("SIMULATED state=EMERGENCY floor=1 WOULD_ACTION=SIGKILL reason=floor");
- p.pid++; s.at++;
- plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
- action_commit(&a, &c, &s, &plan);
- p.pid++; s.at++;
- plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
- CHECK(!strcmp(plan.budget_override, "floor"));
- e.state = NORMAL; e.danger = 0; s.free = 160000;
- e.recovery_since = s.at - c.recovery_s;
- (void)action_plan(&a, &c, &e, &s, &p); CHECK(a.nvictims == 0);
- e.state = EMERGENCY; e.danger = 1; e.floor = 0; s.free = 40000;
- a.nvictims = c.max_victims; a.nrate = c.max_per_minute;
- e.state = CRITICAL;
- CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
- e.state = EMERGENCY;
- a.nvictims = c.max_victims; a.nrate = c.max_per_minute;
- plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == TERM);
- CHECK(!strcmp(plan.budget_override, "emergency"));
- a.nvictims = c.max_emergency_victims;
- CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
- e.floor = 1; s.free = 20000;
- plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
- CHECK(!strcmp(plan.budget_override, "floor"));
- a.nvictims = c.max_floor_victims;
- CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
- memset(&a, 0, sizeof (a)); e.floor = 0; e.state = CRITICAL; s.free = 60000;
- plan = action_plan(&a, &c, &e, &s, &p); action_commit(&a, &c, &s, &plan);
- s.free = 140000; e.danger = 0; e.state = RECOVERY;
- CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE && !a.has_pending);
- for (i = 0; i < 2; i++) {
- c.armed = (int)i; memset(&a, 0, sizeof (a));
- e.danger = 1; e.state = EMERGENCY; e.floor = 1; s.free = 1000;
- p.zid = 0; CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
- p.zid = 9; c.zones[0].policy = PROTECT;
- CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
- c.zones[0].policy = ALLOW;
- }
- puts("PASS TERM/KILL sequencing, emergency grace, floor, cooldown, budgets and no-action protections");
- }
- static void hard_floor_policy(void)
- {
- config_t c = allowed();
- controller_t a;
- engine_t e;
- sample_t s = sample(100, 6513, 1000000);
- process_t p = process(3000);
- delta_t growing = {1, 5, 100}, invalid = {0, 0, 0};
- unsigned i;
- memset(&a, 0, sizeof (a)); engine_init(&e);
- a.nvictims = c.max_floor_victims; s.pageout_ok = 1;
- for (i = 0; i < 6; i++) {
- s.at = 100 + i;
- engine_step(&e, &c, &s, invalid, growing, growing);
- CHECK(e.hard_floor_ready == (i == 5));
- CHECK((action_plan(&a, &c, &e, &s, &p).action == KILL) == (i == 5));
- }
- CHECK(!strcmp(action_plan(&a, &c, &e, &s, &p).budget_override, "hard_floor"));
- a.nvictims = 16;
- CHECK(!strcmp(action_plan(&a, &c, &e, &s, &p).reason, "hard_floor_victim_budget_exhausted"));
- a.nvictims = 12; s.at += 2;
- engine_step(&e, &c, &s, invalid, growing, growing);
- CHECK(!e.hard_floor_ready);
- for (i = 0; i < 8; i++) {
- s.at++; engine_step(&e, &c, &s, invalid, growing, invalid);
- CHECK(!e.hard_floor_ready);
- }
- s.scan = 0;
- for (i = 0; i < 8; i++) {
- s.at++; engine_step(&e, &c, &s, invalid, growing, growing);
- CHECK(!e.hard_floor_ready);
- }
- s.free = 70000; s.at++;
- engine_step(&e, &c, &s, invalid, invalid, invalid);
- CHECK(e.state == RECOVERY);
- (void)action_plan(&a, &c, &e, &s, &p); CHECK(a.nvictims == 12);
- s.at += 2; s.free = 6513;
- engine_step(&e, &c, &s, invalid, invalid, invalid);
- CHECK(e.state == EMERGENCY && !e.hard_floor_ready);
- (void)action_plan(&a, &c, &e, &s, &p); CHECK(a.nvictims == 12);
- engine_gap(&e); s.at++; s.free = s.lots;
- (void)action_plan(&a, &c, &e, &s, &p); CHECK(a.nvictims == 12);
- s.free = 6513; s.at++; engine_step(&e, &c, &s, invalid, invalid, invalid);
- s.free = s.lots;
- for (i = 0; i <= c.recovery_s; i++) {
- s.at++; engine_step(&e, &c, &s, invalid, invalid, invalid);
- (void)action_plan(&a, &c, &e, &s, &p);
- CHECK(a.nvictims == (i == c.recovery_s ? 0u : 12u));
- }
- CHECK(e.state == NORMAL);
- puts("PASS hard-floor persistence, finite ceiling, missing corroboration, gap, brief/stable recovery");
- }
-
- static void deadman_policy(void)
- {
- config_t c = allowed();
- controller_t a;
- engine_t e;
- sample_t s;
- process_t p = process(9000);
- delta_t none = {0, 0, 0}, growing = {1, 5, 100};
- unsigned i, mode;
- memset(&a, 0, sizeof (a)); engine_init(&e); a.nvictims = 8;
- for (i = 0; i < 9; i++) {
- s = sample(100 + i, i == 6 ? 100 : 48500 + i, i == 2 ? 0 : 1000000);
- s.pageout_ok = s.swap_physical_ok = 1;
- s.anon_pageout_pages = 1000 + i; s.swap_physical_used_bytes = 10000 + i;
- engine_step(&e, &c, &s, none, growing, growing);
- (void)action_plan(&a, &c, &e, &s, &p);
- CHECK(e.floor == (i == 6));
- CHECK(e.risk.ready == (i >= 5));
- CHECK(a.override_stage == (i < 5 ? 0u : i < 8 ? 1u : 2u));
- if (i == 5) {
- CHECK(action_limit(&a, &c, &e, &s) == 12);
- a.nvictims = 12;
- }
- if (i == 6 || i == 7) CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
- }
- CHECK(action_limit(&a, &c, &e, &s) == 16);
- CHECK(action_plan(&a, &c, &e, &s, &p).action == TERM);
- s.free = s.min + 1024;
- CHECK(deadman_risk_current(&e, &c, &s));
- s.free++;
- CHECK(!deadman_risk_current(&e, &c, &s));
- s.free = 48500;
- a.nvictims = 16;
- CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
- a.nvictims = 12;
- s.at++; s.free = s.des + 1;
- engine_step(&e, &c, &s, none, none, none);
- CHECK(!e.risk.ready && e.risk.count == 6);
- (void)action_plan(&a, &c, &e, &s, &p);
- CHECK(a.nvictims == 12 && a.override_stage == 2);
- s.at += 2; s.free = 48500; s.anon_pageout_pages++;
- engine_step(&e, &c, &s, none, growing, growing);
- (void)action_plan(&a, &c, &e, &s, &p);
- CHECK(a.nvictims == 12 && a.override_stage == 2 && e.risk.count == 6);
- for (i = 0; i <= c.recovery_s; i++) {
- s.at++; s.free = s.lots + 1; s.scan = 0;
- engine_step(&e, &c, &s, none, none, none);
- (void)action_plan(&a, &c, &e, &s, &p);
- }
- CHECK(e.state == NORMAL && !e.risk.count && !a.nvictims && !a.override_stage);
-
- /* FLOOR alone: neither a fresh episode nor risk stage1 may jump to16. */
- for (mode = 0; mode < 2; mode++) {
- memset(&a, 0, sizeof (a)); engine_init(&e);
- a.nvictims = 12; a.override_stage = mode; a.override_since = 100;
- s = sample(100, 100, 1000000); s.pageout_ok = 1; s.anon_pageout_pages = 100;
- engine_step(&e, &c, &s, none, growing, growing);
- CHECK(e.floor && !e.hard_floor_ready && !e.risk.ready);
- CHECK(action_limit(&a, &c, &e, &s) == 12);
- CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
- a.nvictims = 8;
- CHECK(action_plan(&a, &c, &e, &s, &p).action == KILL);
- }
- /* Negative windows: no paging, no scarcity, brief dip, reset/invalid counter. */
- for (mode = 0; mode < 5; mode++) {
- memset(&a, 0, sizeof (a)); engine_init(&e); a.nvictims = 8;
- for (i = 0; i < 10; i++) {
- s = sample(100 + i, mode == 1 || (mode == 2 && i > 1) ? 140000 : 48500,
- mode == 0 ? 0 : 1000000);
- s.pageout_ok = mode != 4;
- s.anon_pageout_pages = mode == 0 ? 100 : mode == 3 ? 1000 - i : 1000 + i;
- engine_step(&e, &c, &s, none, none, none);
- (void)action_plan(&a, &c, &e, &s, &p);
- CHECK(!e.risk.ready && !a.override_stage);
- }
- }
- /* Real monotonic overruns retain evidence; very old windows cannot authorize. */
- engine_init(&e);
- for (i = 0; i < 6; i++) {
- s = sample(100 + 2 * i, 48500, i == 3 ? 0 : 1000000);
- s.pageout_ok = 1; s.anon_pageout_pages = 100 + i;
- engine_step(&e, &c, &s, none, none, none);
- }
- CHECK(e.risk.ready);
- s.at += 30; s.anon_pageout_pages++;
- engine_step(&e, &c, &s, none, none, none);
- CHECK(!e.risk.ready && e.risk.count == 6);
- c.risk_samples = 32; c.risk_seconds = 31;
- c.risk_scan_samples = c.risk_anon_samples = 30;
- engine_init(&e);
- for (i = 0; i < 40; i++) {
- s = sample(100 + i, 48500, 1000000);
- s.pageout_ok = 1; s.anon_pageout_pages = 1000 + i;
- engine_step(&e, &c, &s, none, none, none);
- }
- CHECK(e.risk.ready && e.risk.count == 32);
- s.anon_pageout_pages = 0;
- CHECK(!deadman_risk_current(&e, &c, &s));
- s.at = 90; engine_step(&e, &c, &s, none, none, none);
- CHECK(!e.risk.ready && e.risk.count == 1);
- puts("PASS deadman window, staged8/12/16, recovery, overruns, negatives and single FLOOR never16");
- }
-
- int main(void)
- {
- configs(); protections(); histories(); states(); real_incident_fixture();
- successful_avoidance_fixture(); actions(); hard_floor_policy(); deadman_policy();
- printf("PASS %u checks; no native backend linked, no signals sent\n", checks);
- return 0;
- }
|