interplane.Get Omega
Read in your language

physics-mv

FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence.

Brought by drakestapleton

branch main Branches Tags History

physics-mv / tests / test_forge_seam.c

187 lines · 7408 bytes Raw Blame History
1#include "forge_descriptor.h"
2#include "forge_realize.h"
3#include "forge_types.h"
4#include "nvrm.h"
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9
10static int g_total = 0, g_passed = 0, g_failed = 0;
11
12static void report(const char *name, int ok) {
13 g_total++;
14 if (ok) {
15 g_passed++;
16 printf("[PASS] %s\n", name);
17 } else {
18 g_failed++;
19 printf("[FAIL] %s\n", name);
20 }
21}
22
23static const char *get_base_commit(char *buf, size_t sz) {
24 FILE *fp = popen("git merge-base HEAD origin/main 2>/dev/null || git merge-base HEAD main 2>/dev/null || echo origin/main", "r");
25 if (!fp) return "origin/main";
26 if (!fgets(buf, sz, fp)) {
27 pclose(fp);
28 return "origin/main";
29 }
30 pclose(fp);
31 char *nl = strchr(buf, '\n');
32 if (nl) *nl = '\0';
33 return buf[0] ? buf : "origin/main";
34}
35
36static int check_git_diff_no_renames_or_deletions(void) {
37 char base[128];
38 char cmd[256];
39 snprintf(cmd, sizeof(cmd), "git diff --name-status %s", get_base_commit(base, sizeof(base)));
40 FILE *fp = popen(cmd, "r");
41 if (!fp) return 0;
42 char line[512];
43 int ok = 1;
44 while (fgets(line, sizeof(line), fp)) {
45 char status = line[0];
46 if (status == 'D' || status == 'R') {
47 fprintf(stderr, "[-] Forbidden git status '%c': %s", status, line);
48 ok = 0;
49 }
50 }
51 pclose(fp);
52 return ok;
53}
54
55static int check_historical_files_unmodified(void) {
56 char base[128];
57 char cmd[512];
58 snprintf(cmd, sizeof(cmd), "git diff --exit-code %s -- physics.bin physics.audit physics.s nvrm/ m16/", get_base_commit(base, sizeof(base)));
59 FILE *fp = popen(cmd, "r");
60 if (!fp) return 0;
61 int rc = pclose(fp);
62 return (rc == 0);
63}
64
65int main(void) {
66 printf("============================================================\n");
67 printf(" GATE 3: FORGE-0 SEAM QUALIFICATION SUITE \n");
68 printf("============================================================\n\n");
69
70 /* 1. Hardware probe to get target descriptor */
71 ForgeMachineDescriptor desc;
72 if (forge_probe_hardware(&desc) != 0) {
73 fprintf(stderr, "FATAL: hardware probe failed\n");
74 return 1;
75 }
76 uint8_t desc_digest[32];
77 forge_descriptor_compute_digest(&desc, desc_digest);
78
79 /* 2. Build Omega RealizationRequest */
80 uint8_t dummy_ir[64];
81 memset(dummy_ir, 0x5a, sizeof(dummy_ir));
82
83 ForgeRealizationRequest req;
84 memset(&req, 0, sizeof(req));
85 memset(req.program_digest, 0x11, 32);
86 req.opcode_count = 4;
87 req.ir_payload = dummy_ir;
88 req.ir_size = sizeof(dummy_ir);
89 req.target_machine = desc;
90 memcpy(req.target_descriptor_digest, desc_digest, 32);
91
92 /* 3. FORGE_REALIZE_API_NAMED */
93 ForgeRealizationResult res;
94 int realize_rc = forge_realize(&req, &res);
95 report("FORGE_REALIZE_API_NAMED", realize_rc == FORGE_SEAM_OK && res.is_lowered);
96
97 /* 4. AEGIS_VERIFY_API_NAMED */
98 ForgeVerifiedRealization verified_real;
99 int verify_rc = aegis_verify_realization(&req, &res, &desc, &verified_real);
100 report("AEGIS_VERIFY_API_NAMED", verify_rc == FORGE_SEAM_OK && verified_real.verified);
101
102 /* Open real physical Nvrm GPU substrate */
103 Nvrm rm;
104 memset(&rm, 0, sizeof(rm));
105 int rm_rc = nvrm_open(&rm);
106 if (rm_rc != 0) {
107 fprintf(stderr, "FATAL: nvrm_open failed: %d\n", rm_rc);
108 return 1;
109 }
110
111 /* 5. FORGE_TYPED_BOUNDARY_ENFORCED (real native GB10 physical submission) */
112 ForgeExecutionEvidence evidence;
113 memset(&evidence, 0, sizeof(evidence));
114 int submit_rc = forge_submit_realization(&rm, &verified_real, &desc, &evidence);
115 int hw_ok = (submit_rc == FORGE_SEAM_OK) &&
116 evidence.hardware_success &&
117 (evidence.submission_marker_payload == 0x44444444U) &&
118 (evidence.execution_duration_ns > 0) &&
119 (memcmp(evidence.descriptor_digest, desc_digest, 32) == 0) &&
120 (memcmp(evidence.code_digest, res.code_digest, 32) == 0) &&
121 /* DRA-176: the GPU wrote a completion marker; it did not run the
122 * code buffer, and the code is a labelled simulation. */
123 (evidence.lowering_kind == FORGE_LOWERING_SIMULATED) &&
124 !evidence.code_executed;
125 report("FORGE_TYPED_BOUNDARY_ENFORCED", hw_ok);
126
127 /* 6. HISTORICAL_NAMES_PRESERVED */
128 int hist_ok = check_historical_files_unmodified();
129 report("HISTORICAL_NAMES_PRESERVED", hist_ok);
130
131 /* 7. NO_EXISTING_FILE_RENAMED */
132 int no_renames_ok = check_git_diff_no_renames_or_deletions();
133 report("NO_EXISTING_FILE_RENAMED", no_renames_ok);
134
135 /* --- NEGATIVE TESTS --- */
136 printf("\n[*] Running Gate 3 Negative Tests...\n");
137
138 /* Neg 0: NULL Nvrm rejected */
139 ForgeExecutionEvidence neg_ev;
140 int neg_null_rm = forge_submit_realization(NULL, &verified_real, &desc, &neg_ev);
141 report("NEG_NULL_NVRM_REJECTED", neg_null_rm == FORGE_SEAM_ERR_NULL_NVRM);
142
143 /* Neg 1: Unverified realization cannot be submitted */
144 ForgeVerifiedRealization unverified = verified_real;
145 unverified.verified = false;
146 int neg_unverified_rc = forge_submit_realization(&rm, &unverified, &desc, &neg_ev);
147 report("NEG_UNVERIFIED_REALIZATION_REJECTED", neg_unverified_rc == FORGE_SEAM_ERR_UNVERIFIED);
148
149 /* Neg 2: Descriptor mismatch fails submission */
150 ForgeMachineDescriptor mismatched_desc = desc;
151 mismatched_desc.rm_sm_version = 0x0a00; /* altered SM version */
152 int neg_desc_rc = forge_submit_realization(&rm, &verified_real, &mismatched_desc, &neg_ev);
153 report("NEG_DESCRIPTOR_MISMATCH_REJECTED", neg_desc_rc == FORGE_SEAM_ERR_DESC_MISMATCH);
154
155 /* Neg 3: Stale realization identity fails submission */
156 ForgeVerifiedRealization stale_real = verified_real;
157 stale_real.realization.realization_id[0] ^= 0xff; /* corrupted identity */
158 int neg_stale_rc = forge_submit_realization(&rm, &stale_real, &desc, &neg_ev);
159 report("NEG_STALE_REALIZATION_IDENTITY_REJECTED",
160 neg_stale_rc == FORGE_SEAM_ERR_STALE_IDENTITY || neg_stale_rc == FORGE_SEAM_ERR_VERIFY_FAILED);
161
162 /* Neg 4: Verification failure prevents hardware submission */
163 uint8_t bad_code[1024];
164 memcpy(bad_code, res.machine_code, res.machine_code_size);
165 bad_code[0] ^= 0xff;
166 ForgeRealizationResult corrupted_res = res;
167 corrupted_res.machine_code = bad_code;
168 ForgeVerifiedRealization bad_verified;
169 int bad_verify_rc = aegis_verify_realization(&req, &corrupted_res, &desc, &bad_verified);
170 report("NEG_VERIFICATION_FAILURE_PREVENTS_SUBMISSION", bad_verify_rc != FORGE_SEAM_OK);
171
172 /* Neg 5: Post-verification machine code mutation MUST REFUSE submission */
173 uint8_t mutated_code[1024];
174 memcpy(mutated_code, verified_real.sealed_code, verified_real.sealed_code_size);
175 mutated_code[0] ^= 0xff;
176 ForgeVerifiedRealization mutated_verified = verified_real;
177 mutated_verified.realization.machine_code = mutated_code;
178 int neg_mutate_rc = forge_submit_realization(&rm, &mutated_verified, &desc, &neg_ev);
179 report("NEG_POST_VERIFICATION_MUTATION_REFUSED", neg_mutate_rc == FORGE_SEAM_ERR_CODE_MUTATED);
180
181 /* Clean up */
182 nvrm_close(&rm);
183 if (res.machine_code) free(res.machine_code);
184
185 printf("\nGate 3 Results: TOTAL=%d PASSED=%d FAILED=%d\n", g_total, g_passed, g_failed);
186 return (g_failed == 0) ? 0 : 1;
187}