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

234 lines · 12908 bytes Raw Blame History
1# Native Store (C, no outside library):
2# torn_slot torn-write-safe 4096-byte record over 512/4096-byte blocks
3# store_v1/engine/disk Store v1 (ADR 0015) C twin of the Rust engine
4# store_sealed M5 sealed store: envelopes, transaction records, anchor
5# SHA-256 is ../argus/sha256.c; crypto is ../crypto and ../m5; block I/O is
6# ../disk. Library objects import no heap, clock or I/O; tests use hosted libc.
7#
8# make build out/libtorn_slot.a and out/libaienos_store.a
9# make test torn_slot test + every Store test + Rust cross-check
10# (host emulation over files, not QEMU, not hardware)
11# make sanitize the same tests under ASan+UBSan (Linux)
12# make mutants remove each GUARD-tagged rule in turn; tests must fail
13# make freestanding -ffreestanding -nostdlib build; nm -u must close over
14# the sibling native objects
15# make clean
16# RUST_STORE_TOOL=<path> selects the Rust aienos-store-tool for the
17# cross-check (default ../../target/release/aienos-store-tool; NOT_RUN if absent).
18
19CC ?= cc
20AR ?= ar
21LD ?= ld
22NM ?= nm
23CFLAGS ?= -std=gnu11 -O2 -Wall -Wextra -Werror -fstack-protector-strong
24OUT ?= out
25
26LIB = $(OUT)/libtorn_slot.a
27TEST = $(OUT)/torn_slot_test
28SANFLAGS = -fsanitize=address,undefined -fno-omit-frame-pointer -fno-sanitize-recover=all -g
29
30STORE_SRCS = store_v1.c store_engine.c store_disk.c store_sealed.c
31STORE_HDRS = store_v1.h store_engine.h store_sealed.h torn_slot.h ../disk/disk.h ../m5/m5.h ../argus/sha256.h
32DEP_SRCS = ../argus/sha256.c ../disk/disk.c ../m5/m5_keys.c ../m5/m5_envelope.c ../m5/m5_state.c \
33 ../crypto/aes.c ../crypto/polyval.c ../crypto/gcm_siv.c ../crypto/hmac.c ../crypto/ct.c
34STORE_OBJS = $(STORE_SRCS:%.c=$(OUT)/%.o)
35DEP_OBJS = $(OUT)/sha256.o $(OUT)/dep_disk.o $(OUT)/dep_m5_keys.o $(OUT)/dep_m5_envelope.o \
36 $(OUT)/dep_m5_state.o $(OUT)/dep_aes.o $(OUT)/dep_polyval.o $(OUT)/dep_gcm_siv.o \
37 $(OUT)/dep_hmac.o $(OUT)/dep_ct.o
38STORE_LIB = $(OUT)/libaienos_store.a
39INC = -I. -I../argus -I../disk -Itests -I$(OUT)
40GOLDEN_JSON = ../../docs/adr/0015-golden-vectors.json
41GOLDEN_H = $(OUT)/golden_vectors.h
42TEST_NAMES = store_v1_test store_engine_test store_sealed_test store_powercut_test
43TEST_OBJS = $(TEST_NAMES:%=$(OUT)/t_%.o)
44STORE_TESTS = $(TEST_NAMES:%=$(OUT)/%)
45XCHECK = $(OUT)/store_xcheck
46HOST_OBJS = $(OUT)/disk_file.o
47
48all: $(LIB) $(STORE_LIB)
49
50$(OUT):
51 mkdir -p $(OUT)
52
53$(OUT)/torn_slot.o: torn_slot.c torn_slot.h ../argus/sha256.h | $(OUT)
54 $(CC) $(CFLAGS) -c -o $@ torn_slot.c
55
56$(OUT)/sha256.o: ../argus/sha256.c ../argus/sha256.h | $(OUT)
57 $(CC) $(CFLAGS) -c -o $@ ../argus/sha256.c
58
59$(LIB): $(OUT)/torn_slot.o $(OUT)/sha256.o
60 $(AR) rcs $@ $^
61
62$(TEST): tests/torn_slot_test.c $(LIB)
63 $(CC) $(CFLAGS) -o $@ tests/torn_slot_test.c $(LIB)
64
65# ---- Store objects ----
66$(OUT)/store_%.o: store_%.c $(STORE_HDRS) | $(OUT)
67 $(CC) $(CFLAGS) $(INC) -c -o $@ $<
68$(OUT)/dep_disk.o: ../disk/disk.c ../disk/disk.h | $(OUT)
69 $(CC) $(CFLAGS) -c -o $@ $<
70$(OUT)/dep_m5_%.o: ../m5/m5_%.c ../m5/m5.h ../m5/m5_internal.h | $(OUT)
71 $(CC) $(CFLAGS) -c -o $@ $<
72$(OUT)/dep_%.o: ../crypto/%.c ../crypto/aienos_crypto.h | $(OUT)
73 $(CC) $(CFLAGS) -c -o $@ $<
74$(OUT)/disk_file.o: ../disk/disk_file.c ../disk/disk_file.h ../disk/disk.h | $(OUT)
75 $(CC) $(CFLAGS) -c -o $@ $<
76
77$(STORE_LIB): $(STORE_OBJS) $(OUT)/torn_slot.o $(DEP_OBJS)
78 $(AR) rcs $@ $^
79
80$(GOLDEN_H): tests/gen_golden.sh $(GOLDEN_JSON) | $(OUT)
81 sh tests/gen_golden.sh $(GOLDEN_JSON) $@
82
83$(OUT)/t_%.o: tests/%.c tests/store_test_util.h tests/store_rig.h $(STORE_HDRS) $(GOLDEN_H) | $(OUT)
84 $(CC) $(CFLAGS) $(INC) -c -o $@ $<
85
86$(OUT)/store_%_test: $(OUT)/t_store_%_test.o $(STORE_LIB) $(HOST_OBJS)
87 $(CC) $(CFLAGS) -o $@ $< $(HOST_OBJS) $(STORE_LIB)
88
89$(XCHECK): tests/store_xcheck.c tests/store_test_util.h $(STORE_LIB) $(HOST_OBJS)
90 $(CC) $(CFLAGS) $(INC) -o $@ tests/store_xcheck.c $(HOST_OBJS) $(STORE_LIB)
91
92test: $(TEST) $(STORE_TESTS) $(XCHECK) lib-checks
93 @fail=0; \
94 ./$(TEST) || fail=1; \
95 ./$(OUT)/store_v1_test || fail=1; \
96 ./$(OUT)/store_engine_test $(OUT) || fail=1; \
97 ./$(OUT)/store_sealed_test $(OUT) || fail=1; \
98 ./$(OUT)/store_powercut_test $(OUT) || fail=1; \
99 RUST_STORE_TOOL=$(RUST_STORE_TOOL) sh tests/rust_crosscheck.sh ./$(XCHECK) $(OUT); xrc=$$?; if [ $$xrc -ne 0 ] && [ $$xrc -ne 2 ]; then fail=1; fi; \
100 if [ $$fail -eq 0 ]; then echo "AIENOS_STORE_NATIVE: PASS"; else echo "AIENOS_STORE_NATIVE: FAIL"; exit 1; fi
101
102# Library objects must not allocate, read a clock, or do their own I/O.
103LIB_CHECK_OBJS = $(OUT)/torn_slot.o $(STORE_OBJS)
104lib-checks: $(LIB_CHECK_OBJS)
105 @for o in $(LIB_CHECK_OBJS); do \
106 if $(NM) -u $$o | awk '{print $$NF}' | sed 's/^_//' | grep -qxE 'malloc|calloc|realloc|free|clock_gettime|time|gettimeofday|open|read|write|pread|pwrite|fopen|printf|fprintf|puts|abort|exit'; then \
107 echo "lib-checks FAIL: $$o allocates, reads a clock or does its own I/O"; exit 1; fi; \
108 done
109 @echo "lib-checks PASS (no heap, clock or direct I/O in torn_slot.o, store_v1.o, store_engine.o, store_disk.o, store_sealed.o)"
110
111SAN_SRCS = $(STORE_SRCS) torn_slot.c $(DEP_SRCS) ../disk/disk_file.c
112sanitize: $(GOLDEN_H) | $(OUT)
113ifeq ($(shell uname -s),Linux)
114 $(CC) $(CFLAGS) $(SANFLAGS) -o $(OUT)/torn_slot_test_asan tests/torn_slot_test.c torn_slot.c ../argus/sha256.c
115 ASAN_OPTIONS=detect_leaks=1 UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1 ./$(OUT)/torn_slot_test_asan
116 @for t in $(TEST_NAMES); do \
117 $(CC) $(CFLAGS) $(SANFLAGS) $(INC) -o $(OUT)/$${t}_asan tests/$$t.c $(SAN_SRCS) || exit 1; \
118 done
119 @fail=0; for t in $(TEST_NAMES); do \
120 ASAN_OPTIONS=detect_leaks=1 UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1 ./$(OUT)/$${t}_asan $(OUT) > $(OUT)/$${t}_asan.log 2>&1 || fail=1; \
121 tail -n 1 $(OUT)/$${t}_asan.log; \
122 done; \
123 if [ $$fail -eq 0 ]; then echo "AIENOS_STORE_SANITIZE: PASS"; else echo "AIENOS_STORE_SANITIZE: FAIL"; exit 1; fi
124else
125 @echo "sanitize: Linux only"
126endif
127
128# Mutants: remove each GUARD-tagged rule in turn. The tests must fail for
129# every mutant, or that rule is not tested. A mutant counts as killed only
130# when a test exits 1 with its own FAIL verdict line (or, for store mutants,
131# a sanitizer report of the invalid access); a crash, a signal, any other exit
132# status, a build failure or a gate timeout is never counted as a detection.
133GUARDS = parity-a parity-b b-needs-a adjacent first-remnant a-alone-seq1
134
135mutants: torn-slot-mutants store-mutants
136
137$(OUT)/t_torn_slot_test.o: tests/torn_slot_test.c torn_slot.h ../argus/sha256.h | $(OUT)
138 $(CC) $(CFLAGS) -Wno-error -I. -c -o $@ $<
139
140torn-slot-mutants: $(OUT)/t_torn_slot_test.o $(OUT)/sha256.o | $(OUT)
141 @fail=0; \
142 dups=$$(printf '%s\n' $(GUARDS) | sort | uniq -d); \
143 [ -z "$$dups" ] || { echo "duplicate guard names (parallel runs would collide):" $$dups; echo "TORN_SLOT_MUTANTS: FAIL"; exit 1; }; \
144 for g in $(GUARDS); do \
145 rm -f $(OUT)/mut_$$g.msg $(OUT)/mut_$$g.fail $(OUT)/mut_$$g.killed; \
146 ( grep -q "GUARD:$$g \*/" torn_slot.c || { echo "mutant $$g: guard tag missing" > $(OUT)/mut_$$g.msg; echo 1 > $(OUT)/mut_$$g.fail; exit 0; }; \
147 sed "/GUARD:$$g \*\//d" torn_slot.c > $(OUT)/mut_$$g.c; \
148 $(CC) $(CFLAGS) -Wno-error -I. -c -o $(OUT)/mut_$$g.o $(OUT)/mut_$$g.c \
149 && $(CC) $(CFLAGS) -Wno-error -o $(OUT)/mut_$$g $(OUT)/t_torn_slot_test.o $(OUT)/mut_$$g.o $(OUT)/sha256.o \
150 || { echo "mutant $$g: does not compile" > $(OUT)/mut_$$g.msg; echo 1 > $(OUT)/mut_$$g.fail; exit 0; }; \
151 ./$(OUT)/mut_$$g --rules-only > $(OUT)/mut_$$g.log 2>&1; rc=$$?; \
152 if [ $$rc -eq 0 ]; then \
153 echo "mutant $$g: SURVIVED (FAIL)" > $(OUT)/mut_$$g.msg; echo 1 > $(OUT)/mut_$$g.fail; \
154 elif [ $$rc -eq 1 ] && grep -q '^TORN_SLOT_RULES: FAIL' $(OUT)/mut_$$g.log; then \
155 echo "mutant $$g: killed" > $(OUT)/mut_$$g.msg; : > $(OUT)/mut_$$g.killed; \
156 else echo "mutant $$g: exit $$rc without a FAIL verdict, not a detection (FAIL)" > $(OUT)/mut_$$g.msg; echo 1 > $(OUT)/mut_$$g.fail; fi \
157 ) & \
158 done; \
159 wait; \
160 for g in $(GUARDS); do \
161 if [ -f $(OUT)/mut_$$g.msg ]; then cat $(OUT)/mut_$$g.msg; else echo "mutant $$g: no result recorded (FAIL)"; fi; \
162 [ -f $(OUT)/mut_$$g.killed ] && [ ! -f $(OUT)/mut_$$g.fail ] || fail=1; \
163 done; \
164 if [ $$fail -eq 0 ]; then echo "TORN_SLOT_MUTANTS: PASS"; else echo "TORN_SLOT_MUTANTS: FAIL"; exit 1; fi
165
166STORE_GUARDS = $(shell grep -ho 'GUARD:[a-z0-9-]* \*/' $(STORE_SRCS) | sed 's/GUARD://; s/ \*\///')
167# A removed refusal may read outside a bounded workspace and still return
168# the expected error by accident. Detect that invalid access at its source.
169MUTANT_SANFLAGS = -fsanitize=undefined -fno-sanitize-recover=all
170# Each store mutant works in its own $(OUT)/smut_<guard> directory, a different name
171# space from the torn-slot mutants above, so `make -j mutants` can run both at once.
172store-mutants: $(STORE_OBJS) $(TEST_OBJS) $(DEP_OBJS) $(OUT)/torn_slot.o $(HOST_OBJS)
173 @fail=0; total=0; killed=0; \
174 dups=$$(printf '%s\n' $(STORE_GUARDS) | sort | uniq -d); \
175 [ -z "$$dups" ] || { echo "duplicate guard tags (parallel runs would collide):" $$dups; echo "AIENOS_STORE_MUTANTS: FAIL"; exit 1; }; \
176 for g in $(STORE_GUARDS); do \
177 total=$$((total + 1)); d=$(OUT)/smut_$$g; rm -rf $$d; mkdir -p $$d; \
178 ( f=$$(grep -l "GUARD:$$g \*/" $(STORE_SRCS)); \
179 [ $$(echo $$f | wc -w) -eq 1 ] || { echo "mutant $$g: tag not unique ($$f)" > $$d/msg; echo 1 > $$d/fail; exit 0; }; \
180 sed "/GUARD:$$g \*\//d" $$f > $$d/$$f; \
181 $(CC) $(CFLAGS) $(MUTANT_SANFLAGS) -Wno-error $(INC) -c -o $$d/m.o $$d/$$f 2> $$d/cc.log || { echo "mutant $$g: does not compile (see $$d/cc.log)" > $$d/msg; echo 1 > $$d/fail; exit 0; }; \
182 objs=""; for s in $(STORE_SRCS); do [ $$s = $$f ] || objs="$$objs $(OUT)/$${s%.c}.o"; done; \
183 case "$$f" in \
184 store_v1.c) order="store_v1_test store_engine_test store_sealed_test store_powercut_test" ;; \
185 store_sealed.c) order="store_sealed_test store_powercut_test store_engine_test store_v1_test" ;; \
186 *) order="store_engine_test store_v1_test store_sealed_test store_powercut_test" ;; \
187 esac; \
188 dead=0; by=""; \
189 for t in $$order; do \
190 $(CC) $(CFLAGS) $(MUTANT_SANFLAGS) -o $$d/$$t $(OUT)/t_$$t.o $$d/m.o $$objs $(OUT)/torn_slot.o $(DEP_OBJS) $(HOST_OBJS) || { dead=2; break; }; \
191 ./$$d/$$t $$d > $$d/$$g.log 2>&1; rc=$$?; \
192 if [ $$rc -ne 0 ]; then \
193 if [ $$rc -eq 1 ] && grep -q '^STORE_[A-Z0-9_]*: FAIL$$' $$d/$$g.log; then dead=1; by="$$t"; \
194 elif [ $$rc -eq 1 ] && grep -q 'runtime error:' $$d/$$g.log; then dead=1; by="$$t (sanitizer)"; \
195 else dead=3; by="$$t exit $$rc"; fi; \
196 break; \
197 fi; \
198 done; \
199 if [ $$dead -eq 1 ]; then echo "mutant $$g ($$f): killed by $$by" > $$d/msg; : > $$d/killed; \
200 elif [ $$dead -eq 3 ]; then echo "mutant $$g ($$f): $$by without a FAIL verdict, not a detection (FAIL)" > $$d/msg; echo 1 > $$d/fail; \
201 elif [ $$dead -eq 2 ]; then echo "mutant $$g ($$f): does not link (FAIL)" > $$d/msg; echo 1 > $$d/fail; \
202 else echo "mutant $$g ($$f): SURVIVED (FAIL)" > $$d/msg; echo 1 > $$d/fail; fi \
203 ) & \
204 done; \
205 wait; \
206 for g in $(STORE_GUARDS); do \
207 d=$(OUT)/smut_$$g; \
208 if [ -f $$d/msg ]; then cat $$d/msg; else echo "mutant $$g: no result recorded (FAIL)"; fi; \
209 if [ -f $$d/killed ] && [ ! -f $$d/fail ]; then killed=$$((killed + 1)); else fail=1; fi; \
210 done; \
211 echo "store mutants: $$killed of $$total killed"; \
212 if [ $$fail -eq 0 ] && [ $$total -ge 12 ]; then echo "AIENOS_STORE_MUTANTS: PASS"; else echo "AIENOS_STORE_MUTANTS: FAIL"; exit 1; fi
213
214# Freestanding: no libc, no builtins. store_mem.c supplies mem* for this
215# build only. Every undefined symbol of the Store objects must be defined by
216# a sibling native object, so the combined relocatable has none left.
217FS = $(OUT)/fs
218FSFLAGS = -std=gnu11 -O2 -Wall -Wextra -Werror -ffreestanding -nostdlib -fno-builtin \
219 -fno-stack-protector -fno-tree-loop-distribute-patterns -fno-pic
220FS_MINE = $(STORE_SRCS) torn_slot.c store_mem.c
221freestanding: | $(OUT)
222 @mkdir -p $(FS); rm -f $(FS)/*.o
223 @for s in $(FS_MINE); do $(CC) $(FSFLAGS) $(INC) -c -o $(FS)/$${s%.c}.o $$s || exit 1; done
224 @for s in $(DEP_SRCS); do b=$$(basename $$s .c); $(CC) $(FSFLAGS) -c -o $(FS)/sib_$$b.o $$s || exit 1; done
225 @for s in $(FS_MINE); do echo "nm -u $${s%.c}.o: $$($(NM) -u $(FS)/$${s%.c}.o | awk '{print $$NF}' | tr '\n' ' ')"; done
226 @$(LD) -r -o $(FS)/store_all.o $(FS)/*.o
227 @u=$$($(NM) -u $(FS)/store_all.o | awk '{print $$NF}' | tr '\n' ' '); \
228 echo "nm -u store_all.o (Store + torn_slot + store_mem + argus/sha256 + disk + m5 + crypto): [$$u]"; \
229 if [ -z "$$u" ]; then echo "AIENOS_STORE_FREESTANDING: PASS"; else echo "AIENOS_STORE_FREESTANDING: FAIL"; exit 1; fi
230
231clean:
232 rm -rf $(OUT)
233
234.PHONY: all test lib-checks sanitize mutants torn-slot-mutants store-mutants freestanding clean