aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / net / tests / net_diff.c
| 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 | /* Differential corpus tool for the M6-A protocol layer. |
| 18 | * |
| 19 | * net_diff gen SEED COUNT > corpus.bin structure-aware hostile corpus |
| 20 | * net_diff run < corpus.bin one canonical result line per record |
| 21 | * |
| 22 | * The same corpus is fed to the unmodified Rust reference (net.rs and |
| 23 | * virtio_net.rs) by a driver kept OUTSIDE this repository (no Rust in AIENOS |
| 24 | * code). Both outputs must be byte-identical; the agreed output digest is |
| 25 | * pinned in the Makefile (`make diff-check`) so C regressions are caught |
| 26 | * without Rust. Record: kind u8, length u16 big-endian, bytes. */ |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | #include "../aienos_net.h" |
| 32 | #include "../aienos_virtio_pci.h" |
| 33 | |
| 34 | enum { K_ETH = 1, K_ARP, K_IPV4, K_ICMP, K_UDP, K_CSUM, K_VIRTIO, K_UDP_BUILD, K_IPV4_BUILD, K_ICMP_BUILD, K_MAX }; |
| 35 | |
| 36 | static uint64_t rng; |
| 37 | static uint32_t rnd(void) { rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; return (uint32_t)(rng >> 11); } |
| 38 | |
| 39 | static void emit(int kind, const uint8_t *b, size_t n) |
| 40 | { |
| 41 | uint8_t h[3] = {(uint8_t)kind, (uint8_t)(n >> 8), (uint8_t)n}; |
| 42 | fwrite(h, 1, 3, stdout); |
| 43 | fwrite(b, 1, n, stdout); |
| 44 | } |
| 45 | |
| 46 | static void fix_csum(uint8_t *b, size_t at, size_t from, size_t to) |
| 47 | { |
| 48 | b[at] = b[at + 1] = 0; |
| 49 | uint16_t c = net_checksum(b + from, to - from); |
| 50 | b[at] = (uint8_t)(c >> 8); b[at + 1] = (uint8_t)c; |
| 51 | } |
| 52 | |
| 53 | /* Mutate: flips, boundary length fields, truncation, extension. */ |
| 54 | static size_t mutate(uint8_t *b, size_t n, size_t cap) |
| 55 | { |
| 56 | unsigned m = rnd() % 8; |
| 57 | for (unsigned i = 0; i < m && n; i++) { |
| 58 | switch (rnd() % 6) { |
| 59 | case 0: b[rnd() % n] ^= (uint8_t)(1u << (rnd() % 8)); break; |
| 60 | case 1: b[rnd() % n] = (uint8_t)rnd(); break; |
| 61 | case 2: n = rnd() % (n + 1); break; |
| 62 | case 3: { size_t add = rnd() % 16; while (add-- && n < cap) b[n++] = (uint8_t)rnd(); } break; |
| 63 | case 4: { static const uint8_t v[] = {0, 1, 7, 8, 19, 20, 0x45, 0x7f, 0x80, 0xff}; b[rnd() % n] = v[rnd() % sizeof v]; } break; |
| 64 | default: break; |
| 65 | } |
| 66 | } |
| 67 | return n; |
| 68 | } |
| 69 | |
| 70 | static void gen_one(void) |
| 71 | { |
| 72 | uint8_t b[4096]; |
| 73 | size_t n = 0; |
| 74 | int kind = 1 + (int)(rnd() % (K_MAX - 1)); |
| 75 | for (size_t i = 0; i < sizeof b; i++) b[i] = (uint8_t)rnd(); |
| 76 | int structured = rnd() % 4 != 0; |
| 77 | switch (kind) { |
| 78 | case K_ETH: n = rnd() % 40; break; |
| 79 | case K_ARP: |
| 80 | n = 28 + rnd() % 4; |
| 81 | if (structured) { const uint8_t h[8] = {0, 1, 8, 0, 6, 4, 0, (uint8_t)(1 + rnd() % 2)}; memcpy(b, h, 8); } |
| 82 | break; |
| 83 | case K_IPV4: case K_ICMP: case K_UDP: { |
| 84 | /* IPv4 header (+opts) | payload, built valid, then maybe mutated */ |
| 85 | size_t ihl = 5 + (rnd() % 4 == 0 ? rnd() % 3 : 0); |
| 86 | size_t pl = rnd() % 64; |
| 87 | n = ihl * 4 + pl; |
| 88 | b[0] = (uint8_t)(0x40 | ihl); b[2] = (uint8_t)(n >> 8); b[3] = (uint8_t)n; |
| 89 | b[6] &= 0x40; b[7] = 0; if (!b[8]) b[8] = 1; |
| 90 | if (kind == K_IPV4) { fix_csum(b, 10, 0, ihl * 4); break; } |
| 91 | if (kind == K_ICMP) { |
| 92 | /* the ICMP record is the IPv4 payload only */ |
| 93 | memmove(b, b + ihl * 4, pl); n = pl < 8 ? 8 : pl; |
| 94 | b[0] = rnd() % 2 ? 0 : 8; b[1] = 0; fix_csum(b, 2, 0, n); |
| 95 | break; |
| 96 | } |
| 97 | /* UDP record: src(4) dst(4) datagram */ |
| 98 | n = 8 + 8 + pl; |
| 99 | b[12] = (uint8_t)((8 + pl) >> 8); b[13] = (uint8_t)(8 + pl); |
| 100 | if (rnd() % 5 == 0) { b[14] = b[15] = 0; } |
| 101 | else { |
| 102 | uint8_t tmp[4096]; net_udp_header u = {(uint16_t)(b[8] << 8 | b[9]), (uint16_t)(b[10] << 8 | b[11])}; |
| 103 | net_ipv4 s, d; memcpy(s.b, b, 4); memcpy(d.b, b + 4, 4); size_t w; |
| 104 | if (net_udp_build(&u, b + 16, pl, s, d, tmp, sizeof tmp, &w) == NET_OK) memcpy(b + 8, tmp, w); |
| 105 | } |
| 106 | break; |
| 107 | } |
| 108 | case K_CSUM: n = rnd() % 300; break; |
| 109 | case K_VIRTIO: { |
| 110 | n = rnd() % 16 == 0 ? (rnd() % 2 ? 4096 : 255) : 256; |
| 111 | memset(b, 0, 256); |
| 112 | b[6] = 16; b[0x34] = (uint8_t)(0x40 + 4 * (rnd() % 4)); |
| 113 | size_t at = b[0x34]; |
| 114 | unsigned caps = rnd() % 7; |
| 115 | for (unsigned i = 0; i < caps && at + 20 <= 256; i++) { |
| 116 | uint8_t kd = (uint8_t)(rnd() % 6), cl = kd == 2 ? 20 : 16; |
| 117 | size_t nx = at + cl + 4 * (rnd() % 3); |
| 118 | b[at] = rnd() % 5 ? 9 : 1; b[at + 1] = (uint8_t)(i + 1 == caps || nx + 20 > 256 ? 0 : nx); |
| 119 | b[at + 2] = cl; b[at + 3] = kd; b[at + 4] = (uint8_t)(rnd() % 7); |
| 120 | uint32_t off = rnd() % 3 ? rnd() % 0x10000 : rnd() * 3u, len = rnd() % 4 ? rnd() % 0x400 : rnd() * 5u; |
| 121 | for (int k = 0; k < 4; k++) { b[at + 8 + k] = (uint8_t)(off >> (8 * k)); b[at + 12 + k] = (uint8_t)(len >> (8 * k)); } |
| 122 | if (rnd() % 8 == 0) b[at + 1] = b[0x34]; /* loop */ |
| 123 | at = nx; |
| 124 | } |
| 125 | if (structured) n = n == 256 ? 256 : n; /* mutations below may still break it */ |
| 126 | if (rnd() % 2) { size_t m = n; mutate(b, 256, 256); n = m; } |
| 127 | emit(kind, b, n); |
| 128 | return; |
| 129 | } |
| 130 | case K_UDP_BUILD: n = 12 + rnd() % 40; break; |
| 131 | case K_IPV4_BUILD: n = 13 + rnd() % 40; break; |
| 132 | case K_ICMP_BUILD: n = 5 + rnd() % 40; break; |
| 133 | } |
| 134 | if (rnd() % 2 || !structured) n = mutate(b, n, sizeof b); |
| 135 | emit(kind, b, n); |
| 136 | } |
| 137 | |
| 138 | static void mac(const net_mac *m) { printf(" %02x%02x%02x%02x%02x%02x", m->b[0], m->b[1], m->b[2], m->b[3], m->b[4], m->b[5]); } |
| 139 | static void ip(const net_ipv4 *a) { printf(" %u.%u.%u.%u", a->b[0], a->b[1], a->b[2], a->b[3]); } |
| 140 | static void hexs(const uint8_t *b, size_t n) { putchar(' '); for (size_t i = 0; i < n; i++) printf("%02x", b[i]); } |
| 141 | static void region(const virtio_pci_region *r) { if (r->present) printf(" %u:%x:%x", r->bar, r->offset, r->length); else printf(" -"); } |
| 142 | |
| 143 | static void run_one(int kind, const uint8_t *b, size_t n) |
| 144 | { |
| 145 | net_err e = NET_OK; |
| 146 | switch (kind) { |
| 147 | case K_ETH: { net_eth_frame f; printf("E"); if ((e = net_eth_parse(b, n, &f)) == NET_OK) { printf(" Ok"); mac(&f.destination); mac(&f.source); printf(" %04x %zu", f.ethertype, f.payload_len); } break; } |
| 148 | case K_ARP: { net_arp_packet a; printf("A"); if ((e = net_arp_parse(b, n, &a)) == NET_OK) { printf(" Ok %u", a.operation); mac(&a.sender_mac); ip(&a.sender_ip); mac(&a.target_mac); ip(&a.target_ip); } break; } |
| 149 | case K_IPV4: { net_ipv4_header h; const uint8_t *p; size_t pl; printf("I"); if ((e = net_ipv4_parse(b, n, &h, &p, &pl)) == NET_OK) { printf(" Ok %u %u %u %u", h.dscp_ecn, h.identification, h.ttl, h.protocol); ip(&h.source); ip(&h.destination); printf(" %zu %zu", (size_t)(p - b), pl); } break; } |
| 150 | case K_ICMP: { net_icmp_echo c; printf("C"); if ((e = net_icmp_echo_parse(b, n, &c)) == NET_OK) printf(" Ok %d %u %u %zu", c.reply, c.identifier, c.sequence, c.data_len); break; } |
| 151 | case K_UDP: { |
| 152 | printf("U"); |
| 153 | if (n < 8) { printf(" Short\n"); return; } |
| 154 | net_ipv4 s, d; memcpy(s.b, b, 4); memcpy(d.b, b + 4, 4); |
| 155 | net_udp_header u; const uint8_t *p; size_t pl; |
| 156 | if ((e = net_udp_parse(b + 8, n - 8, s, d, &u, &p, &pl)) == NET_OK) printf(" Ok %u %u %zu %zu", u.source_port, u.destination_port, (size_t)(p - (b + 8)), pl); |
| 157 | break; |
| 158 | } |
| 159 | case K_CSUM: printf("K %04x\n", net_checksum(b, n)); return; |
| 160 | case K_VIRTIO: { |
| 161 | virtio_pci_caps c; virtio_pci_err ve = virtio_pci_parse_caps(b, n, &c); |
| 162 | printf("V"); |
| 163 | if (ve) { printf(" Err %s\n", virtio_pci_err_name(ve)); return; } |
| 164 | printf(" Ok"); region(&c.common_cfg); region(&c.notify_cfg); |
| 165 | if (c.has_notify_off_multiplier) printf(" %x", c.notify_off_multiplier); else printf(" -"); |
| 166 | region(&c.isr_cfg); region(&c.device_cfg); putchar('\n'); |
| 167 | return; |
| 168 | } |
| 169 | case K_UDP_BUILD: case K_IPV4_BUILD: case K_ICMP_BUILD: { |
| 170 | uint8_t out[4096]; size_t w = 0; |
| 171 | size_t hdr = kind == K_UDP_BUILD ? 12 : kind == K_IPV4_BUILD ? 13 : 5; |
| 172 | printf(kind == K_UDP_BUILD ? "BU" : kind == K_IPV4_BUILD ? "BI" : "BC"); |
| 173 | if (n < hdr) { printf(" Short\n"); return; } |
| 174 | if (kind == K_UDP_BUILD) { |
| 175 | net_ipv4 s, d; memcpy(s.b, b, 4); memcpy(d.b, b + 4, 4); |
| 176 | net_udp_header u = {(uint16_t)(b[8] << 8 | b[9]), (uint16_t)(b[10] << 8 | b[11])}; |
| 177 | e = net_udp_build(&u, b + hdr, n - hdr, s, d, out, sizeof out, &w); |
| 178 | } else if (kind == K_IPV4_BUILD) { |
| 179 | net_ipv4_header h = {b[0], (uint16_t)(b[1] << 8 | b[2]), b[3], b[4], {{b[5], b[6], b[7], b[8]}}, {{b[9], b[10], b[11], b[12]}}}; |
| 180 | e = net_ipv4_build(&h, b + hdr, n - hdr, out, sizeof out, &w); |
| 181 | } else { |
| 182 | net_icmp_echo c = {b[0] & 1, (uint16_t)(b[1] << 8 | b[2]), (uint16_t)(b[3] << 8 | b[4]), 0}; |
| 183 | e = net_icmp_echo_build(&c, b + hdr, n - hdr, out, sizeof out, &w); |
| 184 | } |
| 185 | if (e == NET_OK) { printf(" Ok"); hexs(out, w); } |
| 186 | break; |
| 187 | } |
| 188 | default: printf("?\n"); return; |
| 189 | } |
| 190 | if (e != NET_OK) printf(" Err %s", net_err_name(e)); |
| 191 | putchar('\n'); |
| 192 | } |
| 193 | |
| 194 | int main(int argc, char **argv) |
| 195 | { |
| 196 | if (argc == 4 && strcmp(argv[1], "gen") == 0) { |
| 197 | rng = strtoull(argv[2], NULL, 0); if (!rng) rng = 1; |
| 198 | unsigned long count = strtoul(argv[3], NULL, 0); |
| 199 | for (unsigned long i = 0; i < count; i++) gen_one(); |
| 200 | return 0; |
| 201 | } |
| 202 | if (argc == 2 && strcmp(argv[1], "run") == 0) { |
| 203 | static uint8_t b[65536]; |
| 204 | int k; |
| 205 | while ((k = getchar()) != EOF) { |
| 206 | int h1 = getchar(), h2 = getchar(); |
| 207 | if (h1 == EOF || h2 == EOF) return 2; |
| 208 | size_t n = (size_t)h1 << 8 | (size_t)h2; |
| 209 | if (fread(b, 1, n, stdin) != n) return 2; |
| 210 | run_one(k, b, n); |
| 211 | } |
| 212 | return 0; |
| 213 | } |
| 214 | fprintf(stderr, "usage: net_diff gen SEED COUNT | net_diff run\n"); |
| 215 | return 2; |
| 216 | } |