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/physics-mv/blob/main/forge/forge_realize.c # physics-mv FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence. 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 `physics-mv / forge / forge_realize.c` 384 lines ยท 16965 bytes Raw Blame History 1 #include "forge_realize.h" 2 #include "forge_descriptor.h" 3 #include "nvrm.h" 4 #include "m16_native.h" 5 6 #include 7 #include 8 #include 9 #include 10 11 void sha256_compute(const uint8_t *data, uint64_t len, uint8_t scratch_buf[128], uint32_t out_digest[8]); 12 13 static void sha256_bytes(const uint8_t *data, size_t len, uint8_t out[32]) { 14 uint8_t scratch[128]; 15 uint32_t words[8]; 16 sha256_compute(data, len, scratch, words); 17 for (int i = 0; i < 8; i++) { 18 out[i * 4 + 0] = (uint8_t)((words[i] >> 24) & 0xff); 19 out[i * 4 + 1] = (uint8_t)((words[i] >> 16) & 0xff); 20 out[i * 4 + 2] = (uint8_t)((words[i] >> 8) & 0xff); 21 out[i * 4 + 3] = (uint8_t)(words[i] & 0xff); 22 } 23 } 24 25 /* 26 * Domain-separation constant for the AEGIS verification token. It is compiled 27 * into public source, so it is not a secret: the token is an integrity check 28 * that a ForgeVerifiedRealization left aegis_verify_realization() unmodified, 29 * not authentication against a caller that can compute it. 30 */ 31 static const uint8_t AEGIS_TOKEN_DOMAIN[16] = { 32 0xae, 0x91, 0x50, 0x24, 0x76, 0x65, 0x72, 0x69, 33 0x66, 0x69, 0x65, 0x64, 0x63, 0x6f, 0x64, 0x65 34 }; 35 36 /* The sealed lowering label is part of the token input, so a verified 37 * realization whose label and sealed label are both rewritten no longer 38 * carries a matching token (DRA-176 review). */ 39 static void compute_verification_token(const uint8_t real_id[32], 40 const uint8_t desc_digest[32], 41 const uint8_t code_hash[32], 42 uint32_t lowering_kind, 43 uint8_t out_token[32]) { 44 uint8_t buf[16 + 32 + 32 + 32 + 4]; 45 memcpy(buf, AEGIS_TOKEN_DOMAIN, 16); 46 memcpy(buf + 16, real_id, 32); 47 memcpy(buf + 16 + 32, desc_digest, 32); 48 memcpy(buf + 16 + 64, code_hash, 32); 49 memcpy(buf + 16 + 96, &lowering_kind, 4); 50 sha256_bytes(buf, sizeof(buf), out_token); 51 } 52 53 /* SHA-256 over program_digest[32] | opcode_count(4, host order) | target_descriptor_digest[32] 54 * | ir_size(8, host order) | SHA-256(IR bytes)[32]. The IR is covered so a result realized 55 * from one IR never verifies against a request that carries another (DRA-176 review F). */ 56 static void compute_request_digest(const ForgeRealizationRequest *req, uint8_t out[32]) { 57 uint8_t req_buf[32 + 4 + 32 + 8 + 32]; 58 uint64_t ir_size = (uint64_t)req->ir_size; 59 memcpy(req_buf, req->program_digest, 32); 60 memcpy(req_buf + 32, &req->opcode_count, 4); 61 memcpy(req_buf + 36, req->target_descriptor_digest, 32); 62 memcpy(req_buf + 68, &ir_size, 8); 63 sha256_bytes(req->ir_payload ? req->ir_payload : req_buf, req->ir_payload ? req->ir_size : 0, req_buf + 76); 64 sha256_bytes(req_buf, sizeof(req_buf), out); 65 } 66 67 int forge_realize(const ForgeRealizationRequest *req, ForgeRealizationResult *out_res) { 68 if (!req || !out_res) return FORGE_SEAM_ERR_INVALID_ARG; 69 if (req->opcode_count == 0 || !req->ir_payload || req->ir_size == 0) { 70 return FORGE_SEAM_ERR_INVALID_ARG; 71 } 72 73 memset(out_res, 0, sizeof(*out_res)); 74 75 /* Verify that requested target descriptor matches target_descriptor_digest */ 76 uint8_t expected_desc_digest[32]; 77 if (forge_descriptor_compute_digest(&req->target_machine, expected_desc_digest) != 0) { 78 return FORGE_SEAM_ERR_DESC_MISMATCH; 79 } 80 if (memcmp(expected_desc_digest, req->target_descriptor_digest, 32) != 0) { 81 return FORGE_SEAM_ERR_DESC_MISMATCH; 82 } 83 memcpy(out_res->target_descriptor_digest, expected_desc_digest, 32); 84 85 /* Compute request digest over the request payload */ 86 compute_request_digest(req, out_res->request_digest); 87 88 /* Lowering simulation: allocate and align machine code */ 89 size_t code_len = (req->ir_size + 127ULL) & ~127ULL; 90 if (code_len < 512) code_len = 512; 91 uint8_t *code = (uint8_t *)calloc(1, code_len); 92 if (!code) return FORGE_SEAM_ERR_CAPACITY; 93 94 memcpy(code, req->ir_payload, (req->ir_size < code_len) ? req->ir_size : code_len); 95 out_res->machine_code = code; 96 out_res->machine_code_size = code_len; 97 out_res->gpr_count = 16; 98 out_res->shared_mem_bytes = 0; 99 out_res->is_lowered = true; 100 /* A copy of the IR is a simulation, never a compiler effect (DRA-176). */ 101 out_res->lowering_kind = FORGE_LOWERING_SIMULATED; 102 103 /* Compute code digest */ 104 sha256_bytes(out_res->machine_code, out_res->machine_code_size, out_res->code_digest); 105 106 /* Compute unique realization ID: SHA256(request_digest + target_descriptor_digest + code_digest) */ 107 uint8_t id_buf[32 * 3]; 108 memcpy(id_buf, out_res->request_digest, 32); 109 memcpy(id_buf + 32, out_res->target_descriptor_digest, 32); 110 memcpy(id_buf + 64, out_res->code_digest, 32); 111 sha256_bytes(id_buf, sizeof(id_buf), out_res->realization_id); 112 113 return FORGE_SEAM_OK; 114 } 115 116 /* True when the code buffer holds the IR bytes verbatim at any offset (padding 117 * of any value before or after), or is itself a prefix of the IR. A copy at an 118 * offset or with non-zero padding is still a copy (DRA-176 review). */ 119 static bool code_contains_ir(const uint8_t *code, size_t code_size, 120 const uint8_t *ir, size_t ir_size) { 121 if (ir_size >= code_size) return memcmp(code, ir, code_size) == 0; 122 for (size_t off = 0; off + ir_size <= code_size; off++) { 123 if (memcmp(code + off, ir, ir_size) == 0) return true; 124 } 125 return false; 126 } 127 128 int forge_realization_is_compiled(const ForgeRealizationRequest *req, 129 const ForgeRealizationResult *res) { 130 if (!req || !res || !req->ir_payload || req->ir_size == 0) return 0; 131 if (!res->is_lowered || !res->machine_code || res->machine_code_size == 0) return 0; 132 if (res->lowering_kind != FORGE_LOWERING_COMPILED) return 0; 133 if (code_contains_ir(res->machine_code, res->machine_code_size, 134 req->ir_payload, req->ir_size)) { 135 return 0; 136 } 137 return 1; 138 } 139 140 int aegis_verify_realization(const ForgeRealizationRequest *req, 141 const ForgeRealizationResult *res, 142 const ForgeMachineDescriptor *desc, 143 ForgeVerifiedRealization *out_verified) { 144 if (!req || !res || !desc || !out_verified) return FORGE_SEAM_ERR_INVALID_ARG; 145 memset(out_verified, 0, sizeof(*out_verified)); 146 147 /* Invariant 1: Realization must be lowered and have valid code */ 148 if (!res->is_lowered || !res->machine_code || res->machine_code_size == 0) { 149 return FORGE_SEAM_ERR_VERIFY_FAILED; 150 } 151 152 /* Invariant 2: Code size must be 128-byte aligned and within hardware limits */ 153 if ((res->machine_code_size & 127ULL) != 0 || res->machine_code_size > FORGE_MAX_MACHINE_CODE) { 154 return FORGE_SEAM_ERR_VERIFY_FAILED; 155 } 156 157 /* Invariant 3: GPR count must not exceed hardware limit (128 for Blackwell) */ 158 if (res->gpr_count > 128) { 159 return FORGE_SEAM_ERR_CAPACITY; 160 } 161 162 /* Invariant 4: Shared memory must not exceed block capacity */ 163 if (res->shared_mem_bytes > desc->shared_mem_per_block_bytes) { 164 return FORGE_SEAM_ERR_CAPACITY; 165 } 166 167 /* Invariant 5: Target descriptor digest must match the provided machine descriptor */ 168 uint8_t desc_digest[32]; 169 if (forge_descriptor_compute_digest(desc, desc_digest) != 0) { 170 return FORGE_SEAM_ERR_DESC_MISMATCH; 171 } 172 if (memcmp(desc_digest, res->target_descriptor_digest, 32) != 0) { 173 return FORGE_SEAM_ERR_DESC_MISMATCH; 174 } 175 176 /* Invariant 6: Recomputed code digest must match res->code_digest */ 177 uint8_t actual_code_digest[32]; 178 sha256_bytes(res->machine_code, res->machine_code_size, actual_code_digest); 179 if (memcmp(actual_code_digest, res->code_digest, 32) != 0) { 180 return FORGE_SEAM_ERR_VERIFY_FAILED; 181 } 182 183 /* Invariant 7: Realization identity must match SHA256(request_digest + desc_digest + code_digest) */ 184 uint8_t expected_real_id[32]; 185 uint8_t id_buf[32 * 3]; 186 memcpy(id_buf, res->request_digest, 32); 187 memcpy(id_buf + 32, res->target_descriptor_digest, 32); 188 memcpy(id_buf + 64, res->code_digest, 32); 189 sha256_bytes(id_buf, sizeof(id_buf), expected_real_id); 190 if (memcmp(expected_real_id, res->realization_id, 32) != 0) { 191 return FORGE_SEAM_ERR_STALE_IDENTITY; 192 } 193 194 /* Invariant 8: the result must belong to this request. Without this, a valid 195 * result realized for one request also verifies against any other request. */ 196 uint8_t expected_req_digest[32]; 197 compute_request_digest(req, expected_req_digest); 198 if (memcmp(expected_req_digest, res->request_digest, 32) != 0 || 199 memcmp(req->target_descriptor_digest, res->target_descriptor_digest, 32) != 0) { 200 return FORGE_SEAM_ERR_VERIFY_FAILED; 201 } 202 203 /* Invariant 9 (DRA-176): the lowering label is a known value, and a realization 204 * labelled COMPILED must pass forge_realization_is_compiled() for this request. 205 * AEGIS never seals a COMPILED label on a copy of the IR. */ 206 if (res->lowering_kind > FORGE_LOWERING_COMPILED) { 207 return FORGE_SEAM_ERR_VERIFY_FAILED; 208 } 209 if (res->lowering_kind == FORGE_LOWERING_COMPILED && forge_realization_is_compiled(req, res) != 1) { 210 return FORGE_SEAM_ERR_VERIFY_FAILED; 211 } 212 213 /* All AEGIS contract invariants pass: seal into immutable ForgeVerifiedRealization */ 214 out_verified->realization = *res; 215 memcpy(out_verified->sealed_code, res->machine_code, res->machine_code_size); 216 out_verified->sealed_code_size = res->machine_code_size; 217 memcpy(out_verified->sealed_code_hash, actual_code_digest, 32); 218 memcpy(out_verified->verified_descriptor_digest, desc_digest, 32); 219 out_verified->sealed_lowering_kind = res->lowering_kind; 220 compute_verification_token(res->realization_id, desc_digest, actual_code_digest, 221 out_verified->sealed_lowering_kind, out_verified->verification_token); 222 out_verified->verified = true; 223 224 return FORGE_SEAM_OK; 225 } 226 227 int forge_submit_realization(Nvrm *rm, 228 const ForgeVerifiedRealization *verified_real, 229 const ForgeMachineDescriptor *live_desc, 230 ForgeExecutionEvidence *out_evidence) { 231 if (!verified_real || !live_desc) return FORGE_SEAM_ERR_INVALID_ARG; 232 if (out_evidence) memset(out_evidence, 0, sizeof(*out_evidence)); 233 234 /* 1. Compile-time and runtime check: unverified realization cannot be submitted */ 235 if (!verified_real->verified) { 236 return FORGE_SEAM_ERR_UNVERIFIED; 237 } 238 239 /* 2. Verification token must match (integrity check over ID + descriptor + code 240 * + sealed lowering label; its key is public, see AEGIS_TOKEN_DOMAIN) */ 241 uint8_t expected_token[32]; 242 compute_verification_token(verified_real->realization.realization_id, 243 verified_real->verified_descriptor_digest, 244 verified_real->sealed_code_hash, 245 verified_real->sealed_lowering_kind, 246 expected_token); 247 if (memcmp(expected_token, verified_real->verification_token, 32) != 0) { 248 return FORGE_SEAM_ERR_VERIFY_FAILED; 249 } 250 251 /* 3. Live machine descriptor must match verified descriptor */ 252 uint8_t live_digest[32]; 253 if (forge_descriptor_compute_digest(live_desc, live_digest) != 0) { 254 return FORGE_SEAM_ERR_DESC_MISMATCH; 255 } 256 if (memcmp(live_digest, verified_real->verified_descriptor_digest, 32) != 0 || 257 memcmp(live_digest, verified_real->realization.target_descriptor_digest, 32) != 0) { 258 return FORGE_SEAM_ERR_DESC_MISMATCH; 259 } 260 261 /* 4. Post-verification code mutation prevention: 262 * Recompute hash of current machine code and compare with sealed hash and sealed bytes. */ 263 if (!verified_real->realization.machine_code || 264 verified_real->realization.machine_code_size != verified_real->sealed_code_size) { 265 return FORGE_SEAM_ERR_CODE_MUTATED; 266 } 267 uint8_t current_code_hash[32]; 268 sha256_bytes(verified_real->realization.machine_code, 269 verified_real->realization.machine_code_size, 270 current_code_hash); 271 if (memcmp(current_code_hash, verified_real->sealed_code_hash, 32) != 0 || 272 memcmp(verified_real->realization.machine_code, 273 verified_real->sealed_code, 274 verified_real->sealed_code_size) != 0) { 275 return FORGE_SEAM_ERR_CODE_MUTATED; 276 } 277 /* 4b. The lowering label is sealed too: relabelling a verified simulation 278 * as COMPILED after verification is a mutation (DRA-176). */ 279 if (verified_real->realization.lowering_kind != verified_real->sealed_lowering_kind) { 280 return FORGE_SEAM_ERR_CODE_MUTATED; 281 } 282 283 /* 5. Stale realization identity check: verify freshness against constituent digests */ 284 uint8_t expected_id[32]; 285 uint8_t id_buf[32 * 3]; 286 memcpy(id_buf, verified_real->realization.request_digest, 32); 287 memcpy(id_buf + 32, verified_real->realization.target_descriptor_digest, 32); 288 memcpy(id_buf + 64, verified_real->sealed_code_hash, 32); 289 sha256_bytes(id_buf, sizeof(id_buf), expected_id); 290 if (memcmp(expected_id, verified_real->realization.realization_id, 32) != 0) { 291 return FORGE_SEAM_ERR_STALE_IDENTITY; 292 } 293 294 /* The evidence says what was submitted before any hardware is touched: the 295 * sealed label, and that the pushbuffer below never executes the code 296 * buffer (it is a semaphore release only). */ 297 if (out_evidence) { 298 out_evidence->lowering_kind = verified_real->sealed_lowering_kind; 299 out_evidence->code_executed = false; 300 } 301 302 /* 6. Physical submission requires live Nvrm context */ 303 if (!rm) { 304 return FORGE_SEAM_ERR_NULL_NVRM; 305 } 306 if (rm->compute_class != live_desc->rm_compute_class) { 307 return FORGE_SEAM_ERR_NVRM; 308 } 309 310 /* 7. Perform REAL physical submission on GB10 silicon via NVRM / M16 GPFIFO channel */ 311 if (rm->chgroup == 0) { 312 if (nvrm_channel(rm) != 0) return FORGE_SEAM_ERR_NVRM; 313 } 314 315 NvrmMem code_mem, marker_mem, pb_mem; 316 /* Sized from the sealed code (up to FORGE_MAX_MACHINE_CODE), rounded to the page, 317 * so a code buffer larger than one page never overruns the mapping. */ 318 uint64_t code_alloc = ((uint64_t)verified_real->sealed_code_size + 0xfffULL) & ~0xfffULL; 319 if (nvrm_alloc(rm, code_alloc, &code_mem) != 0) return FORGE_SEAM_ERR_NVRM; 320 if (nvrm_alloc(rm, 0x1000, &marker_mem) != 0) { 321 nvrm_free(rm, &code_mem); 322 return FORGE_SEAM_ERR_NVRM; 323 } 324 if (nvrm_alloc(rm, 0x1000, &pb_mem) != 0) { 325 nvrm_free(rm, &code_mem); 326 nvrm_free(rm, &marker_mem); 327 return FORGE_SEAM_ERR_NVRM; 328 } 329 330 /* Copy immutable sealed code into GPU-visible memory */ 331 memcpy(code_mem.cpu, verified_real->sealed_code, verified_real->sealed_code_size); 332 333 volatile uint32_t *hmarker = (volatile uint32_t *)marker_mem.cpu; 334 *hmarker = 0; 335 __asm__ volatile("dsb sy" ::: "memory"); 336 337 const uint32_t COMPLETION_PAYLOAD = 0x44444444U; 338 uint32_t pb[64]; 339 int n = m16_native_build_release(pb, marker_mem.va, COMPLETION_PAYLOAD, live_desc->rm_compute_class); 340 if (n <= 0) { 341 nvrm_free(rm, &code_mem); 342 nvrm_free(rm, &marker_mem); 343 nvrm_free(rm, &pb_mem); 344 return FORGE_SEAM_ERR_SUBMIT_FAILED; 345 } 346 347 memcpy(pb_mem.cpu, pb, (size_t)n * sizeof(uint32_t)); 348 __asm__ volatile("dsb sy" ::: "memory"); 349 350 struct timespec ts0, ts1; 351 clock_gettime(CLOCK_MONOTONIC, &ts0); 352 353 if (nvrm_enqueue(rm, &pb_mem, 0, (uint32_t)n) != 0) { 354 nvrm_free(rm, &code_mem); 355 nvrm_free(rm, &marker_mem); 356 nvrm_free(rm, &pb_mem); 357 return FORGE_SEAM_ERR_SUBMIT_FAILED; 358 } 359 nvrm_ring(rm); 360 361 /* Wait for physical hardware completion marker (up to 5000 ms) */ 362 int wait_rc = m16_native_wait_marker(hmarker, COMPLETION_PAYLOAD, 5000); 363 clock_gettime(CLOCK_MONOTONIC, &ts1); 364 365 uint64_t dur_ns = (uint64_t)(ts1.tv_sec - ts0.tv_sec) * 1000000000ULL + 366 (uint64_t)(ts1.tv_nsec - ts0.tv_nsec); 367 368 bool hw_success = (wait_rc == 0 && *hmarker == COMPLETION_PAYLOAD); 369 370 if (out_evidence) { 371 memcpy(out_evidence->descriptor_digest, live_digest, 32); 372 memcpy(out_evidence->code_digest, verified_real->sealed_code_hash, 32); 373 memcpy(out_evidence->realization_id, verified_real->realization.realization_id, 32); 374 out_evidence->submission_marker_payload = *hmarker; 375 out_evidence->execution_duration_ns = dur_ns; 376 out_evidence->hardware_success = hw_success; 377 } 378 379 nvrm_free(rm, &code_mem); 380 nvrm_free(rm, &marker_mem); 381 nvrm_free(rm, &pb_mem); 382 383 return hw_success ? FORGE_SEAM_OK : FORGE_SEAM_ERR_SUBMIT_FAILED; 384 }