Conservative SmartOS global-zone memory pressure guard with dry-run-first policy and SMF integration.
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

467 satır
22 KiB

  1. #include "guard.h"
  2. #include <assert.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <inttypes.h>
  6. static unsigned checks;
  7. #define CHECK(x) do { if (!(x)) { fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #x); exit(1); } checks++; } while (0)
  8. static const char *uuid = "11111111-1111-4111-8111-111111111111";
  9. static config_t allowed(void)
  10. {
  11. config_t c; defaults(&c);
  12. c.nzones = 1; strcpy(c.zones[0].uuid, uuid);
  13. strcpy(c.zones[0].alias, "fixture-web"); c.zones[0].policy = ALLOW;
  14. return c;
  15. }
  16. static process_t process(int pid)
  17. {
  18. process_t p; memset(&p, 0, sizeof (p));
  19. p.pid = pid; p.ppid = 100; p.zid = 9; p.initpid = 99; p.nlwp = 1;
  20. p.zone_generation = 20; p.start_sec = 1000; p.start_nsec = 123;
  21. p.rss_kib = 1048576; strcpy(p.uuid, uuid); strcpy(p.name, "php-fpm");
  22. return p;
  23. }
  24. static sample_t sample(double at, uint64_t free_pages, uint64_t scanner)
  25. {
  26. sample_t s; memset(&s, 0, sizeof (s)); s.at = at; s.free = free_pages;
  27. s.lots = 130875; s.des = 65437; s.min = 49077; s.scan = scanner;
  28. return s;
  29. }
  30. static void step(engine_t *e, config_t *c, double at, uint64_t free_pages, uint64_t scanner)
  31. {
  32. sample_t s = sample(at, free_pages, scanner); delta_t d = {0, 0, 0};
  33. engine_step(e, c, &s, d, d, d);
  34. }
  35. static int parse(const char *text, config_t *c)
  36. {
  37. FILE *f = tmpfile(); int result; char error[128];
  38. CHECK(f != NULL); CHECK(fputs(text, f) >= 0); rewind(f);
  39. result = config_read(f, c, error, sizeof (error)); fclose(f); return result;
  40. }
  41. static void configs(void)
  42. {
  43. config_t c;
  44. CHECK(parse("# defaults\n", &c) == 0 && !c.armed && c.pre_s == 1 &&
  45. c.pressure_s == 2 && c.max_cached_candidate_age_s == 15 &&
  46. c.max_emergency_victims == 8 && c.max_floor_victims == 12);
  47. CHECK(parse("ACTION_MODE dry-run\nprotect_process postgres\n", &c) == 0);
  48. CHECK(c.syslog_enabled && c.syslog_facility == 5);
  49. CHECK(parse("SYSLOG_ENABLED no\nSYSLOG_FACILITY local0\n", &c) == 0 &&
  50. !c.syslog_enabled && c.syslog_facility == 0);
  51. CHECK(parse("SYSLOG_ENABLED yes\nSYSLOG_FACILITY local7\n", &c) == 0 &&
  52. c.syslog_enabled && c.syslog_facility == 7);
  53. CHECK(parse("SYSLOG_ENABLED maybe\n", &c) != 0);
  54. CHECK(parse("SYSLOG_FACILITY daemon\n", &c) != 0);
  55. CHECK(parse("SYSLOG_FACILITY local8\n", &c) != 0);
  56. CHECK(parse("SYSLOG_FACILITY local5\nSYSLOG_FACILITY local6\n", &c) != 0);
  57. CHECK(parse("ACTION_MODE armd\n", &c) != 0);
  58. CHECK(parse("ACTION_MODE armed\n", &c) != 0);
  59. CHECK(parse("ACTION_MODE dry-run\nACTION_MODE armed\n", &c) != 0);
  60. CHECK(parse("LOG_KEEP 0\n", &c) != 0);
  61. CHECK(parse("TERM_GRACE_SECONDS -1\n", &c) != 0);
  62. CHECK(parse("TERM_GRACE_SECONDS 1 garbage\n", &c) != 0);
  63. CHECK(parse("CRITICAL_TERM_GRACE_SECONDS 4\nEMERGENCY_TERM_GRACE_SECONDS 1\n"
  64. "FLOOR_KILL_DELAY_SECONDS 0\nMAX_CACHED_CANDIDATE_AGE_SECONDS 15\n", &c) == 0);
  65. CHECK(parse("MAX_EMERGENCY_VICTIMS_EPISODE 3\n", &c) != 0);
  66. CHECK(parse("MAX_FLOOR_VICTIMS_EPISODE 7\n", &c) != 0);
  67. CHECK(parse("MAX_FLOOR_VICTIMS_EPISODE 16\n", &c) != 0);
  68. CHECK(parse("MAX_HARD_FLOOR_VICTIMS_EPISODE 16\n", &c) == 0 && c.max_hard_floor_victims == 16);
  69. CHECK(parse("MAX_HARD_FLOOR_VICTIMS_EPISODE 17\n", &c) != 0);
  70. CHECK(parse("DEADMAN_RISK_MARGIN_PAGES 1024\nDEADMAN_RISK_SECONDS 5\n"
  71. "DEADMAN_RISK_SAMPLES 6\nDEADMAN_RISK_NSCAN_ACTIVE_SAMPLES 3\n"
  72. "DEADMAN_RISK_ANONPGOUT_ACTIVE_SAMPLES 4\n", &c) == 0);
  73. CHECK(parse("DEADMAN_RISK_SAMPLES 0\n", &c) != 0);
  74. CHECK(parse("DEADMAN_RISK_SAMPLES 33\n", &c) != 0);
  75. CHECK(parse("DEADMAN_RISK_SECONDS 0\n", &c) != 0);
  76. CHECK(parse("DEADMAN_RISK_MARGIN_PAGES -1\n", &c) != 0);
  77. CHECK(parse("DEADMAN_RISK_NSCAN_ACTIVE_SAMPLES 7\n", &c) != 0);
  78. CHECK(parse("DEADMAN_RISK_ANONPGOUT_ACTIVE_SAMPLES 6\n", &c) != 0);
  79. CHECK(parse("DEADMAN_RISK_SECONDS 5\nDEADMAN_RISK_SECONDS 5\n", &c) != 0);
  80. CHECK(parse("MAX_HARD_FLOOR_VICTIMS_EPISODE 11\n", &c) != 0);
  81. CHECK(parse("MAX_HARD_FLOOR_VICTIMS_EPISODE 16\nMAX_HARD_FLOOR_VICTIMS_EPISODE 16\n", &c) != 0);
  82. CHECK(parse("UNKNOWN foo\n", &c) != 0);
  83. CHECK(parse("PRE_SECONDS 20\nPRESSURE_SECONDS 10\n", &c) != 0);
  84. CHECK(parse("zone global global allow\n", &c) != 0);
  85. CHECK(parse("zone 11111111-1111-4111-8111-111111111111 web allow\nACTION_MODE armed\n", &c) == 0 && c.armed);
  86. CHECK(parse("zone 11111111-1111-4111-8111-111111111111 web allow\nzone 11111111-1111-4111-8111-111111111111 db protect\n", &c) != 0);
  87. CHECK(!uuid_valid("11111111-1111-4111-8111-11111111111g"));
  88. CHECK(!uuid_valid("00000000-0000-0000-0000-000000000000"));
  89. {
  90. FILE *f = fopen("etc/o6h-memory-guard.conf", "r"); char error[128];
  91. CHECK(f != NULL); CHECK(config_read(f, &c, error, sizeof (error)) == 0);
  92. fclose(f); CHECK(!c.armed && c.pre_s == 1 && c.pressure_s == 2);
  93. }
  94. puts("PASS strict configuration, safe defaults, explicit UUID authorization");
  95. }
  96. static void protections(void)
  97. {
  98. config_t c = allowed(); process_t p = process(123), q;
  99. CHECK(!protected_process(&c, &p));
  100. q = p; q.zid = 0; CHECK(protected_process(&c, &q));
  101. q = p; q.pid = 1; CHECK(protected_process(&c, &q));
  102. q = p; q.pid = q.initpid; CHECK(protected_process(&c, &q));
  103. q = p; q.initpid = 0; CHECK(protected_process(&c, &q));
  104. q = p; q.zone_generation = 0; CHECK(protected_process(&c, &q));
  105. q = p; q.nlwp = 0; CHECK(protected_process(&c, &q));
  106. q = p; q.ppid = 0; CHECK(protected_process(&c, &q));
  107. q = p; strcpy(q.name, "sshd"); CHECK(protected_process(&c, &q));
  108. q = p; q.rss_kib = 100; CHECK(protected_process(&c, &q));
  109. q = p; q.uuid[0] = '2'; CHECK(protected_process(&c, &q));
  110. c.zones[0].policy = PROTECT; CHECK(protected_process(&c, &p));
  111. c.zones[0].policy = OBSERVE; CHECK(protected_process(&c, &p));
  112. c.zones[0].policy = ALLOW;
  113. strcpy(c.protected_names[c.nnames++], "php-fpm"); CHECK(protected_process(&c, &p));
  114. c.nnames = 0; c.protected_pids[c.npids++] = p.pid; CHECK(protected_process(&c, &p));
  115. q = p; CHECK(same_process(&p, &q));
  116. q.start_nsec++; CHECK(!same_process(&p, &q));
  117. q = p; q.zone_generation++; CHECK(!same_process(&p, &q));
  118. q = p; q.uuid[0] = '2'; CHECK(!same_process(&p, &q));
  119. puts("PASS mandatory protections, PID reuse and zone incarnation identity");
  120. }
  121. static void histories(void)
  122. {
  123. history_t h = {0}; delta_t d; unsigned i;
  124. for (i = 0; i <= 70; i++) history_add(&h, i, 10000 - 10 * i);
  125. d = history_delta(&h, 70, 5); CHECK(d.valid && d.value == -50 && d.age == 5);
  126. d = history_delta(&h, 70, 15); CHECK(d.valid && d.value == -150);
  127. d = history_delta(&h, 70, 60); CHECK(d.valid && d.value == -600);
  128. CHECK(!history_delta(&h, 90, 5).valid);
  129. memset(&h, 0, sizeof (h));
  130. history_add(&h, 0, 123); history_add(&h, 15, 100);
  131. CHECK(!history_delta(&h, 15, 5).valid);
  132. CHECK(history_delta(&h, 15, 15).value == -23);
  133. d.valid = 1; d.value = 100;
  134. CHECK(rank_score(1000, d) == 1200);
  135. CHECK(rank_score(UINT64_MAX - 10, d) == UINT64_MAX);
  136. d.value = -100; CHECK(rank_score(1000, d) == 1000);
  137. {
  138. uint64_t bytes = 0;
  139. CHECK(swap_physical_bytes_from_blocks(67024888, 58931824, &bytes) == 0);
  140. CHECK(bytes == UINT64_C(4143648768));
  141. CHECK(swap_physical_bytes_from_blocks(10, 11, &bytes) != 0);
  142. CHECK(swap_physical_bytes_from_blocks(UINT64_MAX, 0, &bytes) != 0);
  143. }
  144. puts("PASS timestamped 5/15/60s windows, missing data, signed growth, overflow");
  145. }
  146. static void states(void)
  147. {
  148. config_t c = allowed(); engine_t e; unsigned i;
  149. engine_init(&e);
  150. for (i = 0; i < 10; i++) step(&e, &c, i, 425950, 0);
  151. CHECK(e.state == NORMAL && !e.danger);
  152. step(&e, &c, 10, 100000, 0); CHECK(e.state == NORMAL);
  153. step(&e, &c, 11, 100000, 0); CHECK(e.state == PRE_PRESSURE);
  154. step(&e, &c, 12, 100000, 0);
  155. CHECK(e.state == PRESSURE && !e.danger);
  156. step(&e, &c, 21, 60000, 1);
  157. CHECK(e.state == CRITICAL && e.danger); /* delayed threshold jump */
  158. step(&e, &c, 22, 59000, 1); CHECK(e.danger);
  159. step(&e, &c, 23, 58000, 1); CHECK(e.state == CRITICAL && e.danger);
  160. step(&e, &c, 24, 140000, 0); CHECK(e.state == RECOVERY && !e.danger);
  161. for (i = 25; i <= 39; i++) step(&e, &c, i, 140000, 0);
  162. CHECK(e.state == NORMAL);
  163. for (i = 40; i <= 56; i++) step(&e, &c, i, 150000, 0);
  164. CHECK(e.state == NORMAL);
  165. step(&e, &c, 57, 40000, 0); CHECK(e.state == EMERGENCY && e.danger && !e.floor);
  166. step(&e, &c, 58, 20000, 0); CHECK(e.floor);
  167. step(&e, &c, 59, 70000, 0); CHECK(e.state == RECOVERY && !e.danger);
  168. engine_init(&e);
  169. for (i = 0; i < 20; i++) step(&e, &c, i, 60000, 0);
  170. CHECK(!e.danger); /* low but stable, no scanner */
  171. engine_init(&e);
  172. step(&e, &c, 0, 60000, 1); step(&e, &c, 20, 59000, 1);
  173. CHECK(e.danger); /* monotonic elapsed time survives delayed sampling */
  174. engine_gap(&e); step(&e, &c, 21, 58000, 1); CHECK(!e.danger);
  175. step(&e, &c, 22, 57000, 1); step(&e, &c, 23, 56000, 1); CHECK(e.danger);
  176. engine_init(&e);
  177. step(&e, &c, 0.0, 150000, 0);
  178. step(&e, &c, 1.8, 100000, 0); CHECK(e.state == NORMAL);
  179. step(&e, &c, 4.2, 95000, 0); CHECK(e.state == PRESSURE && !e.danger);
  180. engine_init(&e);
  181. step(&e, &c, 0.0, 150000, 0);
  182. step(&e, &c, 3.1, 65000, 1); CHECK(e.state == CRITICAL && e.danger);
  183. {
  184. sample_t s = sample(1, 60000, 0);
  185. delta_t none = {0, 0, 0}, rising = {1, 5, 4096};
  186. engine_init(&e); engine_step(&e, &c, &s, none, rising, none);
  187. CHECK(!e.danger && e.critical_since < 0); /* physical swap alone is inert */
  188. engine_init(&e); engine_step(&e, &c, &s, none, none, rising);
  189. CHECK(!e.danger && e.critical_since < 0); /* pageout alone is inert */
  190. s.free = 150000;
  191. engine_init(&e); engine_step(&e, &c, &s, none, rising, rising);
  192. CHECK(e.state == NORMAL && e.low_since < 0); /* corroboration needs low memory */
  193. s.free = 60000;
  194. engine_init(&e);
  195. for (i = 0; i <= 3; i++) {
  196. s.at = i; engine_step(&e, &c, &s, none, rising, rising);
  197. }
  198. CHECK(e.state == CRITICAL && e.danger);
  199. }
  200. puts("PASS all six states, monotonic overruns, threshold jumps, corroboration and recovery");
  201. }
  202. static void successful_avoidance_fixture(void)
  203. {
  204. config_t c = allowed(); engine_t e; FILE *f; char line[256], event[64];
  205. double at, late; uint64_t free_pages, nscan; unsigned rows = 0;
  206. engine_init(&e);
  207. f = fopen("tests/fixtures/successful_pageout_deadman_avoidance_2026_09_21.csv", "r");
  208. CHECK(f != NULL); CHECK(fgets(line, sizeof (line), f) != NULL);
  209. while (fscanf(f, "%lf,%" SCNu64 ",%" SCNu64 ",%lf,%63s",
  210. &at, &free_pages, &nscan, &late, event) == 5) {
  211. step(&e, &c, at, free_pages, nscan); rows++;
  212. CHECK(late >= 0.0);
  213. if (!strcmp(event, "emergency")) CHECK(e.state == EMERGENCY && e.danger);
  214. if (!strcmp(event, "recovery")) CHECK(e.state == RECOVERY && !e.danger);
  215. if (!strcmp(event, "normal")) CHECK(e.state == NORMAL && !e.danger);
  216. }
  217. fclose(f); CHECK(rows == 7); CHECK(e.state == NORMAL);
  218. puts("PASS successful_pageout_deadman_avoidance_2026_09_21 incident fixture");
  219. }
  220. static void real_incident_fixture(void)
  221. {
  222. config_t c = allowed(); engine_t e; FILE *f;
  223. unsigned t, rows = 0; uint64_t free_pages, nscan;
  224. char line[128];
  225. engine_init(&e);
  226. f = fopen("tests/fixtures/pageout_deadman_real_incident_2026_09.csv", "r");
  227. CHECK(f != NULL); CHECK(fgets(line, sizeof (line), f) != NULL);
  228. while (fscanf(f, "%u,%" SCNu64 ",%" SCNu64, &t, &free_pages, &nscan) == 3) {
  229. step(&e, &c, t, free_pages, nscan); rows++;
  230. if (t == 2) CHECK(e.state == PRE_PRESSURE);
  231. if (t == 3) CHECK(e.state == PRESSURE);
  232. }
  233. fclose(f);
  234. CHECK(rows == 5); CHECK(e.state == EMERGENCY && e.danger);
  235. puts("PASS pageout_deadman_real_incident_2026_09 exits NORMAL before minfree");
  236. }
  237. static void actions(void)
  238. {
  239. config_t c = allowed(); engine_t e; controller_t a = {0};
  240. process_t p = process(123); sample_t s; plan_t plan; unsigned i;
  241. engine_init(&e); e.state = CRITICAL; e.danger = 1;
  242. s = sample(100, 60000, 1);
  243. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == TERM);
  244. action_commit(&a, &c, &s, &plan); CHECK(a.has_pending && a.nvictims == 1);
  245. printf("SIMULATED t=100 state=CRITICAL UUID=%s pid=123 WOULD_ACTION=SIGTERM\n", uuid);
  246. s.at = 104; plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == NONE);
  247. s.at = 105; plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
  248. action_commit(&a, &c, &s, &plan); CHECK(!a.has_pending && a.nvictims == 1);
  249. puts("SIMULATED t=105 state=CRITICAL pid=123 WOULD_ACTION=SIGKILL reason=memory_not_recovered");
  250. p.pid++; s.at = 106; CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  251. e.state = EMERGENCY; s.free = 40000;
  252. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == TERM);
  253. action_commit(&a, &c, &s, &plan);
  254. s.at = 106.5; CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  255. s.at = 107; CHECK(action_plan(&a, &c, &e, &s, &p).action == KILL);
  256. s.free = 20000; e.floor = 1;
  257. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
  258. action_commit(&a, &c, &s, &plan);
  259. p.pid++; s.at++;
  260. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
  261. action_commit(&a, &c, &s, &plan);
  262. puts("SIMULATED state=EMERGENCY floor=1 WOULD_ACTION=SIGKILL reason=floor");
  263. p.pid++; s.at++;
  264. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
  265. action_commit(&a, &c, &s, &plan);
  266. p.pid++; s.at++;
  267. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
  268. CHECK(!strcmp(plan.budget_override, "floor"));
  269. e.state = NORMAL; e.danger = 0; s.free = 160000;
  270. e.recovery_since = s.at - c.recovery_s;
  271. (void)action_plan(&a, &c, &e, &s, &p); CHECK(a.nvictims == 0);
  272. e.state = EMERGENCY; e.danger = 1; e.floor = 0; s.free = 40000;
  273. a.nvictims = c.max_victims; a.nrate = c.max_per_minute;
  274. e.state = CRITICAL;
  275. CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  276. e.state = EMERGENCY;
  277. a.nvictims = c.max_victims; a.nrate = c.max_per_minute;
  278. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == TERM);
  279. CHECK(!strcmp(plan.budget_override, "emergency"));
  280. a.nvictims = c.max_emergency_victims;
  281. CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  282. e.floor = 1; s.free = 20000;
  283. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
  284. CHECK(!strcmp(plan.budget_override, "floor"));
  285. a.nvictims = c.max_floor_victims;
  286. CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  287. memset(&a, 0, sizeof (a)); e.floor = 0; e.state = CRITICAL; s.free = 60000;
  288. plan = action_plan(&a, &c, &e, &s, &p); action_commit(&a, &c, &s, &plan);
  289. s.free = 140000; e.danger = 0; e.state = RECOVERY;
  290. CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE && !a.has_pending);
  291. for (i = 0; i < 2; i++) {
  292. c.armed = (int)i; memset(&a, 0, sizeof (a));
  293. e.danger = 1; e.state = EMERGENCY; e.floor = 1; s.free = 1000;
  294. p.zid = 0; CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  295. p.zid = 9; c.zones[0].policy = PROTECT;
  296. CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  297. c.zones[0].policy = ALLOW;
  298. }
  299. puts("PASS TERM/KILL sequencing, emergency grace, floor, cooldown, budgets and no-action protections");
  300. }
  301. static void hard_floor_policy(void)
  302. {
  303. config_t c = allowed();
  304. controller_t a;
  305. engine_t e;
  306. sample_t s = sample(100, 6513, 1000000);
  307. process_t p = process(3000);
  308. delta_t growing = {1, 5, 100}, invalid = {0, 0, 0};
  309. unsigned i;
  310. memset(&a, 0, sizeof (a)); engine_init(&e);
  311. a.nvictims = c.max_floor_victims; s.pageout_ok = 1;
  312. for (i = 0; i < 6; i++) {
  313. s.at = 100 + i;
  314. engine_step(&e, &c, &s, invalid, growing, growing);
  315. CHECK(e.hard_floor_ready == (i == 5));
  316. CHECK((action_plan(&a, &c, &e, &s, &p).action == KILL) == (i == 5));
  317. }
  318. CHECK(!strcmp(action_plan(&a, &c, &e, &s, &p).budget_override, "hard_floor"));
  319. a.nvictims = 16;
  320. CHECK(!strcmp(action_plan(&a, &c, &e, &s, &p).reason, "hard_floor_victim_budget_exhausted"));
  321. a.nvictims = 12; s.at += 2;
  322. engine_step(&e, &c, &s, invalid, growing, growing);
  323. CHECK(!e.hard_floor_ready);
  324. for (i = 0; i < 8; i++) {
  325. s.at++; engine_step(&e, &c, &s, invalid, growing, invalid);
  326. CHECK(!e.hard_floor_ready);
  327. }
  328. s.scan = 0;
  329. for (i = 0; i < 8; i++) {
  330. s.at++; engine_step(&e, &c, &s, invalid, growing, growing);
  331. CHECK(!e.hard_floor_ready);
  332. }
  333. s.free = 70000; s.at++;
  334. engine_step(&e, &c, &s, invalid, invalid, invalid);
  335. CHECK(e.state == RECOVERY);
  336. (void)action_plan(&a, &c, &e, &s, &p); CHECK(a.nvictims == 12);
  337. s.at += 2; s.free = 6513;
  338. engine_step(&e, &c, &s, invalid, invalid, invalid);
  339. CHECK(e.state == EMERGENCY && !e.hard_floor_ready);
  340. (void)action_plan(&a, &c, &e, &s, &p); CHECK(a.nvictims == 12);
  341. engine_gap(&e); s.at++; s.free = s.lots;
  342. (void)action_plan(&a, &c, &e, &s, &p); CHECK(a.nvictims == 12);
  343. s.free = 6513; s.at++; engine_step(&e, &c, &s, invalid, invalid, invalid);
  344. s.free = s.lots;
  345. for (i = 0; i <= c.recovery_s; i++) {
  346. s.at++; engine_step(&e, &c, &s, invalid, invalid, invalid);
  347. (void)action_plan(&a, &c, &e, &s, &p);
  348. CHECK(a.nvictims == (i == c.recovery_s ? 0u : 12u));
  349. }
  350. CHECK(e.state == NORMAL);
  351. puts("PASS hard-floor persistence, finite ceiling, missing corroboration, gap, brief/stable recovery");
  352. }
  353. static void deadman_policy(void)
  354. {
  355. config_t c = allowed();
  356. controller_t a;
  357. engine_t e;
  358. sample_t s;
  359. process_t p = process(9000);
  360. delta_t none = {0, 0, 0}, growing = {1, 5, 100};
  361. unsigned i, mode;
  362. memset(&a, 0, sizeof (a)); engine_init(&e); a.nvictims = 8;
  363. for (i = 0; i < 9; i++) {
  364. s = sample(100 + i, i == 6 ? 100 : 48500 + i, i == 2 ? 0 : 1000000);
  365. s.pageout_ok = s.swap_physical_ok = 1;
  366. s.anon_pageout_pages = 1000 + i; s.swap_physical_used_bytes = 10000 + i;
  367. engine_step(&e, &c, &s, none, growing, growing);
  368. (void)action_plan(&a, &c, &e, &s, &p);
  369. CHECK(e.floor == (i == 6));
  370. CHECK(e.risk.ready == (i >= 5));
  371. CHECK(a.override_stage == (i < 5 ? 0u : i < 8 ? 1u : 2u));
  372. if (i == 5) {
  373. CHECK(action_limit(&a, &c, &e, &s) == 12);
  374. a.nvictims = 12;
  375. }
  376. if (i == 6 || i == 7) CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  377. }
  378. CHECK(action_limit(&a, &c, &e, &s) == 16);
  379. CHECK(action_plan(&a, &c, &e, &s, &p).action == TERM);
  380. s.free = s.min + 1024;
  381. CHECK(deadman_risk_current(&e, &c, &s));
  382. s.free++;
  383. CHECK(!deadman_risk_current(&e, &c, &s));
  384. s.free = 48500;
  385. a.nvictims = 16;
  386. CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  387. a.nvictims = 12;
  388. s.at++; s.free = s.des + 1;
  389. engine_step(&e, &c, &s, none, none, none);
  390. CHECK(!e.risk.ready && e.risk.count == 6);
  391. (void)action_plan(&a, &c, &e, &s, &p);
  392. CHECK(a.nvictims == 12 && a.override_stage == 2);
  393. s.at += 2; s.free = 48500; s.anon_pageout_pages++;
  394. engine_step(&e, &c, &s, none, growing, growing);
  395. (void)action_plan(&a, &c, &e, &s, &p);
  396. CHECK(a.nvictims == 12 && a.override_stage == 2 && e.risk.count == 6);
  397. for (i = 0; i <= c.recovery_s; i++) {
  398. s.at++; s.free = s.lots + 1; s.scan = 0;
  399. engine_step(&e, &c, &s, none, none, none);
  400. (void)action_plan(&a, &c, &e, &s, &p);
  401. }
  402. CHECK(e.state == NORMAL && !e.risk.count && !a.nvictims && !a.override_stage);
  403. /* FLOOR alone: neither a fresh episode nor risk stage1 may jump to16. */
  404. for (mode = 0; mode < 2; mode++) {
  405. memset(&a, 0, sizeof (a)); engine_init(&e);
  406. a.nvictims = 12; a.override_stage = mode; a.override_since = 100;
  407. s = sample(100, 100, 1000000); s.pageout_ok = 1; s.anon_pageout_pages = 100;
  408. engine_step(&e, &c, &s, none, growing, growing);
  409. CHECK(e.floor && !e.hard_floor_ready && !e.risk.ready);
  410. CHECK(action_limit(&a, &c, &e, &s) == 12);
  411. CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  412. a.nvictims = 8;
  413. CHECK(action_plan(&a, &c, &e, &s, &p).action == KILL);
  414. }
  415. /* Negative windows: no paging, no scarcity, brief dip, reset/invalid counter. */
  416. for (mode = 0; mode < 5; mode++) {
  417. memset(&a, 0, sizeof (a)); engine_init(&e); a.nvictims = 8;
  418. for (i = 0; i < 10; i++) {
  419. s = sample(100 + i, mode == 1 || (mode == 2 && i > 1) ? 140000 : 48500,
  420. mode == 0 ? 0 : 1000000);
  421. s.pageout_ok = mode != 4;
  422. s.anon_pageout_pages = mode == 0 ? 100 : mode == 3 ? 1000 - i : 1000 + i;
  423. engine_step(&e, &c, &s, none, none, none);
  424. (void)action_plan(&a, &c, &e, &s, &p);
  425. CHECK(!e.risk.ready && !a.override_stage);
  426. }
  427. }
  428. /* Real monotonic overruns retain evidence; very old windows cannot authorize. */
  429. engine_init(&e);
  430. for (i = 0; i < 6; i++) {
  431. s = sample(100 + 2 * i, 48500, i == 3 ? 0 : 1000000);
  432. s.pageout_ok = 1; s.anon_pageout_pages = 100 + i;
  433. engine_step(&e, &c, &s, none, none, none);
  434. }
  435. CHECK(e.risk.ready);
  436. s.at += 30; s.anon_pageout_pages++;
  437. engine_step(&e, &c, &s, none, none, none);
  438. CHECK(!e.risk.ready && e.risk.count == 6);
  439. c.risk_samples = 32; c.risk_seconds = 31;
  440. c.risk_scan_samples = c.risk_anon_samples = 30;
  441. engine_init(&e);
  442. for (i = 0; i < 40; i++) {
  443. s = sample(100 + i, 48500, 1000000);
  444. s.pageout_ok = 1; s.anon_pageout_pages = 1000 + i;
  445. engine_step(&e, &c, &s, none, none, none);
  446. }
  447. CHECK(e.risk.ready && e.risk.count == 32);
  448. s.anon_pageout_pages = 0;
  449. CHECK(!deadman_risk_current(&e, &c, &s));
  450. s.at = 90; engine_step(&e, &c, &s, none, none, none);
  451. CHECK(!e.risk.ready && e.risk.count == 1);
  452. puts("PASS deadman window, staged8/12/16, recovery, overruns, negatives and single FLOOR never16");
  453. }
  454. int main(void)
  455. {
  456. configs(); protections(); histories(); states(); real_incident_fixture();
  457. successful_avoidance_fixture(); actions(); hard_floor_policy(); deadman_policy();
  458. printf("PASS %u checks; no native backend linked, no signals sent\n", checks);
  459. return 0;
  460. }