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 / capability / Makefile

177 lines · 10577 bytes Raw Blame History
1# Native AIENOS capability authority, in C. No Rust. Needs only the hosted C
2# library and pthreads (it reads /dev/urandom for the office token).
3#
4# make build out/libaienos_capability.a
5# make test build and run the rule tests (SEED=n picks the random-op seed)
6# make mutants plant 18 known bugs one at a time; the existing tests must
7# catch every one (see the table near the end of this file)
8# make clean
9
10CC ?= cc
11AR ?= ar
12CFLAGS ?= -std=gnu11 -O2 -Wall -Wextra -Werror -fstack-protector-strong
13OUT ?= out
14
15LIB = $(OUT)/libaienos_capability.a
16TEST = $(OUT)/capability_test
17
18all: $(LIB)
19
20$(OUT):
21 mkdir -p $(OUT)
22
23$(OUT)/aienos_capability.o: aienos_capability.c aienos_capability.h | $(OUT)
24 $(CC) $(CFLAGS) -c -o $@ aienos_capability.c
25
26$(LIB): $(OUT)/aienos_capability.o
27 $(AR) rcs $@ $^
28
29$(TEST): tests/capability_test.c $(LIB)
30 $(CC) $(CFLAGS) -pthread -o $@ tests/capability_test.c $(LIB)
31
32test: $(TEST) test-contain
33 ./$(TEST) $(SEED)
34
35clean:
36 rm -rf $(OUT)
37
38.PHONY: all test clean
39
40# ---- ARGUS-1 lane G: AEGIS containment gate (aienos_contain.c) ----------------
41# The gate is its own object, never part of the authority archive. Its test
42# includes ../argus/argus_abi.h for the layout-twin asserts only (no libargus).
43#
44# make test-contain build and run tests/contain_test.c
45# make contain-checks symbol and word checks for the gate (I2, I3.a, I4, G7)
46# make sanitize (Linux) contain_test under ASan+UBSan
47
48GATE_OBJ = $(OUT)/aienos_contain.o
49CONTAIN_TEST = $(OUT)/contain_test
50SANFLAGS_ASAN = -fsanitize=address,undefined -fno-omit-frame-pointer -fno-sanitize-recover=all -g
51GATE_FILES = aienos_contain.c aienos_contain.h tests/contain_test.c
52# Authority symbols the gate may reference (I3.a); anything else from aienos_cap_* fails.
53GATE_ALLOWED = aienos_cap_mint aienos_cap_revoke aienos_cap_inspect aienos_cap_office aienos_cap_clock aienos_cap_authorize
54
55$(GATE_OBJ): aienos_contain.c aienos_contain.h aienos_capability.h | $(OUT)
56 $(CC) $(CFLAGS) -c -o $@ aienos_contain.c
57
58$(CONTAIN_TEST): tests/contain_test.c $(GATE_OBJ) $(LIB) ../argus/argus_abi.h
59 $(CC) $(CFLAGS) -pthread -o $@ tests/contain_test.c $(GATE_OBJ) $(LIB)
60
61test-contain: $(CONTAIN_TEST) contain-checks
62 ./$(CONTAIN_TEST)
63
64contain-checks: $(GATE_OBJ) $(OUT)/aienos_capability.o
65 @bad=$$(nm -u $(GATE_OBJ) | awk '{print $$NF}' | sed 's/^_//' | grep '^aienos_cap_' | grep -vxF $(foreach s,$(GATE_ALLOWED),-e $(s))); \
66 if [ -n "$$bad" ]; then echo "contain-checks FAIL: gate references $$bad"; exit 1; fi
67 @if nm -u $(GATE_OBJ) | awk '{print $$NF}' | sed 's/^_//' | grep -qxE 'malloc|calloc|realloc|free|clock_gettime|time|gettimeofday'; then \
68 echo "contain-checks FAIL: gate allocates or reads a clock (G7)"; exit 1; fi
69 @if nm -g $(GATE_OBJ) | awk '$$2 ~ /[TD]/ {print $$3}' | grep -qiE 'freeze|suspend|isolate|pause'; then \
70 echo "contain-checks FAIL: a gate symbol names a suspension primitive (I2)"; exit 1; fi
71 @if nm $(OUT)/aienos_capability.o | grep -q 'aienos_contain'; then \
72 echo "contain-checks FAIL: the authority references the gate (I4)"; exit 1; fi
73 @if [ "$$(grep -c 'aienos_cap_mint(' aienos_contain.c)" != 1 ]; then \
74 echo "contain-checks FAIL: aienos_cap_mint must be called exactly once, in create (I5.a)"; exit 1; fi
75 @if grep -i 'freeze' aienos_contain.c aienos_contain.h | grep -vq 'stand-in'; then \
76 echo "contain-checks FAIL: a freeze mention without stand-in (I2)"; exit 1; fi
77 @if grep -il 'tok[e]n' $(GATE_FILES); then echo "contain-checks FAIL: forbidden word"; exit 1; fi
78 @echo "contain-checks PASS (I2 words, I3.a symbols, I4 direction, I5.a single mint, G7 no malloc/clock)"
79
80# `make test` runs the gate test alongside the authority rule tests.
81sanitize: | $(OUT)
82ifeq ($(shell uname -s),Linux)
83 $(CC) $(CFLAGS) $(SANFLAGS_ASAN) -pthread -o $(OUT)/contain_test_asan tests/contain_test.c aienos_contain.c aienos_capability.c
84 ASAN_OPTIONS=detect_leaks=1 UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1 ./$(OUT)/contain_test_asan
85else
86 @echo "sanitize: Linux only"
87endif
88
89.PHONY: test-contain contain-checks sanitize
90
91# ---- Mutation check: `make mutants` -------------------------------------------
92# Each line below plants one realistic bug (a dropped or weakened check) in a
93# copy of the source, rebuilds the existing tests against that copy, and runs
94# them. The existing tests must catch every bug ("killed"). A bug the tests
95# miss ("SURVIVED"), a mutation whose target text is no longer in the source
96# ("did not apply"), or a copy that will not compile all count as FAIL, so a
97# source edit that moves the target text cannot silently turn this green.
98# The sources themselves are never edited; mutated copies live in $(OUT)/mut/.
99#
100# Format: name|file|which match (1 = first line containing the text)|text|replacement
101define CAP_MUTANTS
102inspect_stale_generation|aienos_capability.c|1|e->generation != cap.generation) return AIENOS_CAP_ERR_STALE_GEN|0) return AIENOS_CAP_ERR_STALE_GEN
103validate_stale_generation|aienos_capability.c|2|e->generation != cap.generation) return AIENOS_CAP_ERR_STALE_GEN|0) return AIENOS_CAP_ERR_STALE_GEN
104validate_revoked|aienos_capability.c|1|e->state != AIENOS_CAP_STATE_LIVE) return AIENOS_CAP_ERR_REVOKED|0) return AIENOS_CAP_ERR_REVOKED
105validate_epoch|aienos_capability.c|1|e->epoch != s->epoch) return AIENOS_CAP_ERR_EPOCH|0) return AIENOS_CAP_ERR_EPOCH
106validate_parent_live|aienos_capability.c|1|parent->state != AIENOS_CAP_STATE_LIVE) return AIENOS_CAP_ERR_CHAIN|0) return AIENOS_CAP_ERR_CHAIN
107validate_subject|aienos_capability.c|1|e->subject != subject|0
108validate_partial_rights|aienos_capability.c|1|(e->rights & rights) != rights|(e->rights & rights) == 0
109lease_expiry_off_by_one|aienos_capability.c|1|s->clock >= e->lease_expiry|s->clock > e->lease_expiry
110mint_amplify|aienos_capability.c|1|r->rights & ~auth->rights) return AIENOS_CAP_ERR_AMPLIFY|0) return AIENOS_CAP_ERR_AMPLIFY
111mint_child_outlives_lease|aienos_capability.c|1|child > auth->lease_expiry|0
112mint_depth_limit|aienos_capability.c|1|ancestor_hops(s, r->parent.cap_id) >= AIENOS_CAP_MAX_DEPTH|0
113revoke_no_cascade|aienos_capability.c|1|parent->state == AIENOS_CAP_STATE_REVOKED|0
114generation_not_advanced|aienos_capability.c|1|*out = generation + 1|*out = generation
115authorize_epoch|aienos_capability.c|2|e->epoch != s->epoch) return AIENOS_CAP_ERR_EPOCH|0) return AIENOS_CAP_ERR_EPOCH
116privileged_right_ignores_resource|aienos_capability.c|1|e->resource != AIENOS_CAP_RES_AUTHORITY)|0)
117contain_subject_mismatch|aienos_contain.c|1|e.subject != r->principal|0
118contain_not_leaf|aienos_contain.c|1|has_live_child(g, r->target.cap_id, r->target.generation)|0
119contain_protected_subject|aienos_contain.c|1|if (is_protected_subject(r->principal)) return true|if (0) return true
120endef
121
122MUT_DIR = $(OUT)/mut
123# The mutant table lives outside MUT_DIR. GNU make expands every recipe line,
124# including $(file ...), before the first shell line runs, so the table must
125# target a directory that already exists ($(OUT), an order-only prerequisite)
126# and must not sit inside the directory the recipe's first line wipes.
127MUT_TABLE = $(OUT)/mutants.table
128MUT_TIMEOUT ?= 300
129
130CAP_MUT_TEST_OBJS = $(OUT)/t_capability_test.o $(OUT)/t_contain_test.o $(OUT)/aienos_capability.o $(OUT)/aienos_contain.o
131$(OUT)/t_%_test.o: tests/%_test.c aienos_capability.h aienos_contain.h | $(OUT)
132 $(CC) $(CFLAGS) -Wno-error -I. -pthread -c -o $@ $<
133
134mutants: $(TEST) $(CONTAIN_TEST) $(CAP_MUT_TEST_OBJS) | $(OUT)
135 @rm -rf $(MUT_DIR); mkdir -p $(MUT_DIR)
136 $(file >$(MUT_TABLE),$(CAP_MUTANTS))
137 @ulimit -c 0; \
138 if ./$(TEST) > $(MUT_DIR)/baseline.log 2>&1 && ./$(CONTAIN_TEST) >> $(MUT_DIR)/baseline.log 2>&1; then \
139 echo "baseline (no mutation): tests pass"; \
140 else echo "baseline (no mutation): tests FAIL, mutants are meaningless"; \
141 echo "---- baseline log ($(MUT_DIR)/baseline.log) ----"; cat $(MUT_DIR)/baseline.log; echo "---- end baseline log ----"; \
142 echo "AIENOS_CAP_MUTANTS: FAIL"; exit 1; fi; \
143 n=0; killed=0; fail=0; names=""; \
144 while IFS='|' read -r name file k from to; do \
145 [ -n "$$name" ] || continue; n=$$((n+1)); \
146 case " $$names " in *" $$name "*) echo "mutant $$name: name appears twice, parallel runs would collide (FAIL)"; fail=1; continue ;; esac; \
147 names="$$names $$name"; d=$(MUT_DIR)/$$name; mkdir -p $$d; \
148 ( if ! awk -v k="$$k" -v from="$$from" -v to="$$to" \
149 'index($$0, from) { c++; if (c == k) { i = index($$0, from); $$0 = substr($$0, 1, i - 1) to substr($$0, i + length(from)); hit = 1 } } { print } END { exit hit ? 0 : 3 }' \
150 $$file > $$d/$$file; then \
151 echo "mutant $$name: did not apply, text not found in $$file (FAIL)" > $$d/msg; echo 1 > $$d/fail; exit 0; fi; \
152 if cmp -s $$file $$d/$$file; then echo "mutant $$name: no change (FAIL)" > $$d/msg; echo 1 > $$d/fail; exit 0; fi; \
153 if ! $(CC) $(CFLAGS) -Wno-error -I. -pthread -c -o $$d/m.o $$d/$$file > $$d/build.log 2>&1; then \
154 echo "mutant $$name: does not compile (FAIL, see $$d/build.log)" > $$d/msg; echo 1 > $$d/fail; exit 0; fi; \
155 if [ $$file = aienos_capability.c ]; then capobj=$$d/m.o; gateobj=$(OUT)/aienos_contain.o; \
156 else capobj=$(OUT)/aienos_capability.o; gateobj=$$d/m.o; fi; \
157 if ! $(CC) $(CFLAGS) -Wno-error -pthread -o $$d/capability_test $(OUT)/t_capability_test.o $$capobj >> $$d/build.log 2>&1 || \
158 ! $(CC) $(CFLAGS) -Wno-error -pthread -o $$d/contain_test $(OUT)/t_contain_test.o $$gateobj $$capobj >> $$d/build.log 2>&1; then \
159 echo "mutant $$name: does not compile (FAIL, see $$d/build.log)" > $$d/msg; echo 1 > $$d/fail; exit 0; fi; \
160 by=""; \
161 timeout $(MUT_TIMEOUT) $$d/capability_test > $$d/run.log 2>&1 || by="capability_test"; \
162 timeout $(MUT_TIMEOUT) $$d/contain_test >> $$d/run.log 2>&1 || by="$${by:+$$by+}contain_test"; \
163 if [ -n "$$by" ]; then echo "mutant $$name: killed by $$by" > $$d/msg; : > $$d/killed; \
164 else echo "mutant $$name: SURVIVED (FAIL)" > $$d/msg; echo 1 > $$d/fail; fi \
165 ) & \
166 done < $(MUT_TABLE); \
167 wait; \
168 for name in $$names; do \
169 d=$(MUT_DIR)/$$name; \
170 if [ -f $$d/msg ]; then cat $$d/msg; else echo "mutant $$name: no result recorded (FAIL)"; fi; \
171 if [ -f $$d/killed ] && [ ! -f $$d/fail ]; then killed=$$((killed+1)); else fail=1; fi; \
172 done; \
173 if [ $$n -lt 8 ]; then echo "only $$n mutants defined, need at least 8"; fail=1; fi; \
174 if [ $$fail -eq 0 ] && [ $$killed -eq $$n ]; then echo "AIENOS_CAP_MUTANTS: PASS ($$killed/$$n killed)"; \
175 else echo "AIENOS_CAP_MUTANTS: FAIL ($$killed/$$n killed)"; exit 1; fi
176
177.PHONY: mutants