Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/argus/tests/fuzz_argus_event.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / argus / tests / fuzz_argus_event.c` 268 lines ยท 13463 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * fuzz_argus_event.c -- deterministic decode fuzzer (xorshift64*, fixed seed). 19 * Invariants: argus_event_decode returns exactly what an independent byte-level 20 * oracle (below, written from argus_abi.h) says; on error (VERSION or MALFORMED) 21 * the output is untouched; on success the decoded event re-encodes to exactly 22 * the input bytes. Built with -fsanitize=address,undefined by lane_b.mk. 23 * v1.2: kinds 90-93 in the event oracle, plus the same four modes over 152-byte 24 * ContainmentRequest buffers (decode vs byte oracle, re-encode identity, digest = 25 * SHA-256 of the input, output untouched on error). 26 * Usage: fuzz_argus_event [iterations] (default 1,000,000) 27 */ 28 #include "argus_abi.h" 29 #include "sha256.h" 30 #include 31 #include 32 #include 33 34 static uint64_t rng_state = 0x9E3779B97F4A7C15ull; 35 static uint64_t rnd(void) 36 { 37 uint64_t x = rng_state; 38 x ^= x >> 12; x ^= x << 25; x ^= x >> 27; 39 rng_state = x; 40 return x * 0x2545F4914F6CDD1Dull; 41 } 42 43 static const uint16_t kinds[] = { 1, 2, 3, 4, 10, 11, 12, 20, 21, 22, 30, 31, 32, 40, 41, 42, 43, 44 50, 51, 52, 60, 61, 62, 63, 70, 71, 72, 80, 81, 90, 91, 92, 93 }; 45 46 /* Class floor per kind (argus_abi.h hostile-review rules): 1 CRITICAL, 2 SECURITY, 3 AUDIT, 0 unknown. */ 47 static unsigned floor_of(unsigned k) 48 { 49 switch (k) { 50 case 60: case 61: case 62: case 63: case 80: case 42: case 31: case 32: 51 case 71: case 72: case 4: case 12: case 21: case 52: 52 case 90: case 91: case 92: case 93: return 1; 53 case 1: case 3: case 10: case 20: case 22: case 30: case 40: case 41: case 50: case 51: case 70: return 2; 54 case 2: case 11: case 43: case 81: return 3; 55 default: return 0; 56 } 57 } 58 59 static uint64_t le64(const uint8_t *p) { uint64_t v = 0; for (int i = 7; i >= 0; i--) v = (v << 8) | p[i]; return v; } 60 static uint32_t le32(const uint8_t *p) { return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; } 61 62 /* v1.2 kinds 90-92 (ARGUS1_SPEC section 3): object_id = type | status << 8 | code << 16, 63 * world_generation = request_id. Written as explicit cases, independent of argus_event.c. */ 64 static int contain_ok(unsigned kind, uint32_t oid, unsigned out, unsigned fl, uint64_t request_id) 65 { 66 unsigned type = oid & 0xFFu, st = (oid >> 8) & 0xFFu; 67 if (type < 1 || type > 10 || request_id == 0) return 0; 68 if (kind == 90) return st == 0 && out == 1; 69 if (kind == 91) return (st == 1 && out == 1) || (st == 2 && out == 2) || (st == 3 && out == 3); 70 if (st == 4) return out == 1 && (type == 1 || (fl & 1u)); /* DONE; I2 */ 71 if (st == 5) return out == 1; 72 if (st == 6) return out == 2 || out == 3; 73 if (st == 7 || st == 8) return out == 3; 74 return 0; 75 } 76 77 /* Expected decode result, straight from the bytes. */ 78 static int oracle_bytes(const uint8_t b[ARGUS_EVENT_SIZE]) 79 { 80 unsigned cls = b[1], kind = b[2] | (unsigned)b[3] << 8, eff = b[4], out = b[5], fl = b[6] | (unsigned)b[7] << 8; 81 uint64_t seq = le64(b + 8); 82 if (b[0] != 1) return ARGUS_ERR_VERSION; 83 unsigned f = floor_of(kind); 84 if (cls < 1 || cls > 4 || f == 0 || cls > f) return ARGUS_ERR_MALFORMED; 85 if (eff > 3 || out < 1 || out > 3) return ARGUS_ERR_MALFORMED; /* v1.1: flag bits 2-15 = stream id */ 86 if (((fl & 2u) != 0) != (kind == 80 || kind == 90)) return ARGUS_ERR_MALFORMED; /* v1.2: 90 too */ 87 if (kind >= 90 && kind <= 92 && !contain_ok(kind, le32(b + 36), out, fl, le64(b + 48))) return ARGUS_ERR_MALFORMED; 88 if (le32(b + 32) >= 256 && le32(b + 32) != 0xFFFFFFFFu) return ARGUS_ERR_MALFORMED; /* CAP_NONE ok */ 89 if (kind == 81 && out != 1) return ARGUS_ERR_MALFORMED; /* summary: OK only */ 90 if (kind == 81 && le64(b + 48) > le64(b + 40)) return ARGUS_ERR_MALFORMED; /* summary: min gen <= max gen */ 91 if (seq == 0 || seq == UINT64_MAX) return ARGUS_ERR_MALFORMED; 92 if (kind >= 50 && kind <= 52 && eff != 3) return ARGUS_ERR_MALFORMED; 93 return ARGUS_OK; 94 } 95 96 static void random_bytes(uint8_t *b, size_t n) { for (size_t i = 0; i < n; i++) b[i] = (uint8_t)rnd(); } 97 98 static void valid_event(uint8_t b[ARGUS_EVENT_SIZE]) 99 { 100 ArgusEvent e; 101 random_bytes((uint8_t *)&e, sizeof e); 102 e.version = 1; 103 e.kind = kinds[rnd() % (sizeof kinds / sizeof kinds[0])]; 104 e.class_ = (uint8_t)(1 + rnd() % floor_of(e.kind)); 105 e.effect_class = (e.kind >= 50 && e.kind <= 52) ? ARGUS_EFFECT_EXTERNAL : (uint8_t)(rnd() % 4); 106 e.outcome = e.kind == ARGUS_EV_CAPABILITY_USE_SUMMARY ? ARGUS_OUTCOME_OK : (uint8_t)(1 + rnd() % 3); 107 e.flags = (uint16_t)((rnd() % 2) | ((rnd() % ARGUS_STREAM_MAX) << ARGUS_FLAG_STREAM_SHIFT) | 108 (e.kind == ARGUS_EV_TELEMETRY_DROPPED || e.kind == ARGUS_EV_CONTAINMENT_PROPOSED ? ARGUS_FLAG_CONSUMER : 0)); 109 if (e.kind >= ARGUS_EV_CONTAINMENT_PROPOSED && e.kind <= ARGUS_EV_CONTAINMENT_EXECUTED) { /* v1.2 */ 110 static const uint8_t st90[] = { 0 }, st91[] = { 1, 2, 3 }, st92[] = { 4, 5, 6, 6, 7, 8 }; 111 static const uint8_t out90[] = { 1 }, out91[] = { 1, 2, 3 }, out92[] = { 1, 1, 2, 3, 3, 3 }; 112 unsigned type = 1 + (unsigned)(rnd() % 10), j; 113 if (e.kind == 90) { j = 0; e.outcome = out90[j]; e.object_id = ARGUS_CONTAIN_PACK(type, st90[j], rnd()); } 114 else if (e.kind == 91) { j = (unsigned)(rnd() % 3); e.outcome = out91[j]; e.object_id = ARGUS_CONTAIN_PACK(type, st91[j], rnd()); } 115 else { j = (unsigned)(rnd() % 6); e.outcome = out92[j]; e.object_id = ARGUS_CONTAIN_PACK(type, st92[j], rnd()); 116 if (st92[j] == 4 && type != 1) e.flags |= ARGUS_FLAG_SYNTHETIC; } 117 if (e.world_generation == 0) e.world_generation = 1; 118 } 119 e.cap_id = rnd() % 8 == 0 ? ARGUS_CAP_NONE : (uint32_t)(rnd() % ARGUS_CAP_MAX); 120 if (e.kind == ARGUS_EV_CAPABILITY_USE_SUMMARY && e.world_generation > e.cap_generation) 121 e.world_generation = e.cap_generation - (e.cap_generation ? rnd() % (e.cap_generation < 1000 ? e.cap_generation : 1000) : 0); 122 if (e.sequence == 0 || e.sequence == UINT64_MAX) e.sequence = 1; 123 if (argus_event_encode(&e, b) != ARGUS_OK || argus_event_validate(&e) != ARGUS_OK) { 124 fprintf(stderr, "FAIL: generated valid event did not validate/encode\n"); 125 exit(1); 126 } 127 } 128 129 /* ---- v1.2 ContainmentRequest decode (152 bytes) ---- */ 130 static int oracle_request(const uint8_t b[ARGUS_CONTAIN_REQUEST_SIZE]) 131 { 132 if (b[150] != 2) return ARGUS_ERR_VERSION; 133 if (b[8] < 1 || b[8] > 10) return ARGUS_ERR_MALFORMED; /* containment type */ 134 if (b[9] < 1 || b[9] > 5) return ARGUS_ERR_MALFORMED; /* severity INFO..CRITICAL */ 135 if (b[151] != 0) return ARGUS_ERR_MALFORMED; /* reserved */ 136 if ((b[148] & ~3u) != 0 || b[149] != 0) return ARGUS_ERR_MALFORMED; /* flags SYNTHETIC|SATURATION */ 137 if (le64(b + 92) == 0 || le64(b + 100) == 0) return ARGUS_ERR_MALFORMED; /* request_id, finding_sequence */ 138 return ARGUS_OK; 139 } 140 141 static void valid_request(uint8_t b[ARGUS_CONTAIN_REQUEST_SIZE]) 142 { 143 random_bytes(b, ARGUS_CONTAIN_REQUEST_SIZE); 144 b[8] = (uint8_t)(1 + rnd() % 10); 145 b[9] = (uint8_t)(1 + rnd() % 5); 146 b[148] = (uint8_t)(rnd() % 4); b[149] = 0; 147 b[150] = 2; b[151] = 0; 148 if (le64(b + 92) == 0) b[92] = 1; 149 if (le64(b + 100) == 0) b[100] = 1; 150 } 151 152 /* Returns the number of violations. Modes: fully random, random with version 2, valid + 1..4 153 * bit flips, valid + one random byte. On success the request re-encodes to the input and 154 * its digest is SHA-256 of the input; on error the output is untouched. */ 155 static unsigned long fuzz_requests(unsigned long iters) 156 { 157 unsigned long ok[4] = {0}, err[4] = {0}, bad = 0; 158 const char *mode_name[4] = { "fully random", "random, version=2", "valid + 1..4 bit flips", "valid + random byte" }; 159 for (unsigned long i = 0; i < iters; i++) { 160 uint8_t in[ARGUS_CONTAIN_REQUEST_SIZE], re[ARGUS_CONTAIN_REQUEST_SIZE], d1[32], d2[32]; 161 unsigned mode = (unsigned)(i % 4); 162 switch (mode) { 163 case 0: random_bytes(in, sizeof in); break; 164 case 1: random_bytes(in, sizeof in); in[150] = 2; break; 165 case 2: { 166 valid_request(in); 167 unsigned flips = 1 + (unsigned)(rnd() % 4); 168 for (unsigned f = 0; f < flips; f++) { 169 unsigned bit = (unsigned)(rnd() % (ARGUS_CONTAIN_REQUEST_SIZE * 8)); 170 in[bit / 8] ^= (uint8_t)(1u << (bit % 8)); 171 } 172 break; 173 } 174 default: valid_request(in); in[rnd() % ARGUS_CONTAIN_REQUEST_SIZE] = (uint8_t)rnd(); break; 175 } 176 ArgusContainmentRequest out, sentinel; 177 memset(&out, 0xA5, sizeof out); 178 sentinel = out; 179 int rc = argus_contain_request_decode(in, &out), want = oracle_request(in); 180 if (rc != want) { bad++; fprintf(stderr, "FAIL request iteration %lu: decode %d, oracle %d\n", i, rc, want); } 181 if (rc == ARGUS_OK) { 182 ok[mode]++; 183 argus_contain_request_digest(&out, d1); 184 sha256_hash(in, sizeof in, d2); 185 if (argus_contain_request_encode(&out, re) != ARGUS_OK || memcmp(in, re, sizeof in) != 0 || memcmp(d1, d2, 32) != 0) { 186 bad++; fprintf(stderr, "FAIL request iteration %lu: re-encode or digest differs\n", i); 187 } 188 } else { 189 err[mode]++; 190 if (memcmp(&out, &sentinel, sizeof out) != 0) { bad++; fprintf(stderr, "FAIL request iteration %lu: output written on error\n", i); } 191 } 192 if (bad > 10) break; 193 } 194 unsigned long tot_ok = 0, tot_err = 0; 195 for (unsigned m = 0; m < 4; m++) { 196 printf(" request %-24s accepted %8lu rejected %8lu\n", mode_name[m], ok[m], err[m]); 197 tot_ok += ok[m]; tot_err += err[m]; 198 } 199 printf("fuzz_argus_event: %lu request buffers, %lu accepted (re-encoded byte-identical, digest = SHA-256 of input), " 200 "%lu rejected, every result matched the byte oracle, %lu violations\n", tot_ok + tot_err, tot_ok, tot_err, bad); 201 if (tot_ok + tot_err != iters) bad++; 202 return bad; 203 } 204 205 int main(int argc, char **argv) 206 { 207 unsigned long iters = argc > 1 ? strtoul(argv[1], NULL, 10) : 1000000ul; 208 unsigned long ok[4] = {0}, err[4] = {0}, bad = 0; 209 const char *mode_name[4] = { "fully random", "random, version=1", "valid + 1..4 bit flips", "valid + random byte" }; 210 for (unsigned long i = 0; i < iters; i++) { 211 uint8_t in[ARGUS_EVENT_SIZE], re[ARGUS_EVENT_SIZE]; 212 unsigned mode = (unsigned)(i % 4); 213 switch (mode) { 214 case 0: random_bytes(in, sizeof in); break; 215 case 1: random_bytes(in, sizeof in); in[0] = 1; break; 216 case 2: { 217 valid_event(in); 218 unsigned flips = 1 + (unsigned)(rnd() % 4); 219 for (unsigned f = 0; f < flips; f++) { 220 unsigned bit = (unsigned)(rnd() % (ARGUS_EVENT_SIZE * 8)); 221 in[bit / 8] ^= (uint8_t)(1u << (bit % 8)); 222 } 223 break; 224 } 225 default: 226 valid_event(in); 227 in[rnd() % ARGUS_EVENT_SIZE] = (uint8_t)rnd(); 228 break; 229 } 230 ArgusEvent out, sentinel; 231 memset(&out, 0xA5, sizeof out); 232 sentinel = out; 233 int rc = argus_event_decode(in, &out); 234 if (rc != oracle_bytes(in)) { 235 bad++; 236 fprintf(stderr, "FAIL iteration %lu: decode %d, oracle %d\n", i, rc, oracle_bytes(in)); 237 } 238 if (rc == ARGUS_OK) { 239 ok[mode]++; 240 if (argus_event_encode(&out, re) != ARGUS_OK || memcmp(in, re, sizeof in) != 0) { 241 bad++; 242 fprintf(stderr, "FAIL iteration %lu: decoded event does not re-encode to input\n", i); 243 } 244 } else { 245 err[mode]++; 246 if (rc != ARGUS_ERR_VERSION && rc != ARGUS_ERR_MALFORMED) { 247 bad++; 248 fprintf(stderr, "FAIL iteration %lu: unexpected error %d\n", i, rc); 249 } 250 if (memcmp(&out, &sentinel, sizeof out) != 0) { 251 bad++; 252 fprintf(stderr, "FAIL iteration %lu: decode wrote output on error\n", i); 253 } 254 } 255 if (bad > 10) break; 256 } 257 unsigned long tot_ok = 0, tot_err = 0; 258 for (unsigned m = 0; m < 4; m++) { 259 printf(" %-24s accepted %8lu rejected %8lu\n", mode_name[m], ok[m], err[m]); 260 tot_ok += ok[m]; tot_err += err[m]; 261 } 262 printf("fuzz_argus_event: %lu iterations, %lu accepted (all re-encoded byte-identical), %lu rejected, every result matched the byte oracle, %lu violations\n", 263 tot_ok + tot_err, tot_ok, tot_err, bad); 264 bad += fuzz_requests(iters); 265 if (bad || tot_ok + tot_err != iters) { printf("fuzz_argus_event: FAIL\n"); return 1; } 266 printf("fuzz_argus_event: PASS\n"); 267 return 0; 268 }