omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / aienux / disk / tests / test_gpt_ext4_ro.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/disk/tests/test_gpt_ext4_ro.om: Host test for gpt_ro.om + ext4_ro.om + sha256_bare.om. |
| 3 | note Reads a synthetic GPT + ext4 disk image from argv[1] (generated by run_slice_d.sh) and verifies: |
| 4 | note 1. GPT Protective MBR + Primary/Backup CRC32 + Linux Filesystem partition selection. |
| 5 | note 2. 64-bit ext4 block group descriptors (desc_size=64) + depth-1 extent tree + 48-bit block addressing. |
| 6 | note 3. Reading /aienos/spark-bundle.img (includes normal extent, unwritten extent ee_len>32768, sparse hole) |
| 7 | note + parsing OMFWBND1 TOC + verifying per-entry SHA-256 against expected digests. |
| 8 | note 4. Fail-closed refusals on /bad/inline (E4_E_INLINE), /bad/encrypt (E4_E_ENCRYPT), |
| 9 | note /bad/noext (E4_E_NOEXTENT), /bad/badmagic (E4_E_EXT_MAGIC), and corrupted SHA-256 (E4_E_SHA). |
| 10 | |
| 11 | global t_fd int 1 |
| 12 | global t_arg uchar 256 |
| 13 | global t_sink_mode ulong 1 |
| 14 | global t_sink_off ulong 1 |
| 15 | global t_out uchar 65536 |
| 16 | |
| 17 | text P_BND "/aienos/spark-bundle.img" |
| 18 | text P_SPARSE "/aienos/sparse.bin" |
| 19 | text P_INLINE "/bad/inline" |
| 20 | text P_ENCRYPT "/bad/encrypt" |
| 21 | text P_NOEXT "/bad/noext" |
| 22 | text P_BADMAGIC "/bad/badmagic" |
| 23 | |
| 24 | fn t_pread_lba lba ulong is_gpt ulong to int |
| 25 | fd = get t_fd 0 |
| 26 | off = long mul lba 512 |
| 27 | p = ulong 0 |
| 28 | while lt p 512 bound 512 |
| 29 | r = long 0 |
| 30 | if ne is_gpt 0 |
| 31 | r = pread fd gpt_sec p sub 512 p add off long p |
| 32 | else |
| 33 | r = pread fd e4_sec p sub 512 p add off long p |
| 34 | end |
| 35 | if le r 0 |
| 36 | ret -1 |
| 37 | end |
| 38 | p = add p ulong r |
| 39 | end |
| 40 | ret 0 |
| 41 | end |
| 42 | |
| 43 | fn gpt_read_lba lba ulong to int |
| 44 | ret t_pread_lba lba 1 |
| 45 | end |
| 46 | |
| 47 | fn ext4_read_lba lba ulong to int |
| 48 | ret t_pread_lba lba 0 |
| 49 | end |
| 50 | |
| 51 | fn ext4_sink off ulong b uchar to void |
| 52 | m = get t_sink_mode 0 |
| 53 | if eq m 1 |
| 54 | if lt off 4096 |
| 55 | put e4_toc off b |
| 56 | end |
| 57 | ret |
| 58 | end |
| 59 | if eq m 2 |
| 60 | if lt off 65536 |
| 61 | put t_out off b |
| 62 | end |
| 63 | end |
| 64 | end |
| 65 | |
| 66 | fn main to int |
| 67 | if lt argcnt 2 |
| 68 | ret 2 |
| 69 | end |
| 70 | n = argread 1 t_arg 0 255 |
| 71 | if lt n 1 |
| 72 | ret 2 |
| 73 | end |
| 74 | put t_arg ulong n 0 |
| 75 | fd = fopen t_arg 0 ulong n 0 0 |
| 76 | if lt fd 0 |
| 77 | ret 3 |
| 78 | end |
| 79 | put t_fd 0 int fd |
| 80 | |
| 81 | note 1. Parse GPT (4096 sectors = 2 MiB synthetic disk) and select Linux root partition |
| 82 | r = gpt_ro_parse 4096 |
| 83 | if ne r GPT_OK |
| 84 | ret 10 |
| 85 | end |
| 86 | r = gpt_ro_select_linux_root |
| 87 | if ne r GPT_OK |
| 88 | ret 11 |
| 89 | end |
| 90 | if or ne gpt_ro_part_index 2 ne gpt_ro_first_lba 64 |
| 91 | ret 12 |
| 92 | end |
| 93 | |
| 94 | note 2. Mount read-only ext4 on partition 2 |
| 95 | r = ext4_ro_mount gpt_ro_first_lba gpt_ro_sectors |
| 96 | if ne r E4_OK |
| 97 | ret 13 |
| 98 | end |
| 99 | |
| 100 | note 3. Lookup and read /aienos/sparse.bin (block 0=data 0xA5, block 1=unwritten ee_len=32769, block 2=sparse hole, block 3=data 0x5A) |
| 101 | for i ulong len P_SPARSE |
| 102 | put e4_path i get P_SPARSE i |
| 103 | end |
| 104 | r = ext4_ro_lookup len P_SPARSE |
| 105 | if ne r E4_OK |
| 106 | ret 14 |
| 107 | end |
| 108 | if ne ext4_ro_file_size 16384 |
| 109 | ret 15 |
| 110 | end |
| 111 | put t_sink_mode 0 2 |
| 112 | r = ext4_ro_stream 0 16384 1 |
| 113 | if ne r E4_OK |
| 114 | ret 16 |
| 115 | end |
| 116 | note Verify block 0 == 0xA5, block 1 (unwritten) == 0x00, block 2 (sparse hole) == 0x00, block 3 == 0x5A |
| 117 | b = ulong 0 |
| 118 | while lt b 4096 bound 4096 |
| 119 | if ne get t_out b 165 |
| 120 | ret 17 |
| 121 | end |
| 122 | if ne get t_out add 4096 b 0 |
| 123 | ret 18 |
| 124 | end |
| 125 | if ne get t_out add 8192 b 0 |
| 126 | ret 19 |
| 127 | end |
| 128 | if ne get t_out add 12288 b 90 |
| 129 | ret 20 |
| 130 | end |
| 131 | b = add b 1 |
| 132 | end |
| 133 | |
| 134 | note 4. Lookup /aienos/spark-bundle.img, parse OMFWBND1 TOC, and verify SHA-256 of every entry |
| 135 | for i2 ulong len P_BND |
| 136 | put e4_path i2 get P_BND i2 |
| 137 | end |
| 138 | r = ext4_ro_lookup len P_BND |
| 139 | if ne r E4_OK |
| 140 | ret 21 |
| 141 | end |
| 142 | note Verify contiguous extent-run helper returns full 9-block run for 36864 B bundle |
| 143 | if ne ext4_ro_map_extent 0 E4_OK |
| 144 | ret 33 |
| 145 | end |
| 146 | if ne ext4_ro_run_blks 9 |
| 147 | ret 34 |
| 148 | end |
| 149 | bsz = ext4_ro_file_size |
| 150 | put t_sink_mode 0 1 |
| 151 | r = ext4_ro_stream 0 4096 0 |
| 152 | if ne r E4_OK |
| 153 | ret 22 |
| 154 | end |
| 155 | r = bundle_parse_toc bsz |
| 156 | if ne r E4_OK |
| 157 | ret 23 |
| 158 | end |
| 159 | nent = get e4_bnd_meta 2 |
| 160 | if ne nent 7 |
| 161 | ret 24 |
| 162 | end |
| 163 | put t_sink_mode 0 0 |
| 164 | ei = ulong 0 |
| 165 | while lt ei nent bound 8 |
| 166 | r = ext4_ro_stream get e4_bnd_off ei get e4_bnd_size ei 1 |
| 167 | if ne r E4_OK |
| 168 | ret 25 |
| 169 | end |
| 170 | for s ulong 32 |
| 171 | put e4_want_sha s get e4_bnd_sha add mul ei 32 s |
| 172 | end |
| 173 | if ne ext4_ro_verify_sha E4_OK |
| 174 | ret 26 |
| 175 | end |
| 176 | ei = add ei 1 |
| 177 | end |
| 178 | note Negative SHA-256 test: flip 1 bit in e4_want_sha[0], must fail closed with E4_E_SHA |
| 179 | put e4_want_sha 0 xor get e4_want_sha 0 1 |
| 180 | if ne ext4_ro_verify_sha E4_E_SHA |
| 181 | ret 27 |
| 182 | end |
| 183 | |
| 184 | note Negative TOC tests: bundle_len < 4096, sz == 0, and overlapping entries MUST fail closed |
| 185 | if ne bundle_parse_toc 2048 E4_E_TOC |
| 186 | ret 35 |
| 187 | end |
| 188 | note Zero size on entry 0 (offset 32 + 16 = 48) |
| 189 | sv0 = get e4_toc 48 |
| 190 | sv1 = get e4_toc 49 |
| 191 | put e4_toc 48 0 |
| 192 | put e4_toc 49 0 |
| 193 | if ne bundle_parse_toc bsz E4_E_TOC |
| 194 | ret 36 |
| 195 | end |
| 196 | put e4_toc 48 sv0 |
| 197 | put e4_toc 49 sv1 |
| 198 | note Force entry 1 offset (32 + 64 + 8 = 104) to equal entry 0 offset (4096) -> overlap! |
| 199 | put e4_toc 104 0 |
| 200 | put e4_toc 105 16 |
| 201 | put e4_toc 106 0 |
| 202 | put e4_toc 107 0 |
| 203 | if ne bundle_parse_toc bsz E4_E_TOC |
| 204 | ret 37 |
| 205 | end |
| 206 | |
| 207 | note Negative non-regular-file stream test: load directory inode 2 -> MUST fail with E4_E_INO |
| 208 | if ne ext4_ro_load_inode 2 E4_OK |
| 209 | ret 38 |
| 210 | end |
| 211 | if ne ext4_ro_stream 0 16 0 E4_E_INO |
| 212 | ret 39 |
| 213 | end |
| 214 | |
| 215 | note 5. Hostile inode refusals (fail-closed) |
| 216 | for i3 ulong len P_INLINE |
| 217 | put e4_path i3 get P_INLINE i3 |
| 218 | end |
| 219 | if ne ext4_ro_lookup len P_INLINE E4_E_INLINE |
| 220 | ret 28 |
| 221 | end |
| 222 | |
| 223 | for i4 ulong len P_ENCRYPT |
| 224 | put e4_path i4 get P_ENCRYPT i4 |
| 225 | end |
| 226 | if ne ext4_ro_lookup len P_ENCRYPT E4_E_ENCRYPT |
| 227 | ret 29 |
| 228 | end |
| 229 | |
| 230 | for i5 ulong len P_NOEXT |
| 231 | put e4_path i5 get P_NOEXT i5 |
| 232 | end |
| 233 | if ne ext4_ro_lookup len P_NOEXT E4_E_NOEXTENT |
| 234 | ret 30 |
| 235 | end |
| 236 | |
| 237 | for i6 ulong len P_BADMAGIC |
| 238 | put e4_path i6 get P_BADMAGIC i6 |
| 239 | end |
| 240 | r = ext4_ro_lookup len P_BADMAGIC |
| 241 | if ne r E4_OK |
| 242 | ret 31 |
| 243 | end |
| 244 | if ne ext4_ro_map_block 0 E4_E_EXT_MAGIC |
| 245 | ret 32 |
| 246 | end |
| 247 | |
| 248 | z = fclose get t_fd 0 |
| 249 | |
| 250 | note 6. Optional argv[2]: META_BG (0x10) superblock MUST fail closed with E4_E_FEATURE |
| 251 | if ge argcnt 3 |
| 252 | n2 = argread 2 t_arg 0 255 |
| 253 | if lt n2 1 |
| 254 | ret 40 |
| 255 | end |
| 256 | put t_arg ulong n2 0 |
| 257 | fd2 = fopen t_arg 0 ulong n2 0 0 |
| 258 | if lt fd2 0 |
| 259 | ret 41 |
| 260 | end |
| 261 | put t_fd 0 int fd2 |
| 262 | if ne ext4_ro_mount 64 3999 E4_E_FEATURE |
| 263 | ret 42 |
| 264 | end |
| 265 | z2 = fclose get t_fd 0 |
| 266 | end |
| 267 | |
| 268 | ret 0 |
| 269 | end |