interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / core / allen_gate.c

184 lines · 5801 bytes Raw Blame History
1/* allen_gate.c -- see allen_gate.h. No kernel calls, no libc. */
2#include "allen_gate.h"
3
4#define PFX "{\"action\":\""
5#define MID "\",\"args\":{"
6#define TXT "\"text\":\""
7
8static size_t slen(const char *s)
9{
10 size_t n = 0;
11 while (s[n]) n++;
12 return n;
13}
14
15/* json[*at..) starts with lit: advance past it */
16static int eat(const char *j, size_t n, size_t *at, const char *lit)
17{
18 size_t k = slen(lit);
19 if (n - *at < k) return 0;
20 for (size_t i = 0; i < k; i++)
21 if (j[*at + i] != lit[i]) return 0;
22 *at += k;
23 return 1;
24}
25
26static int is_word(const char *s, size_t n, const char *w)
27{
28 size_t k = slen(w);
29 if (k != n) return 0;
30 for (size_t i = 0; i < n; i++)
31 if (s[i] != w[i]) return 0;
32 return 1;
33}
34
35static void copy_line(struct ck_allen_gate *o, const char *v, size_t vl, const char *t, size_t tl)
36{
37 size_t w = 0;
38 for (size_t i = 0; i < vl; i++) o->line[w++] = v[i];
39 if (tl) {
40 o->line[w++] = ' ';
41 for (size_t i = 0; i < tl; i++) o->line[w++] = t[i];
42 }
43 o->line[w] = 0;
44 o->line_len = (uint32_t)w;
45}
46
47static void zero(void *p, size_t n)
48{
49 unsigned char *b = p;
50 for (size_t i = 0; i < n; i++) b[i] = 0;
51}
52
53static void copy_lc(ck_lc *dst, const ck_lc *src)
54{
55 const unsigned char *s = (const unsigned char *)src;
56 unsigned char *d = (unsigned char *)dst;
57 for (size_t i = 0; i < sizeof *src; i++) d[i] = s[i];
58}
59
60static void verdict(struct ck_allen_gate *o, enum ck_allen_verdict v, const char *why)
61{
62 o->verdict = v;
63 o->reason = why;
64}
65
66void ck_allen_gate_judge(const char *j, size_t n, const ck_lc *lc, struct ck_allen_gate *o)
67{
68 if (!o) return;
69 zero(o, sizeof *o);
70 o->verb = CK_LC_V_UNKNOWN;
71 o->fsm = CK_LC_R_REFUSED;
72 if (!j || !lc || n == 0 || n > CK_ALLEN_JSON_MAX) {
73 verdict(o, CK_ALLEN_REJECTED, "decision size out of bounds");
74 return;
75 }
76 /* 1. exact shape */
77 size_t at = 0, vb, vl, tb = 0, tl = 0;
78 if (!eat(j, n, &at, PFX)) goto shape;
79 vb = at;
80 while (at < n && j[at] >= 'a' && j[at] <= 'z') at++;
81 vl = at - vb;
82 if (vl == 0 || vl > 16 || !eat(j, n, &at, MID)) goto shape;
83 if (eat(j, n, &at, TXT)) {
84 tb = at;
85 while (at < n && j[at] >= 0x20 && j[at] <= 0x7e && j[at] != '"' && j[at] != '\\') at++;
86 tl = at - tb;
87 if (tl == 0 || !eat(j, n, &at, "\"")) goto shape;
88 }
89 if (!eat(j, n, &at, "}}") || at != n) goto shape;
90
91 /* 2. no command */
92 if (is_word(j + vb, vl, "none") || is_word(j + vb, vl, "clarify") || is_word(j + vb, vl, "refuse")) {
93 if (tl) {
94 verdict(o, CK_ALLEN_REJECTED, "no-command decision carries text");
95 return;
96 }
97 verdict(o, CK_ALLEN_NO_COMMAND, "no command");
98 return;
99 }
100
101 /* 3. the card command parser: verb whitelist and text bounds */
102 if (vl + 1 + tl + 1 > CK_AGENT_CMD_LINE_MAX) {
103 verdict(o, CK_ALLEN_REJECTED, "command line too long");
104 return;
105 }
106 copy_line(o, j + vb, vl, j + tb, tl);
107 {
108 static const char hdr[] = CK_AGENT_CMD_HEADER "\n";
109 uint8_t file[sizeof hdr + CK_AGENT_CMD_LINE_MAX + 1];
110 struct ck_agent_cmd cmd;
111 const char *why = 0;
112 size_t w = 0;
113 for (size_t i = 0; i + 1 < sizeof hdr; i++) file[w++] = (uint8_t)hdr[i];
114 for (size_t i = 0; i < o->line_len; i++) file[w++] = (uint8_t)o->line[i];
115 file[w++] = '\n';
116 if (ck_agent_cmd_parse(file, w, &cmd, &why) != 0 || cmd.nlines != 1) {
117 verdict(o, CK_ALLEN_REJECTED, why ? why : "not a card command");
118 return;
119 }
120 }
121 const char *arg = 0;
122 size_t arg_len = 0;
123 o->verb = ck_lc_decode(o->line, o->line_len, &arg, &arg_len);
124 if (o->verb <= CK_LC_V_EMPTY || !is_word(o->line, vl, ck_lc_verb_name(o->verb))) {
125 verdict(o, CK_ALLEN_REJECTED, "command does not decode to its own verb");
126 return;
127 }
128
129 /* 4. the lifecycle judges it on a copy */
130 ck_lc copy;
131 copy_lc(&copy, lc);
132 o->fsm = ck_lc_apply(&copy, o->verb, arg, arg_len);
133 if (o->fsm == CK_LC_R_REFUSED || o->fsm == CK_LC_R_INVALID) {
134 verdict(o, CK_ALLEN_REFUSED, copy.reason && copy.reason[0] ? copy.reason : "refused by the lifecycle");
135 return;
136 }
137
138 /* 5. authority stays with the operator */
139 switch (o->verb) {
140 case CK_LC_V_APPROVE:
141 case CK_LC_V_DENY:
142 case CK_LC_V_CANCEL:
143#ifdef CK_ALLEN_GATE_MUTANT_TRUST_AUTHORITY
144 /* TEST-ONLY mutant: ALLEN's approve/deny/cancel would be proposed like any other verb. */
145 verdict(o, CK_ALLEN_PROPOSED, "mutant");
146 return;
147#else
148 verdict(o, CK_ALLEN_NEEDS_OPERATOR, "approval authority stays with the operator");
149 return;
150#endif
151 case CK_LC_V_REMEMBER:
152 case CK_LC_V_PREFER:
153 verdict(o, CK_ALLEN_NEEDS_OPERATOR, "writes the sealed Store: operator only");
154 return;
155 case CK_LC_V_STATUS:
156 case CK_LC_V_HELP:
157 case CK_LC_V_EVIDENCE:
158 case CK_LC_V_RECALL:
159 case CK_LC_V_FACTS:
160 verdict(o, CK_ALLEN_PROPOSED, "read only");
161 return;
162 case CK_LC_V_TASK:
163 case CK_LC_V_PROMPT:
164 verdict(o, CK_ALLEN_PROPOSED, "creates a pending item that waits for the operator's approve");
165 return;
166 default:
167 verdict(o, CK_ALLEN_REJECTED, "verb not allowed to ALLEN");
168 return;
169 }
170shape:
171 verdict(o, CK_ALLEN_REJECTED, "decision is not the exact JSON shape");
172}
173
174const char *ck_allen_verdict_name(enum ck_allen_verdict v)
175{
176 switch (v) {
177 case CK_ALLEN_NO_COMMAND: return "NO_COMMAND";
178 case CK_ALLEN_PROPOSED: return "PROPOSED";
179 case CK_ALLEN_NEEDS_OPERATOR: return "NEEDS_OPERATOR";
180 case CK_ALLEN_REFUSED: return "REFUSED";
181 case CK_ALLEN_REJECTED: return "REJECTED";
182 }
183 return "?";
184}