| -- Copyright 2026 The Fuchsia Authors | |
| -- | |
| -- Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 | |
| -- <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT | |
| -- license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. | |
| -- This file may not be copied, modified, or distributed except according to | |
| -- those terms. | |
| -- anneal-trust-tier: candidate:refinement | |
| module | |
| prelude | |
| public import Init.NotationExtra | |
| public import Init.Data.List.Basic | |
| public import Trusted.CppAst | |
| public import Trusted.CppSemantics.State | |
| public import Trusted.CppSemantics.Memory | |
| public import Trusted.CppSemantics.Eval | |
| @[expose] public section | |
| namespace Trusted.CppSemantics | |
| open Trusted.CppAst | |
| /-! | |
| # Rung-0 C11 Small-Step Transition and Execution Predicates | |
| (`Trusted.CppSemantics.Step`) | |
| Deterministic tactic-free small-step semantics (`initCppState`, `evalStep`) | |
| and unbounded execution predicates (`CppReaches`, `CppReachesResult`, | |
| `CppWellFormed`, `CppBehavior`, `CppDiverges`, `CppFaultsUB`) for Rung-0 C11. | |
| -/ | |
| /-- Construct initial local slots for a function frame: | |
| - Local `0` (return slot): `.val .void` when `ret = .void`, else `.uninit`. | |
| - Locals `1..=K` (parameters): `.val arg_i`. | |
| - Locals `K+1..` (user locals and SSENF temporaries): `.uninit` (reading before | |
| initialization faults `.undefined`). -/ | |
| noncomputable def initLocalSlotsAux (args : List CppVal) : | |
| List CppLocalDecl → List CppLocalSlot | |
| | [] => [] | |
| | _ :: rest => | |
| match args with | |
| | v :: vs => .val v :: initLocalSlotsAux vs rest | |
| | [] => .uninit :: initLocalSlotsAux [] rest | |
| /-- Build the initial local slot array for `body` given argument values `args`. | |
| -/ | |
| noncomputable def initLocalSlots (body : CppBody) (args : List CppVal) : | |
| List CppLocalSlot := | |
| match body.locals with | |
| | [] => [] | |
| | retDecl :: rest => | |
| let retSlot := match retDecl.ty with | |
| | .void => CppLocalSlot.val .void | |
| | _ => CppLocalSlot.uninit | |
| retSlot :: initLocalSlotsAux args rest | |
| /-- C++ reference (and `this`) parameters flagged in `paramNonNull` must be | |
| bound to an object (C++20 [dcl.ref]p5): the pointer argument must point to a | |
| suitably aligned `pointee` lying entirely inside a live allocation. -/ | |
| noncomputable def refArgsOk (policy : CppLangPolicy) | |
| (records : List CppRecordDef) (mem : CppMemory) : | |
| List Bool → List CppTy → List CppVal → Bool | |
| | true :: fs, t :: ts, v :: vs => | |
| (match t, v with | |
| | .ptr pointee _ _ _, .ptr (some aid) addr sr => | |
| match cppTySize? policy records pointee, | |
| cppTyAlign? policy records pointee with | |
| | some sz, some al => | |
| placeInBounds mem (some aid) addr sz && subRangeContains sr addr sz && | |
| ptrTargetAligned mem (some aid) addr al | |
| | _, _ => false | |
| | _, _ => false) && | |
| refArgsOk policy records mem fs ts vs | |
| | false :: fs, _ :: ts, _ :: vs => refArgsOk policy records mem fs ts vs | |
| | [], _, _ => true | |
| | _ :: _, _, _ => false | |
| /-- Check that `locals` match `sig.params` starting at local `1`. -/ | |
| noncomputable def paramLocalsMatch : | |
| List CppLocalDecl → List CppTy → Bool | |
| | _, [] => true | |
| | [], _ :: _ => false | |
| | d :: ds, t :: ts => | |
| d.isParam && CppTy.beq d.ty t && paramLocalsMatch ds ts | |
| /-- Check that `b.locals[0]` is a non-parameter return slot of type `b.sig.ret` | |
| and `b.locals[1..=K]` are parameter slots matching `b.sig.params`. -/ | |
| noncomputable def frameLocalsMatchSig (b : CppBody) : Bool := | |
| match b.locals with | |
| | [] => false | |
| | retDecl :: rest => | |
| !retDecl.isParam && CppTy.beq retDecl.ty b.sig.ret && | |
| paramLocalsMatch rest b.sig.params | |
| /-- `true` iff `retTy` is `.void` or has a valid size and alignment under | |
| `policy`. -/ | |
| noncomputable def retSized (policy : CppLangPolicy) | |
| (records : List CppRecordDef) (retTy : CppTy) : Bool := | |
| match retTy with | |
| | .void => true | |
| | _ => | |
| match cppTySize? policy records retTy, | |
| cppTyAlign? policy records retTy with | |
| | some _, some _ => true | |
| | _, _ => false | |
| /-- `true` iff every non-return local declaration in `ds` has a valid size and | |
| alignment under `policy` (`< maxObjSizeBytes policy`, non-`.void`, non-ZST, | |
| known record). -/ | |
| noncomputable def nonRetLocalsSized (policy : CppLangPolicy) | |
| (records : List CppRecordDef) : List CppLocalDecl → Bool | |
| | [] => true | |
| | d :: ds => | |
| (match cppTySize? policy records d.ty, | |
| cppTyAlign? policy records d.ty with | |
| | some _, some _ => true | |
| | _, _ => false) && nonRetLocalsSized policy records ds | |
| /-- `true` iff `b.locals[0]` is `.void` or sized/aligned, and every local | |
| `b.locals[1..]` is sized/aligned (`< maxObjSizeBytes policy`). -/ | |
| noncomputable def bodyLocalsSized (policy : CppLangPolicy) | |
| (records : List CppRecordDef) (b : CppBody) : Bool := | |
| match b.locals with | |
| | [] => false | |
| | retDecl :: rest => | |
| retSized policy records retDecl.ty && | |
| nonRetLocalsSized policy records rest | |
| /-- `true` iff every call in `blocks` stores its result, if any, into a whole | |
| local (C11 §6.5.16p3 leaves a projected destination unsequenced with the | |
| call). -/ | |
| noncomputable def cppBlocksAdmitted : List CppBlock → Bool | |
| | [] => true | |
| | b :: rest => | |
| (match b.term with | |
| | .call _ _ (some (p, _)) _ => p.projs.isEmpty | |
| | _ => true) && cppBlocksAdmitted rest | |
| /-- Shared non-external body structural, type, and layout guard enforced at | |
| both entry (`initCppState`), intra-program `.call`, and `cppBodyAdmitted`. -/ | |
| noncomputable def bodyRuntimeAdmitted (prog : CppProgram) | |
| (b : CppBody) : Bool := | |
| let locTys := b.locals.map CppLocalDecl.ty | |
| !b.isExternal && !(b.identity == "") && | |
| CppTy.noRestrictList b.sig.params && CppTy.noRestrict b.sig.ret && | |
| CppTy.noRestrictList locTys && | |
| CppTy.nonZeroArrayList b.sig.params && CppTy.nonZeroArray b.sig.ret && | |
| CppTy.nonZeroArrayList locTys && | |
| Nat.ble b.sig.paramNonNull.length b.sig.params.length && | |
| retSized prog.policy prog.records b.sig.ret && | |
| !b.blocks.isEmpty && | |
| Nat.blt b.entryBlock b.blocks.length && | |
| cppBlocksAdmitted b.blocks && | |
| frameLocalsMatchSig b && | |
| bodyLocalsSized prog.policy prog.records b | |
| /-- Initialize execution state for `prog` at `prog.entryDefId` on query `q`. -/ | |
| noncomputable def initCppState (prog : CppProgram) (q : CppQuery) : | |
| Except CppFaultKind CppState := | |
| if !cppPolicyAdmitted prog.policy then | |
| .error .unsupported | |
| else if !validCppMemory prog.policy q.mem || | |
| !cppRecordsValid prog.policy prog.records then | |
| .error .unsupported | |
| else | |
| match cppGetElem? prog.bodies prog.entryDefId with | |
| | none => .error .unsupported | |
| | some body => | |
| if !bodyRuntimeAdmitted prog body then | |
| .error .unsupported | |
| else if !checkCppArgs prog.policy prog.records q.args body.sig.params || | |
| !argsLiveInMem prog.policy prog.records q.mem q.args | |
| body.sig.params then | |
| .error .unsupported | |
| else if !refArgsOk prog.policy prog.records q.mem body.sig.paramNonNull | |
| body.sig.params q.args then | |
| .error .unsupported | |
| else | |
| let frame : CppFrame := { | |
| defId := prog.entryDefId | |
| blockId := body.entryBlock | |
| stmtIdx := 0 | |
| locals := initLocalSlots body q.args | |
| retDst := none | |
| retBlock := 0 | |
| stackAllocs := [] | |
| } | |
| .ok { frames := [frame], mem := q.mem } | |
| /-- `true` iff `place` designates memory: it has projections, or its local | |
| has been moved to the stack. -/ | |
| noncomputable def placeInMemory (frame : CppFrame) (place : CppPlace) : Bool := | |
| match place.projs with | |
| | _ :: _ => true | |
| | [] => | |
| match cppGetElem? frame.locals place.base with | |
| | some (.stackAddr _) => true | |
| | _ => false | |
| /-- Resolve an in-memory place `place` of type `ty` to its | |
| `(allocId, bitLo, bitLen)` span for C11 §6.5.16.1p3 overlap checking across | |
| scalars, bit-fields, arrays, and records. -/ | |
| noncomputable def resolvePlaceBitSpan (prog : CppProgram) (body : CppBody) | |
| (frame : CppFrame) (mem : CppMemory) (place : CppPlace) (ty : CppTy) : | |
| Except CppFaultKind (Option Nat × Nat × Nat × CppFrame × CppMemory) := | |
| match splitBitfield place.projs with | |
| | some (pre, rid, idx, o, w, sgn) => | |
| match resolvePlaceAddr prog body frame mem false | |
| { place with projs := pre } with | |
| | .error k => .error k | |
| | .ok (r, f', m') => | |
| if !bitfieldOk prog r rid idx o w sgn ty then .error .unsupported | |
| else if !bitfieldPlaced prog m' r rid then .error .undefined | |
| else .ok (r.allocId, r.addr * 8 + o, w, f', m') | |
| | none => | |
| match resolvePlaceAddr prog body frame mem false place, | |
| cppTySize? prog.policy prog.records ty with | |
| | .error k, _ => .error k | |
| | .ok (r, f', m'), some sz => | |
| if !CppTy.beq r.ty ty then .error .unsupported | |
| else .ok (r.allocId, r.addr * 8, sz * 8, f', m') | |
| | .ok _, none => .error .unsupported | |
| /-- Peel zero or more scalar, pointer, or object-representation conversions | |
| (`.intCast`, `.ptrCast`, `.intToBool`, `.boolToInt`, `.bitCast`) around a | |
| `.readPlace src srcTy` RHS, returning `some (src, srcTy, hadCast)` (`none` on | |
| arithmetic, pointer-offset, address-of, or literal expressions). -/ | |
| noncomputable def peelCastReadPlace? : | |
| CppExpr → Option (CppPlace × CppTy × Bool) | |
| | .readPlace src srcTy => some (src, srcTy, false) | |
| | .intCast _ _ _ _ _ arg | |
| | .ptrCast _ arg | |
| | .intToBool arg | |
| | .boolToInt _ _ arg | |
| | .bitCast _ _ arg => | |
| match peelCastReadPlace? arg with | |
| | some (src, srcTy, _) => some (src, srcTy, true) | |
| | none => none | |
| | .constInt _ _ _ _ | .constBool _ | .nullPtr _ | .addrOf _ _ | |
| | .unary _ _ _ | .binary _ _ _ _ | .ptrAdd _ _ _ _ => none | |
| /-- Guard for an assignment `dst = rhs` (C11 §6.5.16.1p3, C++20 [expr.ass]p8): | |
| when `rhs` reads `src` (directly or through conversions) and both sides are in | |
| memory (or share a projected local), their accessed bit spans must either be | |
| disjoint or coincide at identical types without conversion; unconverted partial | |
| overlap is `.undefined`, while converted or type-mismatched overlap is | |
| `.unsupported` (UB when implicit in C11 §6.5.16.1p3 / C++20 [expr.ass]p8, | |
| defined for explicit casts or prvalue-returning `std::bit_cast`; nominal | |
| same-width/sign rank aliases such as `long` vs. `long long` are covered by | |
| `clang_ast_lowering` disclosure). -/ | |
| noncomputable def assignOverlapCheck (prog : CppProgram) (body : CppBody) | |
| (frame : CppFrame) (mem : CppMemory) (dst : CppPlace) (dstTy : CppTy) | |
| (rhs : CppExpr) : Except CppFaultKind (CppFrame × CppMemory) := | |
| match peelCastReadPlace? rhs with | |
| | some (src, srcTy, hadCast) => | |
| if (placeInMemory frame src && placeInMemory frame dst) || | |
| (Nat.beq src.base dst.base && | |
| (!src.projs.isEmpty || !dst.projs.isEmpty)) then | |
| match resolvePlaceBitSpan prog body frame mem dst dstTy with | |
| | .error k => .error k | |
| | .ok (aidD, loD, lenD, f2, m2) => | |
| match resolvePlaceBitSpan prog body f2 m2 src srcTy with | |
| | .error k => .error k | |
| | .ok (aidS, loS, lenS, f3, m3) => | |
| match aidS, aidD with | |
| | some a, some b => | |
| if !Nat.beq a b || Nat.ble (loS + lenS) loD || | |
| Nat.ble (loD + lenD) loS then | |
| .ok (f3, m3) | |
| else if hadCast then | |
| .error .unsupported | |
| else if Nat.beq loS loD && Nat.beq lenS lenD then | |
| if CppTy.beq srcTy dstTy then .ok (f3, m3) | |
| else .error .unsupported | |
| else .error .undefined | |
| | _, _ => .ok (f3, m3) | |
| else .ok (frame, mem) | |
| | none => .ok (frame, mem) | |
| /-- Zero-initialize the object `dst` designates in memory, leaving record | |
| padding `.uninit` and invalidating any enclosing record's padding (C11 | |
| §6.2.6.1p6, §6.7.9p10/p21). -/ | |
| noncomputable def zeroInitInMemory (prog : CppProgram) (body : CppBody) | |
| (frame : CppFrame) (mem : CppMemory) (dst : CppPlace) (ty : CppTy) : | |
| Except CppFaultKind (CppFrame × CppMemory) := | |
| match resolvePlaceAddr prog body frame mem false dst with | |
| | .error k => .error k | |
| | .ok (r, f2, m2) => | |
| if !CppTy.beq r.ty ty then .error .unsupported | |
| else | |
| match writeZeroMemory prog.policy prog.records m2 r.allocId r.addr | |
| r.subRange ty r.alignCap with | |
| | .error k => .error k | |
| | .ok m3 => | |
| match clobberEnclosingRecords prog.policy prog.records m3 | |
| r.enclosingRecords with | |
| | .ok m4 => .ok (f2, m4) | |
| | .error k => .error k | |
| /-- Zero-initialize uninitialized non-`const` aggregate local `lid` of type `ty` | |
| in fresh stack storage. -/ | |
| noncomputable def zeroInitFreshLocal (prog : CppProgram) (frame : CppFrame) | |
| (mem : CppMemory) (lid : LocalId) (ty : CppTy) : | |
| Except CppFaultKind (CppFrame × CppMemory) := | |
| match cppTySize? prog.policy prog.records ty, | |
| cppTyAlign? prog.policy prog.records ty with | |
| | some sz, some al => | |
| let (aid, mem1) := allocStack mem sz al false | |
| if !Nat.blt mem1.nextAddr (ptrAddrBound prog.policy) then | |
| .error .unsupported | |
| else | |
| match findAlloc? aid mem1.allocs with | |
| | none => .error .undefined | |
| | some alloc => | |
| match writeZeroMemory prog.policy prog.records mem1 (some aid) | |
| alloc.baseAddr none ty none with | |
| | .error k => .error k | |
| | .ok m2 => | |
| match cppSetElem? frame.locals lid (.stackAddr aid) with | |
| | none => .error .unsupported | |
| | some locs' => | |
| .ok ({ frame with locals := locs', | |
| stackAllocs := aid :: frame.stackAllocs }, m2) | |
| | _, _ => .error .unsupported | |
| /-- Execute one `CppStmt` inside the top call frame. -/ | |
| noncomputable def evalStmt (prog : CppProgram) (body : CppBody) | |
| (frame : CppFrame) (mem : CppMemory) (stmt : CppStmt) : | |
| Except CppFaultKind (CppFrame × CppMemory) := | |
| match stmt with | |
| | .storageLive lid => | |
| match cppGetElem? body.locals lid, cppGetElem? frame.locals lid with | |
| | some decl, some slot => | |
| if Nat.beq lid 0 || decl.isParam then .error .unsupported | |
| else | |
| match slot with | |
| | .stackAddr _ | .val _ => | |
| -- Re-entering `storageLive` without `storageDead` while an earlier | |
| -- value or stack allocation is still live disagrees between C11 | |
| -- §6.2.4p6 (same object across backward jump in block) and C++20 | |
| -- [stmt.jump]p2 (new incarnation), and would reset a live `const` | |
| -- local to `.uninit`. | |
| .error .unsupported | |
| | .dead | .uninit => | |
| match cppSetElem? frame.locals lid .uninit with | |
| | some locs' => | |
| .ok ({ frame with locals := locs', stmtIdx := frame.stmtIdx + 1 }, | |
| mem) | |
| | none => .error .unsupported | |
| | _, _ => .error .unsupported | |
| | .storageDead lid => | |
| match cppGetElem? body.locals lid, cppGetElem? frame.locals lid with | |
| | some decl, some slot => | |
| if Nat.beq lid 0 || decl.isParam then .error .unsupported | |
| else | |
| match slot with | |
| | .stackAddr aid => | |
| let mem' := freeAlloc mem aid | |
| match cppSetElem? frame.locals lid .dead with | |
| | some locs' => | |
| .ok ({ frame with locals := locs', stmtIdx := frame.stmtIdx + 1 }, | |
| mem') | |
| | none => .error .unsupported | |
| | _ => | |
| match cppSetElem? frame.locals lid .dead with | |
| | some locs' => | |
| .ok ({ frame with locals := locs', stmtIdx := frame.stmtIdx + 1 }, | |
| mem) | |
| | none => .error .unsupported | |
| | _, _ => .error .unsupported | |
| | .assign dst dstTy rhs => | |
| match evalExpr prog body frame mem rhs with | |
| | .error k => .error k | |
| | .ok (v, f1, m1) => | |
| if !exprPtrCompat dstTy rhs || | |
| !checkCppVal prog.policy prog.records v dstTy then | |
| .error .unsupported | |
| else | |
| match assignOverlapCheck prog body f1 m1 dst dstTy rhs with | |
| | .error k => .error k | |
| | .ok (f2, m2) => | |
| match writePlaceVal prog body f2 m2 dst dstTy v with | |
| | .error k => .error k | |
| | .ok (f3, m3) => | |
| .ok ({ f3 with stmtIdx := f3.stmtIdx + 1 }, m3) | |
| | .zeroInit dst ty => | |
| -- A scalar local is stored directly; anything else is zeroed in memory | |
| -- (leaving record padding `.uninit` per C11 §6.7.9p10/p21). | |
| let res : Except CppFaultKind (CppFrame × CppMemory) := | |
| match dst.projs, zeroValOf? ty with | |
| | [], some v => writePlaceVal prog body frame mem dst ty v | |
| | [], none => | |
| -- Whole aggregate local: uninitialized non-`const` aggregates get fresh | |
| -- zeroed storage; `const` aggregate `zeroInit` fails closed with | |
| -- `.unsupported` because `CppStmt` has no post-initializer freeze | |
| -- boundary separating partial initialization (`const S s = { 1 }`) from | |
| -- post-init mutation; re-zeroing an initialized `const` local modifies | |
| -- a `const` object (`.undefined`). | |
| match cppGetElem? body.locals dst.base, | |
| cppGetElem? frame.locals dst.base with | |
| | some decl, some .uninit => | |
| if !CppTy.beq decl.ty ty || decl.isConst then .error .unsupported | |
| else zeroInitFreshLocal prog frame mem dst.base ty | |
| | some decl, _ => | |
| if !CppTy.beq decl.ty ty then .error .unsupported | |
| else if decl.isConst then .error .undefined | |
| else zeroInitInMemory prog body frame mem dst ty | |
| | none, _ => .error .unsupported | |
| | _ :: _, _ => zeroInitInMemory prog body frame mem dst ty | |
| match res with | |
| | .error k => .error k | |
| | .ok (f2, m2) => .ok ({ f2 with stmtIdx := f2.stmtIdx + 1 }, m2) | |
| /-- Execute one `CppTerminator` inside the top call frame. -/ | |
| noncomputable def evalTerminator (prog : CppProgram) (body : CppBody) | |
| (frame : CppFrame) (callers : List CppFrame) (mem : CppMemory) | |
| (term : CppTerminator) : CppStepResult CppState CppTerminalOut := | |
| match term with | |
| | .goto target => | |
| if Nat.blt target body.blocks.length then | |
| .step { frames := { frame with blockId := target, stmtIdx := 0 } | |
| :: callers, | |
| mem := mem } | |
| else | |
| .fault .unsupported | |
| | .branch cond thenTarget elseTarget => | |
| match evalExpr prog body frame mem cond with | |
| | .error k => .fault k | |
| | .ok (v, f1, m1) => | |
| match v with | |
| | .bool b => | |
| let target := if b then thenTarget else elseTarget | |
| if Nat.blt target body.blocks.length then | |
| .step { frames := { f1 with blockId := target, stmtIdx := 0 } | |
| :: callers, | |
| mem := m1 } | |
| else | |
| .fault .unsupported | |
| | _ => .fault .unsupported | |
| | .call callee args dst next => | |
| match evalExprList prog body frame mem args with | |
| | .error k => .fault k | |
| | .ok (argVals, f1, m1) => | |
| match findBodyByName? prog.bodies callee with | |
| | none => .fault .unsupported | |
| | some (calleeId, calleeBody) => | |
| if !bodyRuntimeAdmitted prog calleeBody then | |
| .fault .unsupported | |
| else if !exprListPtrCompat calleeBody.sig.params args || | |
| !checkCppArgs prog.policy prog.records argVals | |
| calleeBody.sig.params then | |
| .fault .unsupported | |
| else if !refArgsOk prog.policy prog.records m1 | |
| calleeBody.sig.paramNonNull calleeBody.sig.params | |
| argVals || | |
| !argsLiveInMem prog.policy prog.records m1 argVals | |
| calleeBody.sig.params then | |
| .fault .undefined | |
| else if !(match dst with | |
| | some (p, _) => p.projs.isEmpty | |
| | none => true) then | |
| -- C11 §6.5.16p3 leaves a projected destination's evaluation | |
| -- unsequenced with the call; only whole locals are admitted. | |
| .fault .unsupported | |
| else if Nat.ble body.blocks.length next then | |
| .fault .unsupported | |
| else | |
| let calleeFrame : CppFrame := { | |
| defId := calleeId | |
| blockId := calleeBody.entryBlock | |
| stmtIdx := 0 | |
| locals := initLocalSlots calleeBody argVals | |
| retDst := dst | |
| retBlock := next | |
| stackAllocs := [] | |
| } | |
| .step { frames := calleeFrame :: f1 :: callers, mem := m1 } | |
| | .ret => | |
| let retRes : Except CppFaultKind CppVal := | |
| match body.sig.ret with | |
| | .void => .ok .void | |
| | _ => readLocalScalar prog body frame mem 0 | |
| match retRes with | |
| | .error k => .fault k | |
| | .ok retVal => | |
| if !checkCppVal prog.policy prog.records retVal body.sig.ret then | |
| .fault .unsupported | |
| else | |
| let mem' := freeAllocs mem frame.stackAllocs | |
| -- Returning a scalar pointer to a local of the returning function yields | |
| -- a dangling, indeterminate value (C11 §6.2.4p2); an aggregate with a | |
| -- dead member pointer is `.unsupported` (C11 §6.2.6.1p6 vs. C++20). | |
| if !valLiveInMem prog.policy prog.records mem' retVal body.sig.ret then | |
| match body.sig.ret with | |
| | .ptr _ _ _ _ => .fault .undefined | |
| | _ => .fault .unsupported | |
| else | |
| match callers with | |
| | [] => .done (.returned { ret := retVal, mem := mem' }) | |
| | caller :: restCallers => | |
| match cppGetElem? prog.bodies caller.defId with | |
| | none => .fault .unsupported | |
| | some callerBody => | |
| match frame.retDst with | |
| | none => | |
| .step { frames := { caller with blockId := frame.retBlock, | |
| stmtIdx := 0 } :: restCallers, | |
| mem := mem' } | |
| | some (dstPlace, dstTy) => | |
| if !CppTy.beq body.sig.ret dstTy || !dstPlace.projs.isEmpty then | |
| .fault .unsupported | |
| else | |
| match writePlaceVal prog callerBody caller mem' dstPlace dstTy | |
| retVal with | |
| | .error k => .fault k | |
| | .ok (caller', mem'') => | |
| .step { frames := { caller' with blockId := frame.retBlock, | |
| stmtIdx := 0 } :: restCallers, | |
| mem := mem'' } | |
| | .unreachable => .fault .undefined | |
| /-- Deterministic single-step operational transition for Rung-0 C11. -/ | |
| noncomputable def evalStep (prog : CppProgram) (s : CppState) : | |
| CppStepResult CppState CppTerminalOut := | |
| match s.frames with | |
| | [] => .fault .unsupported | |
| | frame :: callers => | |
| match cppGetElem? prog.bodies frame.defId with | |
| | none => .fault .unsupported | |
| | some body => | |
| match cppGetElem? body.blocks frame.blockId with | |
| | none => .fault .unsupported | |
| | some blk => | |
| match cppGetElem? blk.stmts frame.stmtIdx with | |
| | some stmt => | |
| match evalStmt prog body frame s.mem stmt with | |
| | .error k => .fault k | |
| | .ok (frame', mem') => | |
| .step { frames := frame' :: callers, mem := mem' } | |
| | none => | |
| evalTerminator prog body frame callers s.mem blk.term | |
| /-- Unbounded finite-step reachability between intermediate `CppState`s. -/ | |
| inductive CppReaches (prog : CppProgram) : CppState → CppState → Prop where | |
| | refl (s : CppState) : CppReaches prog s s | |
| | tail {s1 s2 s3 : CppState} : | |
| CppReaches prog s1 s2 → | |
| evalStep prog s2 = .step s3 → | |
| CppReaches prog s1 s3 | |
| /-- Outcome `res` is reached from `(prog, q)` (either at entry via | |
| `initCppState` or in finitely many steps from `s0`). -/ | |
| def CppReachesResult (prog : CppProgram) (q : CppQuery) | |
| (res : CppStepResult CppState CppTerminalOut) : Prop := | |
| match initCppState prog q with | |
| | .error k => res = .fault k | |
| | .ok s0 => | |
| ∃ s : CppState, CppReaches prog s0 s ∧ evalStep prog s = res | |
| /-- Infinite-trace divergence of `(prog, q)` in Rung-0 C11. -/ | |
| def CppDiverges (prog : CppProgram) (q : CppQuery) : Prop := | |
| ∃ s0 : CppState, | |
| initCppState prog q = .ok s0 ∧ | |
| ∃ trace : Nat → CppState, | |
| trace 0 = s0 ∧ | |
| ∀ n : Nat, evalStep prog (trace n) = .step (trace (n + 1)) | |
| /-- All-prefix well-formedness in C11 and C++20: entry succeeds | |
| (`initCppState prog q = .ok s0`), no reachable transition faults | |
| (`.undefined` or `.unsupported`), and no execution diverges (`¬ CppDiverges`), | |
| because the side-effect-free model has no I/O, `volatile`, or atomic operations | |
| and non-terminating observable-free loops have Undefined Behavior in C++20 | |
| [intro.progress]p1 (and in C11 §6.8.5p6 for non-constant controlling | |
| expressions). -/ | |
| def CppWellFormed (prog : CppProgram) (q : CppQuery) : Prop := | |
| (∃ s0 : CppState, initCppState prog q = .ok s0) ∧ | |
| (∀ k : CppFaultKind, ¬ CppReachesResult prog q (.fault k)) ∧ | |
| ¬ CppDiverges prog q | |
| /-- Observable terminal behavior of `(prog, q)` in Rung-0 C11. -/ | |
| def CppBehavior (prog : CppProgram) (q : CppQuery) | |
| (out : CppTerminalOut) : Prop := | |
| CppReachesResult prog q (.done out) | |
| /-- Existential UB fault witness predicate in Rung-0 C11. -/ | |
| def CppFaultsUB (prog : CppProgram) (q : CppQuery) : Prop := | |
| CppReachesResult prog q (.fault .undefined) | |
| end Trusted.CppSemantics | |
| end |