physics-mv
FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence.
Brought by drakestapleton
physics-mv / tests / test_forge_aegis_host.c
| 1 | /* |
| 2 | * Host-only checks of the AEGIS verifier and the submit-side refusals of the |
| 3 | * FORGE seam (physics #53). No GPU and no NVRM: the descriptor is the frozen |
| 4 | * KAT descriptor, and every submit call here is refused before any hardware |
| 5 | * access. Run: tests/run_forge_aegis_host_gates.sh |
| 6 | */ |
| 7 | #include "forge_descriptor.h" |
| 8 | #include "forge_realize.h" |
| 9 | #include "forge_types.h" |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | |
| 15 | static int g_total = 0, g_passed = 0, g_failed = 0; |
| 16 | |
| 17 | static void report(const char *name, int ok) { |
| 18 | g_total++; |
| 19 | if (ok) { |
| 20 | g_passed++; |
| 21 | printf("[PASS] %s\n", name); |
| 22 | } else { |
| 23 | g_failed++; |
| 24 | printf("[FAIL] %s\n", name); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | void sha256_compute(const uint8_t *data, uint64_t len, uint8_t scratch_buf[128], uint32_t out_digest[8]); |
| 29 | |
| 30 | /* SHA-256 as bytes, the same encoding forge_realize.c uses for its digests. */ |
| 31 | static void sha256_out(const uint8_t *data, size_t len, uint8_t out[32]) { |
| 32 | uint8_t scratch[128]; |
| 33 | uint32_t words[8]; |
| 34 | sha256_compute(data, len, scratch, words); |
| 35 | for (int i = 0; i < 8; i++) { |
| 36 | out[i * 4 + 0] = (uint8_t)(words[i] >> 24); |
| 37 | out[i * 4 + 1] = (uint8_t)(words[i] >> 16); |
| 38 | out[i * 4 + 2] = (uint8_t)(words[i] >> 8); |
| 39 | out[i * 4 + 3] = (uint8_t)words[i]; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /* Same values as the frozen KAT in forge_descriptor_run_kat(). */ |
| 44 | static void kat_descriptor(ForgeMachineDescriptor *d) { |
| 45 | memset(d, 0, sizeof(*d)); |
| 46 | d->pci_vendor_id = 0x10de; |
| 47 | d->pci_device_id = 0x2e12; |
| 48 | d->pci_subsystem_vendor_id = 0x10de; |
| 49 | d->pci_subsystem_device_id = 0x0000; |
| 50 | snprintf(d->pci_bdf, sizeof(d->pci_bdf), "000f:01:00.0"); |
| 51 | for (int i = 0; i < 16; i++) d->gpu_uuid[i] = (uint8_t)(i + 1); |
| 52 | d->rm_compute_class = 0x0000cec0; |
| 53 | d->rm_sm_version = 0x00000a04; |
| 54 | d->warp_size = 32; |
| 55 | d->max_warps_per_sm = 48; |
| 56 | d->gpu_core_count = 6144; |
| 57 | d->sm_per_tpc = 2; |
| 58 | snprintf(d->driver_version, sizeof(d->driver_version), "580.173.02"); |
| 59 | snprintf(d->vbios_version, sizeof(d->vbios_version), "9a.0b.2d.00.00"); |
| 60 | snprintf(d->firmware_version, sizeof(d->firmware_version), "580.173.02"); |
| 61 | d->total_system_memory_bytes = 130660888576ULL; |
| 62 | d->dma_mask = 0x1ffffffffffULL; |
| 63 | d->page_size = 4096; |
| 64 | d->max_threads_per_block = 1024; |
| 65 | d->shared_mem_per_block_bytes = 49152; |
| 66 | d->features = 0x1f; |
| 67 | d->observation_flags = 0x1ffff; |
| 68 | } |
| 69 | |
| 70 | static void make_request(ForgeRealizationRequest *req, const ForgeMachineDescriptor *desc, |
| 71 | const uint8_t desc_digest[32], uint8_t program_byte, uint32_t opcodes, |
| 72 | const uint8_t *ir, size_t ir_size) { |
| 73 | memset(req, 0, sizeof(*req)); |
| 74 | memset(req->program_digest, program_byte, 32); |
| 75 | req->opcode_count = opcodes; |
| 76 | req->ir_payload = ir; |
| 77 | req->ir_size = ir_size; |
| 78 | req->target_machine = *desc; |
| 79 | memcpy(req->target_descriptor_digest, desc_digest, 32); |
| 80 | } |
| 81 | |
| 82 | int main(void) { |
| 83 | printf("=== FORGE AEGIS verifier host checks (no GPU) ===\n"); |
| 84 | |
| 85 | ForgeMachineDescriptor desc; |
| 86 | kat_descriptor(&desc); |
| 87 | uint8_t desc_digest[32]; |
| 88 | report("KAT_DESCRIPTOR_DIGEST", forge_descriptor_compute_digest(&desc, desc_digest) == 0); |
| 89 | |
| 90 | /* Non-uniform IR, so a misaligned partial match is not a match. */ |
| 91 | uint8_t ir[64]; |
| 92 | for (size_t i = 0; i < sizeof(ir); i++) ir[i] = (uint8_t)(i * 7 + 3); |
| 93 | |
| 94 | ForgeRealizationRequest req_a, req_b, req_c; |
| 95 | make_request(&req_a, &desc, desc_digest, 0x11, 4, ir, sizeof(ir)); |
| 96 | make_request(&req_b, &desc, desc_digest, 0x22, 4, ir, sizeof(ir)); /* other program */ |
| 97 | make_request(&req_c, &desc, desc_digest, 0x11, 5, ir, sizeof(ir)); /* other opcode count */ |
| 98 | |
| 99 | ForgeRealizationResult res_a; |
| 100 | report("REALIZE_OK", forge_realize(&req_a, &res_a) == FORGE_SEAM_OK && res_a.is_lowered); |
| 101 | |
| 102 | static ForgeVerifiedRealization verified; |
| 103 | report("VERIFY_OWN_REQUEST_ACCEPTED", |
| 104 | aegis_verify_realization(&req_a, &res_a, &desc, &verified) == FORGE_SEAM_OK && verified.verified); |
| 105 | |
| 106 | /* DRA-176: forge_realize only copies the IR into the code buffer. Observe that |
| 107 | * independently here, then require that such a copy never counts as a compiler |
| 108 | * (native-code) effect, whatever it is labelled and whether or not AEGIS verified it. */ |
| 109 | int ir_copy = res_a.machine_code != NULL && res_a.machine_code_size >= sizeof(ir) && |
| 110 | memcmp(res_a.machine_code, ir, sizeof(ir)) == 0; |
| 111 | for (size_t i = sizeof(ir); ir_copy && i < res_a.machine_code_size; i++) { |
| 112 | ir_copy = res_a.machine_code[i] == 0; |
| 113 | } |
| 114 | report("ORACLE_REALIZATION_IS_IR_COPY", ir_copy); |
| 115 | report("REALIZE_LABELS_SIMULATION", res_a.lowering_kind == FORGE_LOWERING_SIMULATED); |
| 116 | report("NEG_MEMCPY_REALIZATION_NOT_COMPILED", forge_realization_is_compiled(&req_a, &res_a) == 0); |
| 117 | report("NEG_VERIFIED_SIMULATION_NOT_COMPILED", |
| 118 | forge_realization_is_compiled(&req_a, &verified.realization) == 0); |
| 119 | |
| 120 | ForgeRealizationResult relabeled = res_a; |
| 121 | relabeled.lowering_kind = FORGE_LOWERING_COMPILED; |
| 122 | report("NEG_COPY_RELABELED_COMPILED_NOT_COMPILED", forge_realization_is_compiled(&req_a, &relabeled) == 0); |
| 123 | |
| 124 | ForgeRealizationResult unlabeled = res_a; |
| 125 | unlabeled.lowering_kind = FORGE_LOWERING_NONE; |
| 126 | report("NEG_UNLABELED_NOT_COMPILED", forge_realization_is_compiled(&req_a, &unlabeled) == 0); |
| 127 | report("NEG_COMPILED_QUERY_INVALID_ARGS", |
| 128 | forge_realization_is_compiled(NULL, &res_a) == 0 && forge_realization_is_compiled(&req_a, NULL) == 0); |
| 129 | |
| 130 | /* Sensitivity control: a buffer that is not a copy of the IR and carries the |
| 131 | * reserved COMPILED label passes the necessary conditions, so the refusals |
| 132 | * above are not a constant 0. No producer of such a buffer exists here. */ |
| 133 | static uint8_t other_code[512]; |
| 134 | memset(other_code, 0xc3, sizeof(other_code)); |
| 135 | ForgeRealizationResult noncopy = res_a; |
| 136 | noncopy.machine_code = other_code; |
| 137 | noncopy.machine_code_size = sizeof(other_code); |
| 138 | noncopy.lowering_kind = FORGE_LOWERING_COMPILED; |
| 139 | report("CONTROL_NONCOPY_COMPILED_LABEL_ACCEPTED", forge_realization_is_compiled(&req_a, &noncopy) == 1); |
| 140 | |
| 141 | /* The other clauses of the predicate, each against the non-copy buffer that |
| 142 | * the control above accepts, so each refusal is that clause's own. */ |
| 143 | ForgeRealizationResult not_lowered = noncopy; |
| 144 | not_lowered.is_lowered = false; |
| 145 | report("NEG_NOT_LOWERED_NOT_COMPILED", forge_realization_is_compiled(&req_a, ¬_lowered) == 0); |
| 146 | ForgeRealizationRequest empty_ir = req_a; |
| 147 | empty_ir.ir_size = 0; |
| 148 | report("NEG_EMPTY_IR_NOT_COMPILED", forge_realization_is_compiled(&empty_ir, &noncopy) == 0); |
| 149 | |
| 150 | /* Copies the zero-padding rule used to miss: the IR at an offset, and the IR |
| 151 | * followed by non-zero padding. Both are copies; neither may count. */ |
| 152 | static uint8_t at_offset[512]; |
| 153 | memset(at_offset, 0, sizeof(at_offset)); |
| 154 | memcpy(at_offset + 128, ir, sizeof(ir)); |
| 155 | ForgeRealizationResult offset_copy = noncopy; |
| 156 | offset_copy.machine_code = at_offset; |
| 157 | offset_copy.machine_code_size = sizeof(at_offset); |
| 158 | report("NEG_IR_AT_OFFSET_COMPILED_NOT_COMPILED", forge_realization_is_compiled(&req_a, &offset_copy) == 0); |
| 159 | static uint8_t nop_padded[512]; |
| 160 | memset(nop_padded, 0x90, sizeof(nop_padded)); |
| 161 | memcpy(nop_padded, ir, sizeof(ir)); |
| 162 | ForgeRealizationResult padded_copy = noncopy; |
| 163 | padded_copy.machine_code = nop_padded; |
| 164 | padded_copy.machine_code_size = sizeof(nop_padded); |
| 165 | report("NEG_IR_NONZERO_PAD_COMPILED_NOT_COMPILED", forge_realization_is_compiled(&req_a, &padded_copy) == 0); |
| 166 | |
| 167 | /* The IR copy ending exactly at the buffer end, and a code buffer that is a |
| 168 | * prefix of the IR: both are copies. */ |
| 169 | static uint8_t at_end[512]; |
| 170 | memset(at_end, 0, sizeof(at_end)); |
| 171 | memcpy(at_end + sizeof(at_end) - sizeof(ir), ir, sizeof(ir)); |
| 172 | ForgeRealizationResult end_copy = noncopy; |
| 173 | end_copy.machine_code = at_end; |
| 174 | end_copy.machine_code_size = sizeof(at_end); |
| 175 | report("NEG_IR_AT_END_COMPILED_NOT_COMPILED", forge_realization_is_compiled(&req_a, &end_copy) == 0); |
| 176 | static uint8_t ir_prefix[32]; |
| 177 | memcpy(ir_prefix, ir, sizeof(ir_prefix)); |
| 178 | ForgeRealizationResult prefix_copy = noncopy; |
| 179 | prefix_copy.machine_code = ir_prefix; |
| 180 | prefix_copy.machine_code_size = sizeof(ir_prefix); |
| 181 | report("NEG_IR_PREFIX_COMPILED_NOT_COMPILED", forge_realization_is_compiled(&req_a, &prefix_copy) == 0); |
| 182 | |
| 183 | /* AEGIS refuses to verify (and so to seal) a COMPILED label on an IR copy, and |
| 184 | * any label outside NONE/SIMULATED/COMPILED. */ |
| 185 | static ForgeVerifiedRealization sealed_label; |
| 186 | report("NEG_VERIFY_COPY_LABELED_COMPILED_REFUSED", |
| 187 | aegis_verify_realization(&req_a, &relabeled, &desc, &sealed_label) == FORGE_SEAM_ERR_VERIFY_FAILED && |
| 188 | !sealed_label.verified); |
| 189 | ForgeRealizationResult unknown_label = res_a; |
| 190 | unknown_label.lowering_kind = 7; |
| 191 | report("NEG_VERIFY_UNKNOWN_LABEL_REFUSED", |
| 192 | aegis_verify_realization(&req_a, &unknown_label, &desc, &sealed_label) == FORGE_SEAM_ERR_VERIFY_FAILED && |
| 193 | !sealed_label.verified); |
| 194 | |
| 195 | /* The predicate's label clause on its own: the non-copy buffer, which the |
| 196 | * control above accepts as COMPILED, answers 0 when labelled SIMULATED or NONE. */ |
| 197 | ForgeRealizationResult noncopy_sim = noncopy; |
| 198 | noncopy_sim.lowering_kind = FORGE_LOWERING_SIMULATED; |
| 199 | ForgeRealizationResult noncopy_none = noncopy; |
| 200 | noncopy_none.lowering_kind = FORGE_LOWERING_NONE; |
| 201 | report("NEG_NONCOPY_SIMULATED_OR_NONE_NOT_COMPILED", |
| 202 | forge_realization_is_compiled(&req_a, &noncopy_sim) == 0 && |
| 203 | forge_realization_is_compiled(&req_a, &noncopy_none) == 0); |
| 204 | |
| 205 | /* Positive control for invariant 9: a well-formed result over the non-copy |
| 206 | * buffer, labelled COMPILED, verifies and is sealed COMPILED. No producer of |
| 207 | * such a buffer exists; this only shows the branch is not a blanket refusal. */ |
| 208 | ForgeRealizationResult genuine = noncopy; |
| 209 | sha256_out(other_code, sizeof(other_code), genuine.code_digest); |
| 210 | uint8_t gid[96]; |
| 211 | memcpy(gid, genuine.request_digest, 32); |
| 212 | memcpy(gid + 32, genuine.target_descriptor_digest, 32); |
| 213 | memcpy(gid + 64, genuine.code_digest, 32); |
| 214 | sha256_out(gid, sizeof(gid), genuine.realization_id); |
| 215 | static ForgeVerifiedRealization sealed_genuine; |
| 216 | report("CONTROL_VERIFY_NONCOPY_COMPILED_SEALED_COMPILED", |
| 217 | aegis_verify_realization(&req_a, &genuine, &desc, &sealed_genuine) == FORGE_SEAM_OK && |
| 218 | sealed_genuine.verified && sealed_genuine.sealed_lowering_kind == FORGE_LOWERING_COMPILED); |
| 219 | |
| 220 | /* The IR is bound to the request: the copy labelled COMPILED, checked against a |
| 221 | * request equal in program digest, opcode count and target but carrying another |
| 222 | * IR (or a shorter IR size), is refused and nothing is sealed. */ |
| 223 | uint8_t ir_other[sizeof(ir)]; |
| 224 | for (size_t i = 0; i < sizeof(ir_other); i++) ir_other[i] = (uint8_t)(i * 13 + 5); |
| 225 | ForgeRealizationRequest req_other_ir = req_a; |
| 226 | req_other_ir.ir_payload = ir_other; |
| 227 | ForgeRealizationRequest req_short_ir = req_a; |
| 228 | req_short_ir.ir_size = sizeof(ir) - 1; |
| 229 | static ForgeVerifiedRealization sealed_other_ir; |
| 230 | int rc_oi = aegis_verify_realization(&req_other_ir, &relabeled, &desc, &sealed_other_ir); |
| 231 | int ok_oi = rc_oi == FORGE_SEAM_ERR_VERIFY_FAILED && !sealed_other_ir.verified; |
| 232 | int rc_si = aegis_verify_realization(&req_short_ir, &relabeled, &desc, &sealed_other_ir); |
| 233 | report("NEG_VERIFY_COPY_COMPILED_OTHER_IR_REFUSED", |
| 234 | ok_oi && rc_si == FORGE_SEAM_ERR_VERIFY_FAILED && !sealed_other_ir.verified); |
| 235 | |
| 236 | static ForgeVerifiedRealization replay; |
| 237 | int rc_b = aegis_verify_realization(&req_b, &res_a, &desc, &replay); |
| 238 | report("NEG_RESULT_REPLAYED_TO_OTHER_PROGRAM_REFUSED", rc_b == FORGE_SEAM_ERR_VERIFY_FAILED && !replay.verified); |
| 239 | int rc_c = aegis_verify_realization(&req_c, &res_a, &desc, &replay); |
| 240 | report("NEG_RESULT_REPLAYED_TO_OTHER_OPCODE_COUNT_REFUSED", rc_c == FORGE_SEAM_ERR_VERIFY_FAILED && !replay.verified); |
| 241 | |
| 242 | /* A result that names another target machine than its request. */ |
| 243 | ForgeRealizationRequest req_other_target = req_a; |
| 244 | req_other_target.target_descriptor_digest[0] ^= 0xff; |
| 245 | int rc_t = aegis_verify_realization(&req_other_target, &res_a, &desc, &replay); |
| 246 | report("NEG_REQUEST_FOR_OTHER_TARGET_REFUSED", rc_t != FORGE_SEAM_OK && !replay.verified); |
| 247 | |
| 248 | /* Existing invariants, now covered on the host. */ |
| 249 | uint8_t bad_code[FORGE_MAX_MACHINE_CODE]; |
| 250 | memcpy(bad_code, res_a.machine_code, res_a.machine_code_size); |
| 251 | bad_code[0] ^= 0xff; |
| 252 | ForgeRealizationResult corrupted = res_a; |
| 253 | corrupted.machine_code = bad_code; |
| 254 | report("NEG_CODE_DIGEST_MISMATCH_REFUSED", |
| 255 | aegis_verify_realization(&req_a, &corrupted, &desc, &replay) == FORGE_SEAM_ERR_VERIFY_FAILED); |
| 256 | |
| 257 | ForgeRealizationResult stale = res_a; |
| 258 | stale.realization_id[0] ^= 0xff; |
| 259 | report("NEG_STALE_IDENTITY_REFUSED", |
| 260 | aegis_verify_realization(&req_a, &stale, &desc, &replay) == FORGE_SEAM_ERR_STALE_IDENTITY); |
| 261 | |
| 262 | ForgeMachineDescriptor other_desc = desc; |
| 263 | other_desc.rm_sm_version = 0x0a00; |
| 264 | report("NEG_OTHER_DESCRIPTOR_REFUSED", |
| 265 | aegis_verify_realization(&req_a, &res_a, &other_desc, &replay) == FORGE_SEAM_ERR_DESC_MISMATCH); |
| 266 | |
| 267 | /* Submit-side refusals; all return before any NVRM call. */ |
| 268 | ForgeExecutionEvidence ev; |
| 269 | static ForgeVerifiedRealization unverified; |
| 270 | unverified = verified; |
| 271 | unverified.verified = false; |
| 272 | report("NEG_SUBMIT_UNVERIFIED_REFUSED", |
| 273 | forge_submit_realization(NULL, &unverified, &desc, &ev) == FORGE_SEAM_ERR_UNVERIFIED); |
| 274 | |
| 275 | static ForgeVerifiedRealization forged; |
| 276 | forged = verified; |
| 277 | forged.verification_token[0] ^= 0xff; |
| 278 | report("NEG_SUBMIT_BAD_TOKEN_REFUSED", |
| 279 | forge_submit_realization(NULL, &forged, &desc, &ev) == FORGE_SEAM_ERR_VERIFY_FAILED); |
| 280 | |
| 281 | uint8_t mutated_code[FORGE_MAX_MACHINE_CODE]; |
| 282 | memcpy(mutated_code, verified.sealed_code, verified.sealed_code_size); |
| 283 | mutated_code[0] ^= 0xff; |
| 284 | static ForgeVerifiedRealization mutated; |
| 285 | mutated = verified; |
| 286 | mutated.realization.machine_code = mutated_code; |
| 287 | report("NEG_SUBMIT_POST_VERIFY_MUTATION_REFUSED", |
| 288 | forge_submit_realization(NULL, &mutated, &desc, &ev) == FORGE_SEAM_ERR_CODE_MUTATED); |
| 289 | |
| 290 | report("NEG_SUBMIT_WITHOUT_NVRM_REFUSED", |
| 291 | forge_submit_realization(NULL, &verified, &desc, &ev) == FORGE_SEAM_ERR_NULL_NVRM); |
| 292 | |
| 293 | /* The evidence carries the sealed simulation label and says the code buffer |
| 294 | * was not executed; it is filled before the NVRM refusal above. */ |
| 295 | memset(&ev, 0xff, sizeof(ev)); |
| 296 | int rc_ev = forge_submit_realization(NULL, &verified, &desc, &ev); |
| 297 | report("EVIDENCE_LABELS_SIMULATION_NOT_EXECUTED", |
| 298 | rc_ev == FORGE_SEAM_ERR_NULL_NVRM && ev.lowering_kind == FORGE_LOWERING_SIMULATED && !ev.code_executed); |
| 299 | |
| 300 | /* Relabelling a verified simulation as COMPILED after verification is refused. */ |
| 301 | static ForgeVerifiedRealization relabeled_after; |
| 302 | relabeled_after = verified; |
| 303 | relabeled_after.realization.lowering_kind = FORGE_LOWERING_COMPILED; |
| 304 | report("NEG_SUBMIT_RELABELED_AFTER_VERIFY_REFUSED", |
| 305 | forge_submit_realization(NULL, &relabeled_after, &desc, &ev) == FORGE_SEAM_ERR_CODE_MUTATED); |
| 306 | |
| 307 | /* The evidence label is the sealed one, not a constant: a verified NONE-labelled |
| 308 | * realization's evidence says NONE. */ |
| 309 | static ForgeVerifiedRealization verified_none; |
| 310 | int rc_none = aegis_verify_realization(&req_a, &unlabeled, &desc, &verified_none); |
| 311 | memset(&ev, 0xff, sizeof(ev)); |
| 312 | int rc_ev_none = (rc_none == FORGE_SEAM_OK) ? forge_submit_realization(NULL, &verified_none, &desc, &ev) : rc_none; |
| 313 | report("EVIDENCE_CARRIES_SEALED_NONE_LABEL", |
| 314 | rc_ev_none == FORGE_SEAM_ERR_NULL_NVRM && ev.lowering_kind == FORGE_LOWERING_NONE && !ev.code_executed); |
| 315 | |
| 316 | /* Rewriting both the label and the sealed label breaks the token. */ |
| 317 | static ForgeVerifiedRealization both_relabeled; |
| 318 | both_relabeled = verified; |
| 319 | both_relabeled.realization.lowering_kind = FORGE_LOWERING_COMPILED; |
| 320 | both_relabeled.sealed_lowering_kind = FORGE_LOWERING_COMPILED; |
| 321 | report("NEG_SUBMIT_BOTH_LABELS_RELABELED_REFUSED", |
| 322 | forge_submit_realization(NULL, &both_relabeled, &desc, &ev) == FORGE_SEAM_ERR_VERIFY_FAILED); |
| 323 | |
| 324 | free(res_a.machine_code); |
| 325 | printf("FORGE AEGIS host results: TOTAL=%d PASSED=%d FAILED=%d\n", g_total, g_passed, g_failed); |
| 326 | return (g_failed == 0) ? 0 : 1; |
| 327 | } |